the programme below doesnt compile. i was wondering if i could get some assistance in solving this problem. thanks
public class FastEnough{ int speed, memory; public static void main(String[] args) { FastEnough myComputer = new FastEnough(); myComputer.speed = 150; myComputer.checkSpeed(); myComputer.memory = 16; myComputer.checkMemoryAndSpeed(); } public void checkSpeed(){ if(speed < 200) System.out.println(speed + "MHz is too slow"); else if(speed < 300)System.out.println(speed + "MHz is fast enough"); else if(speed < 400)System.out.println(speed + "MHz is very fast"); else if(speed <=450)System.out.println(speed + "MHz is the fast"); else System.out.println("Does your PC really run at"+ speed + "MHz?"); }
public void checkMemoryAndSpeed(){ if((speed< 200)|| (memory<32))System.out.println("Upgrade"); } } public void checkMemory(){ String m; if(memory == 16)m = "far too little"; else if(memory == 32)m = "just enough"; else m = "ample memory"; System.out.println(memory + " MB is "+ m); }
What does the compiler error tell you? Something cryptic about it expects something other than what it found? I'm guessing the text of the message is confusing (if I spotted the right problem it's one of the less helpful messagse around) but the line number points at or right after the problem.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
OK, so look at line 21. Then look right before line 21.
Hint: use a programmer's editor that indents your code, and balances parentheses and braces for you. They make this sort of error much easier to find (and prevent them from happening in the first place.)
If you're going to mention line numbers, may I suggest that you point out which line in your source code is the line in question? AT THE VERY LEAST don't add blank lines when you copy the source code into your message.
If line 1 is "public class FastEnough{", line 21 is one of the blank lines. But if all the blank lines are removed, line 21 is "public void checkMemory(){". Is that line 21 in the source code you're compiling?
The error message means that the compiler has reached the end of one class/interface definition in a file and is expecting the start of a second one. Can you think of why compiler might think it has already reached the end of your class when it gets to the start of the checkMemory() method?
Ryan [ May 23, 2005: Message edited by: Ryan McGuire ]