Jump to content

  • Log In with Google      Sign In   
  • Create Account

Need help in java! "reached end of file while parsing"


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Donal Byrne   Members   -  Reputation: 128

Like
0Likes
Like

Posted 24 May 2012 - 05:42 PM

ok so i just started learning java tonight and was doing one of the projects in the book I'm learning from "programming video games for the evil genius"

as i finished the project I kept trying to close the brackets but it kept saying "while expecting" and " reached end of file while parsing"

I've re read the code and the book 6 times and i can't find the mistake. Any help would be really appreciated.

Heres the code. The program is a maths flash card game. it randomly generates a simple sum and it gives you a set amount of time to figure out the answer




//first, allow for input getting
import javax.swing.*;

//create a class named S1P4
public class S1P4
{
private static int sign;
//main method (throws Exception) added for Thread.sleep()
public static void main (String[] args) throws Exception
{
//this will be how many 1/2 seconds the user gets
int difficulty;
difficulty = Integer.parseInt(JOptionPane.showInputDialog("How good are you?\n"+
"1 = Genius..\n"+"10 = Retart"));
//this will tell the loop whether to continue or not:
boolean cont = false;
//the contents of the main method are about to be enclosed in a
// do while loop...
do
{
//reset cont to false
cont = false;

//random numbers for the equation
double num1 = (int)(Math.round(Math.random()*10));
//this do..while loop prevents exceptions. num2 mustbe
//declared outside of the do while so the "while" part can
//see it. It will still be initialized inside the do part,
//thought

double num2;
{
//init num2
num2 = (int)(Math.round(Math.random()*3));
//will store answer
double answer;

//make stuff noticable
System.out.println("\n\n*****");

if(sign==0)
{
//tell user and calculate answer
System.out.println(num1+"times"+num2);
answer = num1*num2;
}
else if(sign==1)
{
//tell user and calculate answer
System.out.println(num1+"divided by"+num2);
answer = num1/num2;
}
else if(sign==1)
{
//tell user answer and calculate
System.out.println(num1+"plus"+num2);
answer = num1+num2;
}
else if(sign==1)
{
//tell user answer and calculate
System.out.println(num1+"minus"+num2);
answer = num1-num2;
}
else
{
//tell user and calculate answer
System.out.println(num1+"%"+num2);
answer = num1%num2;
}
//make it easier to read...
System.out.println("*****\n");

//count down from difficulty... use for a loop!
for(int i = difficulty;i>=0;i--)
{
//count down at double speed!
System.out.println(i+"...");

//instead of waiting a second. this time only
//wait 1/2 a second per difficulty level
Thread.sleep(500);
}
//print answer
System.out.println("ANSWER:"+answer);
//ask if they want to play again
String again;
again=JOptionPane.showInputDialog("Play again?");
//if user says yes set cont=true
if(again.equals("yes"))
cont = true;
}
while(cont);
}
}

Edited by Donal Byrne, 24 May 2012 - 05:43 PM.


Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 8424

Like
0Likes
Like

Posted 24 May 2012 - 06:07 PM

You have a { after the declaration of num2 that is not closed correctly.

I'm not sure if your original code looks like this or if it just got eaten by the forums, but on the off chance you're not already doing this: I very strongly suggest you indent your programs. It makes this kind of thing a lot easier to spot:

//first, allow for input getting
import javax.swing.*;

public class S1P4
{
	private static int sign;

	public static void main (String[] args) throws Exception
	{
		int difficulty;
		difficulty = Integer.parseInt(JOptionPane.showInputDialog("How good are you?\n"+
		"1 = Genius..\n"+"10 = Retart"));

		boolean cont = false;
		do
		{
			cont = false;

			double num1 = (int)(Math.round(Math.random()*10));
			double num2;
			{
				num2 = (int)(Math.round(Math.random()*3));
				double answer;

				System.out.println("\n\n*****");

				if(sign==0)
				{
					System.out.println(num1+"times"+num2);
					answer = num1*num2;
				}
				else if(sign==1)
				{
					System.out.println(num1+"divided by"+num2);
					answer = num1/num2;
				}
				else if(sign==1)
				{
					System.out.println(num1+"plus"+num2);
					answer = num1+num2;
				}
				else if(sign==1)
				{
					System.out.println(num1+"minus"+num2);
					answer = num1-num2;
				}
				else
				{
					System.out.println(num1+"%"+num2);
					answer = num1%num2;
				}
				System.out.println("*****\n");

				for(int i = difficulty;i>=0;i--)
				{
					System.out.println(i+"...");

					Thread.sleep(500);
				}

				System.out.println("ANSWER:"+answer);

				String again;
				again=JOptionPane.showInputDialog("Play again?");
				if(again.equals("yes"))
					cont = true;

			// UH OH! No } here!

		} while(cont);
	}
}

Edited by ApochPiQ, 24 May 2012 - 06:08 PM.


#3 Donal Byrne   Members   -  Reputation: 128

Like
0Likes
Like

Posted 24 May 2012 - 06:21 PM

Thanks so much! :D and I don't mean to sound like a completely noob but what do you mean by indent my programs?

#4 ApochPiQ   Moderators   -  Reputation: 8424

Like
0Likes
Like

Posted 24 May 2012 - 06:55 PM

No worries! We all have to start someplace :-)


As for indentation - look at the code you posted, and compare it to mine.

Every time you open a {, start indenting the next line of code with a tab. For each additional {, add a tab. You can use spaces instead of tabs if you wish, it doesn't really matter.

When you close a }, take off a tab. For each } you close, go back in one tab. This will format your code with the nice layout I used instead of everything being squished up against the left hand side of the page. If you get into the habit of doing this, it will be much easier to read your programs, and much easier to find mistakes like a missing }.

#5 dAND3h   Members   -  Reputation: 187

Like
0Likes
Like

Posted 26 May 2012 - 01:06 PM

Although ApochPiQ pointed out the error, I am struggling to see why a code block was needed in that part of the code in the first place. It certainly doesn't make it look any nicer.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS