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.
Related
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))
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
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 is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Sometime when we declare and initialize a variable, say we have an int i =10; then after some code this variable would be modified like this code bellow
public class reset {
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 10;
int co = 1;
while (co < 10) {
i++;
System.out.println(i + "*" + co + "=" + i * co);
if (i == 99) {
i = 11; //line 11
co++;
}
}
}
}
then at some point (here at line 11) we need to re-initialize then variable, wouldn't it be nice if we had any language feature doing it automatically instead for example
reset:i
I think it's very beneficial for productivity, isn't it?
we need to re-initialize then variable, wouldn't it be nice if we had any language feature doing it automatically instead?
No
I think it's very beneficial for productivity, isn't it?
No
Resetting a variable to its start value is in many cases a sign that the scope of the variable is to large. So with clean code you hardly ever need such feature.
And of course every feature comes at the cost of complicating the language even more.
The initializer line
int i = 10;
simply creates byte code instructions to assign the value 10 to the variable. That assignment is no different than any other assignment.
To implement reset, there would need to be an extra bit of metadata kept for each variable to say what the special, initial value is.. That metadata is not currently kept in the symbol table, since there is no concept in Java for a 'initial value'. The additional overhead would trade off against the utility of the reset command.
Might be a good idea, but you can get the same thing by just declaring a constant, and reassigning to the constant.
What about this code? I can understand your question from a starter's perspective, but usually it requires a bit more practice to see why certain constructs are not required:
public class NoReset {
private static final int X_START = 11;
private static final int X_END = 99;
private static final int Y_START = 1;
private static final int Y_END = 9;
public static void main(String[] args) {
for (int y = Y_START; y <= Y_END; y++) {
for (int x = X_START; x <= X_END; x++) {
final int result = x * y;
System.out.printf("%d * %d = %d%n", x, y, result);
}
}
}
}
Note that you should not nest to many loops, but creating a "hidden loop" is at least as dangerous, it gets very hard to track variables such as i within your code.
If you want to use the current Java language to do what you want to do, then simply provide a function in each class that you want to perform a "reset" like:
private void reset() {
var = xxx;
var2 = yyy;
...
}
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