Java: Syntax error on token "}"? [closed] - java

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

Related

What is the reason behind this kind of error? [closed]

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 1 year ago.
Improve this question
I'm very new to programming and I've been teaching myself for almost a month now, can someone please explain to methe reason behind error in my code? It errors in the "total(moneyConv(moneySum * moneyRate));" line,
saying actual and formal argument differs in length. I've check all my parameters and it seemed fine to me. thanks a lot!
public class NewClass {
static Scanner sc = new Scanner(System.in);
public static float moneySum;
public static float moneyRate;
public static void findSum(float sum) {
moneySum = sum;
}
public static void findRate(float rate) {
moneyRate = rate;
}
public static float moneyConv(float sum, float rate) {
return sum * rate;
}
public static void total(float total) {
System.out.println(total + "Here is the total of your transaction.");
}
public static void main(String[] args) {
System.out.print("Input amount of money : ");
findSum(sc.nextFloat());
System.out.print("Input exchange rate : ");
findRate(sc.nextFloat());
total(moneyConv(moneySum * moneyRate));
}
the moneyConv() method parameters have 2 arguments: a float datatype sum and another float datatype rate. When you look at the method call:
total(moneyConv(moneySum * moneyRate));
you are actually trying to call the method moneyConv(float sum, float rate) but with one argument instead with a float datatype, as a result of the multiplication of moneySum and moneyRate. This is not valid since moneyConv accepts 2 arguments.
So, the fix would be total(moneyConv(moneySum, moneyRate));
First the nested method moneyConv(moneySum,moneyRate) will be executed and after the method total will be executed with the result of the moneyConv method.
Method parameters must be separated by commas:
total(moneyConv(moneySum, moneyRate));
moneySum * moneyRate is first evaluated and becomes a single value which is passed to moneyConv which actually requires two arguments.
It is equivalent to:
float temporary = moneySum * moneyRate;
total(moneyConv(temporary))

Java Beginner - Java Code Errors Advice Needed [closed]

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

Frustrating Syntax Issues with passing values into methods [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I needed to rewrite a java program I had already completed to now include a class I would be able to test with using JUnit4. Unfortunately I can't even get to that point because I'm receiving an error. It's a really simple program where I ask the user for 3 numbers, pass those values into a function that does some calculations and should return a print statement with the value back. I made it work without the function and I'm having trouble with the syntax and fully understanding what I can and can't do with methods.
Here it is:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner kybd = new Scanner(System.in);
System.out.println("Given ax^2 + bx^2 + c = 0");
System.out.println("Please enter 'a', 'b', and 'c' to determine if there are any roots: ");
float numA = kybd.nextFloat();
float numB = kybd.nextFloat();
float numC = kybd.nextFloat();
quadraticAnswer(numA, numB, numC);
}
public static void float quadraticAnswer (float numA, float numB, float numC){
float discriminant = ((numB*numB)-(4*numA*numC));
if (discriminant < 0){
System.out.println("The Equation has no roots!");
}
else if (discriminant ==0) {
float root = (-numB + Math.sqrt(discriminant))/(2*numA);
System.out.println("The Equation has one root: "+ root);
}
else {
float root1 = (-numB + Math.sqrt(discriminant))/(2*numA);
float root2 = (-numB - Math.sqrt(discriminant))/(2*numA);
System.out.println("The Equation has two roots: " + root1 + " and " + root2 + ".");
}
}
}
Change the invalid syntax of
public static void float quadraticAnswer
to
public static void quadraticAnswer
as you are not returning anything.
If you use an IDE like Eclipse it will quickly highlight such errors

Using functions in Java Programming [closed]

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.

Error in Eclipse: "java.lang.Error: Unresolved compilation" [closed]

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
I am getting this error message in eclipse java.lang.Error: Unresolved compilation problem. This is the code.
public class AreaOfSquare {
public static void main (String [] args){ }
int base;
int height;
int area;
base = 10;
height = 10;
area = base * height;
}
The error seems to be around declaring the last variable but i don't know what the problem is...
"java.lang.Error: Unresolved compilation problem"
This means that you're trying to run code that won't compile, something that you should never do. Instead, you should compile first and fix all compilation errors before trying to run your code.
As for your specific problem, your curly braces don't go around your main method body, and your class is missing a closing brace. You have code dangling out where it doesn't belong, neither in a method, nor constructor, nor initializer block.
So change this:
public class AreaOfSquare {
public static void main (String [] args){ }
int base;
int height;
int area;
base = 10;
height = 10;
area = base * height;
}
to this:
public class AreaOfSquare {
public static void main (String [] args){
int base;
int height;
int area;
base = 10;
height = 10;
area = base * height;
System.out.println("area is: " + area);
}
}
You will want to study the first few chapters of any good Java textbook which will show you how to construct simple Java classes that compile and work.

Categories