Skip to main content

reached end of file while parsing (sorry, no other threads over this have helped me with my problem)

3 replies [Last post]
dberry01
Offline
Joined: 2011-02-18
Points: 0

import java.util.*;
public class game
{
public static void main(String[] args)
{
int number = (int) (100*Math.random())+1;
int guess = 150;
int attempts = 0;
string again = 'a';
Scanner keyboard = new Scanner(System.in);
system.out.print("Welcome to the Guess the Number Game /n++++++++++++++++++++++++++++++++++++");
system.out.println("I'm thinking of a number from 1 to 100. Try to guess it.");

while (guess != number){
system.out.println("Enter Number:");
guess = keyboard.nextInt();
if (guess > 100 || guess < 0) system.out.println("Guess invalid!");
if (guess > 0 && guess < 100) {
if (guess - 10 > number) system.out.println("Way Too high!");
if (guess > number && guess - 10 <number) sytem.out.println("Too high!");
if (guess < number) system.out.println("Too low!");
attempts ++;
if (guess == number && attempts <= 3) {system.out.println("You got it in " + attempts + " attempts! /nGreat work! You are a mathematical wizard.");
break;}
if (guess == number && attempts > 7) system.out.println("You got it in " + attempts + " attempts! /nWhat took you so long? Maybe you should take some lessons.");
else system.out.println("You got it in " + attempts + " attempts! /nNot too bad! You've got some potential."); }}
while (again != 'y' && again != 'n'){
system.out.println("Would you like to play again? (y/n) :");
again = keyboard.nextInt();
if (again != 'y' && again != 'n') {system.out.println("Must answer with a 'y' or an 'n'.");
}
if (again == 'y') number = (int) (100*Math.random())+1;
guess = 150;
if (again == 'n') break;
}
}

Main.java:36: reached end of file while parsing
}
^
1 error

Someone help please. I'm new to java and can't find the answer online :\

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
wizpar
Offline
Joined: 2011-02-23
Points: 0

incorrect number of braces { }

should look like this with code fix and logic fix and clean up:

import java.util.Scanner;
import java.util.InputMismatchException;
public class Game {
public static void main(String[] args) {
char again = 'y';
Scanner keyboard = new Scanner(System.in);
System.out.print("Welcome to the Guess the Number Game /n++++++++++++++++++++++++++++++++++++");
do {
if (again == 'y') {
guessMyNumber(keyboard);
}
System.out.println("\nWould you like to play again? (y/n): ");
again = keyboard.next().toLowerCase().charAt(0);
if (again != 'y' && again != 'n') {
System.out.println("Must answer with a 'y' or a 'n'.");
}
} while (again != 'n');
}
private static void guessMyNumber(Scanner keyboard) {
int number = (int) (100 * Math.random()) + 1;
int guess = 0;
int attempts = 0;

System.out.println("\nI'm thinking of a number from 1 to 100. Try to guess it.");
do {
System.out.print("\nEnter Number: ");
try{
guess = keyboard.nextInt();
} catch(InputMismatchException e) {
System.out.println("Invalid input. Only numbers are allowed.");
keyboard.nextLine();
continue;
}

attempts++;
if (guess > 100 || guess < 1) {
System.out.println("Guess invalid!");
} else {
if (guess - 10 > number) {
System.out.println("Way Too high!");
} else if (guess > number) {
System.out.println("Too high!");
} else if (guess < number) {
System.out.println("Too low!");
} else {
if (attempts <= 3) {
System.out.println("You got it in "
+ attempts
+ " attempt"
+ (attempts == 1 ? "" : "s")
+ "! \nGreat work! You are a mathematical wizard.");
} else if (attempts <= 7) {
System.out.println("You got it in "
+ attempts
+ " attempts! \nNot too bad! You've got some potential.");
} else {
System.out.println("You got it in "
+ attempts
+ " attempts! \nWhat took you so long? Maybe you should take some lessons.");
}
}
}
} while (guess != number);
}
}

OneJumpedUp
Offline
Joined: 2012-04-05
Points: 0

Here is the WORKING version:

    public static void main(String[] args) 
    {
        int number;
        int guess;
        int attempts;
        String again, msg;
        Scanner keyboard = new Scanner(System.in);
        do
        {
            number = (int) (100 * Math.random()) + 1;
            attempts = 0;
            msg = null;
           
            System.out.print("Welcome to the Guess the Number Game\n" );
            System.out.print("++++++++++++++++++++++++++++++++++++\n");
            System.out.println("I'm thinking of a number from 1 to 100. Try to guess it.\n");

            do
            {
                System.out.println("Enter Number:");
                guess = keyboard.nextInt();
                if( guess > 100 || guess < 0 )
                {
                    System.out.println("Guess invalid!");
                }
                else
                {
                    if(( guess - 10 ) > number )
                    {
                        System.out.println("Way Too high!");
                    }
               
                    if(( guess > number ) && (( guess - 10 ) < number ))
                    {
                        System.out.println("Too high!");
                    }
               
                    if( guess < number )
                    {
                        System.out.println("Too low!");
                    }
               
                    attempts++;                   
                    if( guess == number && attempts <= 3 )
                    {
                        msg = " attempts!\nGreat work! You are a mathematical wizard.";
                        break;
                    }
                    if( guess == number && attempts > 7 )
                    {
                        msg = " attempts!\nWhat took you so long? Maybe you should take some lessons.";
                    }
                    else
                    {
                        msg =" attempts!\nNot too bad! You've got some potential.";
                    }
                }
            }
            while( guess != number);
           
            System.out.println("You got it in " + attempts + msg );
            System.out.println("Would you like to play again? (y/n) :");
            again = keyboard.next();
            if( !( again.equalsIgnoreCase( "y" ) || again.equalsIgnoreCase( "n" )))
            {
                System.out.println("Must answer with a 'y' or an 'n'.");
            }
           
            if( again.equalsIgnoreCase( "y" ))
            {
                number = (int) (100 * Math.random()) + 1;
            }

            guess = 150;
            if( again.equalsIgnoreCase( "n" ))
            {
                break;
            }
        }
        while( again.equalsIgnoreCase("y") || again.equalsIgnoreCase( "n" ));
    }
OneJumpedUp
Offline
Joined: 2012-04-05
Points: 0

Hey
You got a few problems here, typos, missing curlies, .... look at the above code: