Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
First off, I am a BEGINNER in Java. I am finally taking my core classes in college. I am in Computer Science 1 and I'm correcting a code I got from a book as a practice so I can gain a better understanding on how to fix codes. It's a simple code, however, I keep running into 3 errors every single time. I need advice on how to correct them! I am new to all of this so it can be frustrating at times. Thanks!
public class Mistakes
{
public static void main(String[] args);
{
int y;
int x = 12;
y + x = y;
System.out.println("This is what y + x equals: "+y);
System.out.print("This is what t equals: "+t);
}
}
I keep running into 3 errors:
java:3: error: missing method body, or declare abstract
public void main(String[] args);
^
java:7: error: unexpected type
y + x = y;
^
required: variable
found: value
java:9: error: cannot find symbol
System.out.print("This is what t equals: "+t);
^
symbol: variable t
location: class Mistakes
Do I change t into x?
Do I change public class to public abstract?
Any advice will be greatly appreciated!
First, your main() method has a ; after it's declaration. This is allowed only if the method you are declaring is abstract and, thus, has no "body". In this case, you should remove the semicolon. Look below:
public static void main(String[] args) {
//Your code here
}
Second, your assignment operation is wrong. In Java (and in programming in general) you must first specify a variable and then the value it is going to receive (literally or, in your case, through an expression). You should do it as shown below:
y = x + y; //the value of y will be equal to x+y
In this case you could even use a shortcut, if you want to:
y += x; //this expression will have the same effect as the shown above
Finally, you are getting the last error because the variable t wasn't declared, so the method System.out.print() is trying to print a variable that doesn't exist. In this case, you should either remove the symbol t or declare a variable with this name, like I do below:
int t = 3;
System.out.print("This is what t is equals to " + t); //t will be 3
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have doubt in using functions in java. I have wrote code for sum of natural numbers using a recursive function but I don't understand the error I am getting. I know it's a silly question though I'm a beginner and I need a brief explanation.
Here is the code
import java.util.Scanner;
public class natural {
public static int main(String args[]){
int a, s = 0,y;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number:");
int x = in.nextInt();
public static int SN(y)
{
if(x==1)
{
return 1;
}
else{
int N = SN(x-1) + x;
return N;
System.out.println("THE SUM IS :"+x);
}
}
Several problems:
You cannot declare a method within a method. Your SN method must be declared outside of the main method.
The parameter y in your SN method must have a type. Based on usage, it is probably supposed to be an int, so the method signature should look like SN(int y).
Despite the method parameter being called y, you appear to be using x everywhere. You should change x to y in the SN method, since that is the label of the data being passed to the method.
As others have pointed out, statements after the return line are unreachable, and as Matt Coubrough said, your IDE is likely warning you about this. Place it before the return line.
Well, one problem here is that you have an unreachable statement. Your System.out.println("THE SUM IS...") is never reached.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
The following Java code segment is supposed to print, as a double, the mean average of a sequence of non-negative integers entered by the user. A negative input signals the end of the sequence (it is not itself part of the sequence). However, the code is not working. I am supposed to find 4 logic errors within this segment. Please help me find the 4 logic errors?? I know one is its integer division.
public class practice
{
public static void main (String[]args)
{
int sum = 0;
int numVals = 0;
Scanner scan = new Scanner(System.in);
System.out.println(("enter next integer (-ve to stop): "));
int i = scan.nextInt();
while (i > 0)
{
sum = sum + i;
numVals = numVals + 1;
}
System.out.println("average = " + sum / numVals);
}
}
I won't give you full solution, however, it'll be helpful if you pay attention to:
int division as you said,
does your loop terminate? Is someone changing i? Hint: No, and
how do you ask for input? Do you see any loops there? Why it's asking you for only one input?
Not an error, but pay attention to Java Naming Conventions, class name should begin with upper case
Since the homework is for logic errors, I could point out other errors.
the class name should start with upper case letter.
the println doesn't need two nested parenthesis.
the sum should be a long rather than an int to avoid overflows.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I am not sure if you see what I am trying to do here but basically I have a few questions and problems
1)The part that is called public MethodPractice() ... what is this called? is this considered a constructor a method or what?
2)The part named MethodPracticeDiff() . . . is this allowed and if so how do I insert it into the main method for execution...
Do you guys see what I am trying to do here? Basically I want to split the program up into different pieces for example let say I wanted my own space for a calculation method to add to numbers up
and another method to define the numbers like give them a value
and a last method with a for loop making the numbers printout 10times
Any who before I make this more confusing than what it is, my question is how do I make this program execute
public class MethodPractice {
public static void main (String[]args){
MethodPractice add = new MethodPractice();
//MethodPracticeDiff add2 = new MethodPracticeDiff();
}
public MethodPractice() {
int x = 0;
int y = 99 ;
int total = x + y;
System.out.println(total);
}
public void MethodPracticeDiff(){
int z = 10;
int k = 25;
int total = z + k;
System.out.println(total);
}
}
(1) If it's in class MethodPractice, it's a constructor.
(2) Yes, this is allowed. But it's a method not a constructor. Standard practice is to begin it with a lowercase letter.
As follows in the main() method:
MethodPractice add = new MethodPractice();
add.methodPracticeDiff();
MethodPractice() is a constructor -- it has no return value and matches the name of the class.
MethodPracticeDiff() is a method -- it has a return value and does not match the name of a class.
You call methods once you have an instance of the class. e.g.
MethodPractice add = new MethodPractice();
add.MethodPracticeDiff();
long time searcher but first time I'm posting a question. I am an IT student going into [haven't started yet] my second programming class. The first was just an intro to Java (we're talking basics really). I have been having a hard time with calling methods within the same class. I have attempted search-foo with poor results. A few articles pop up but they don't cover exactly what I'm looking for. Included is an example (quickly and probably poorly written) to get across what I'm asking. The basic gist [remember to stay with me since I'm new to programming in general] is that I want to add two numbers, creating a third, and have the system display the result...
public class MethodCallExample{
public static void main(String[] args){
int valueTwo = 3;
MethodToCall();
int valueOne;
int TrueValue = valueOne + valueTwo;
System.out.println("The total value is " + TrueValue + "!");
}
public static int MethodToCall(){
int valueOne = 2;
return valueOne;
}
}
When I go to compile I get one of two errors depending on which derp I try to underp. If I compile as its' written, I receive a "valueOne might not have been initialized" error, and if I either move or remove -int valueOne - I receive "cannot find symbol" referring to valueOne. Any help is greatly appreciated since I am still learning.
Sincerely,
Hubert Farnsworth
When you call MethodToCall, you aren't doing anything with the returned value. You need to store the returned value in your variable i.e.
int valueOne = MethodToCall();
It looks like you are confused with the variable scopes. Try doing
int valueOne = MethodToCall();
Inside your main.
When you return something then you need a variable to hold the returned value.. so
int valueone = methodtovalue();
would be correct..
Also note that the variable declared in a function would lose its scope when it is reaches the main program because the variable is declared in function. So valueone in function is different from the valueone declared in main() because valueone in function has its scope only within function and valueone in main has its scope till the end of mainprogram
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Attempting to write some code to convert feet to meters and meters to feet and achieve the table shown below. This is my first java program and I have no idea why I'm getting syntax errors involving "}".
Feet Meters| Meters Feet
1.0 0.305 | 20.0 65.574
2.0 0.61 | 25.0 81.967
…
9.0 2.745 | 60.0 196.721
10.0 3.05 | 65.0 213.115
Here's what I have...
public class Hmwk {
public static void main(String[] args){
public static double footToMeter(double foot){
return 0.305 * foot;
}
public static double meterToFoot(double meter){
return 3.279 * meter;
}
for (double i = 1.0; i <11; i++){
System.out.printf(i+footToMeter(i)+"|"+(i*5+15)+meterToFoot(i*5+15));}
}}
Any and all help is much appreciated.
Here's a small tip, if you don't like indenting code, then coding might not be for you.
This may sound harsh, but I sincerely believe that before one writes his/her first program, one needs to know what code structure is all about. Unfortunately, schools and courses tend to hide or ignore this fact completely. I know, I've seen this back at secondary school - telling your teacher to properly format code isn't nice believe me.
public class Hmwk {
public static double footToMeter(double foot){
return 0.305 * foot;
}
public static double meterToFoot(double meter){
return 3.279 * meter;
}
public static void main(String[] args){
for (double i = 1.0; i <11; i++){
System.out.printf(i + footToMeter(i) + "|" + (i*5+15) + meterToFoot(i*5+15));
}
}
}
In Java, you cannot have methods inside methods.
Indenting code means you can find your problems easily.
Brackets are meant to stay on separate lines, except the opening brackets (depends on taste)
You have a lot of braces in incorrect positions. Most notably, your footToMeter and meterToFoot are declared inside of your main method, which is incorrect. Here is your code with correct brace placement:
public class Hmwk
{
public static void main(String[] args)
{
for (double i = 1.0; i <11; i++)
{
System.out.printf(i+footToMeter(i)+"|"+(i*5+15)+meterToFoot(i*5+15));
}
}
public static double footToMeter(double foot)
{
return 0.305 * foot;
}
public static double meterToFoot(double meter)
{
return 3.279 * meter;
}
}
As a general rule, methods cannot be nested in Java. This means that no method can be declared inside of another.
No closing brace in the void main. Put '}' before 'public static double footToMeter' and delete closing bracket after closing bracket from for