1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Two Syntax errors

Discussion in 'Mac Programming' started by fishcrackers, Jan 22, 2008.

  1. macrumors newbie

    #1
    I am having a problem with my program and I need some help. Anything would be greatly appreciated. I am running a guessing game.

    Syntax error, insert "while ( Expression ) ;" to complete DoStatement
    Syntax error, insert "}" to complete MethodBody


    Here is my code:

    import java.util.Scanner;
    import java.util.Random;

    public class Driver
    {
    public static void main(String[] args)
    {
    int z;
    Double answer;
    Double y;
    Double n;
    Scanner stdIn;
    Random rand = new Random();
    int x = rand.nextInt(100)+1;
    do
    {
    System.out.println("Please guess a random number;");
    z = stdIn.nextInt();
    while(z != x)
    {
    if(z>x)
    {
    System.out.println("Too High. Guess Again:");
    z = stdIn.nextInt();
    }
    else if(z<x)
    {
    System.out.println("Too Low. Guess Again:");
    z = stdIn.nextInt();
    {
    System.out.println("Thats Correct!! Play again y or n?");
    answer = stdIn.nextDouble();

    }

    }while(answer == y);

    System.out.println("Thanks for playing!");
    }

    }
     
  2. macrumors 65816

    #2
    Yay for indenting. Use [ code ] [ /code ] tags next time.

    Your else if...the end bracket is { instead of }

    Code:
    public class Driver
    {
        public static void main(String[] args)
        {
            int z;
            Double answer;
            Double y;
            Double n;
            Scanner stdIn;
            Random rand = new Random();
            int x = rand.nextInt(100)+1;
    
            do
            {
                System.out.println("Please guess a random number;");
                z = stdIn.nextInt();
                while(z != x)
                {
                     if(z>x)
                     {
                          System.out.println("Too High. Guess Again:");
                          z = stdIn.nextInt();
                     }
                     else if(z<x)
                     {
                          System.out.println("Too Low. Guess Again:");
                          z = stdIn.nextInt();
                     {
                     System.out.println("Thats Correct!! Play again y or n?");
                     answer = stdIn.nextDouble();
    
                }
    
         }while(answer == y);
    
         System.out.println("Thanks for playing!");
      }
    
    } 
    
     
  3. macrumors newbie

    #3
    Thanks for the help with that now I have another problem.

    The constructor Scanner() is undefined

    [ code ]
    import java.util.Scanner;
    import java.util.Random;

    public class Driver
    {
    public static void main(String[] args)
    {
    int z;
    Double answer;
    Double y;
    Double n;
    Scanner stdIn = new Scanner();
    Random rand = new Random();
    int x = rand.nextInt(100)+1;
    do
    {
    System.out.println("Please guess a random number;");
    z = stdIn.nextInt();
    while(z != x)
    {
    if(z>x)
    {
    System.out.println("Too High. Guess Again:");
    z = stdIn.nextInt();
    }
    else if(z<x)
    {
    System.out.println("Too Low. Guess Again:");
    z = stdIn.nextInt();
    }
    System.out.println("Thats Correct!! Play again y or n?");
    answer = stdIn.nextDouble();

    }

    }while(answer == y);

    System.out.println("Thanks for playing!");
    }

    }
    [ /code ]
     
  4. macrumors G5

    gnasher729

    #4
    Well, yes. If you look at the documentation of java.util.Scanner, can you find any constructor with no arguments?
     
  5. macrumors newbie

    #5
    Got that one now for another problem. At the end I want them to either enter an "y" or an "n". Depending on if they want to run the program again. I need help doing that.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The local variable y may not have been initialized
    The local variable n may not have been initialized
    The local variable a may not have been initialized

    at Driver.main(Driver.java:10)


    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Driver 
    {
    	public static void main(String[] args) 
    	{
    		int z;
    		Double a;
    		Double y = y;
    		Double n = n;
    		Scanner stdIn = new Scanner(System.in);
    	    Random rand = new Random();
    	    int x = rand.nextInt(100)+1;
    	    do
    	    {
    	    	
    	    	System.out.println("Please guess a random number;");
    	    	z = stdIn.nextInt();
    	    	while(z != x)
    	    	{
    	    		if(z>x)
    	    		{
    	    			System.out.println("Too High. Guess Again:");
    	    			z = stdIn.nextInt();
    	    		}
    	    		else if(z<x)
    	    		{
    	    			System.out.println("Too Low. Guess Again:");
    	    			z = stdIn.nextInt();
    	    		}
    	    		System.out.println("Thats Correct!! To play again press y or n to exit.");
    	    		a = stdIn.nextDouble();
    	    		
    	    	}
    	    	
    	    }while(a == y);
    	    
    	    System.out.println("Thanks for playing!");
    	 }
    	   
    }[/code}
     
  6. macrumors newbie

    #6
    Help Plz - DoWhile loops

    Got that one now for another problem. At the end I want them to either enter an "y" or an "n". Depending on if they want to run the program again. I need help doing that.

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    The local variable y may not have been initialized
    The local variable n may not have been initialized
    The local variable a may not have been initialized

    at Driver.main(Driver.java:10)

    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Driver 
    {
    public static void main(String[] args) 
    {
    int z;
    Double a;
    Double y = y;
    Double n = n;
    Scanner stdIn = new Scanner(System.in);
    Random rand = new Random();
    int x = rand.nextInt(100)+1;
    do
    {
    
    System.out.println("Please guess a random number;");
    z = stdIn.nextInt();
    while(z != x)
    {
    if(z>x)
    {
    System.out.println("Too High. Guess Again:");
    z = stdIn.nextInt();
    }
    else if(z<x)
    {
    System.out.println("Too Low. Guess Again:");
    z = stdIn.nextInt();
    }
    System.out.println("Thats Correct!! To play again press y or n to exit.");
    a = stdIn.nextDouble();
    
    }
    
    }while(a == y);
    
    System.out.println("Thanks for playing!");
    }
    
    }
     
  7. macrumors member

    #7
    You should definitly use String or char to store the y/n values (char would probably be easier, seeing it's simple to make comparisons.).

    Also, there's no need to store the y and n values, seeing as you only compare them one time in your code, so:
    Code:
    a == 'y'
    should probably do.

    I might be a good idea to take a look at some documentation about types, btw.
     
  8. macrumors newbie

    #8
    Thanks, that helped make it run but when I got to run the program again or end it i get this error message.

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at Driver.main(Driver.java:34)
     
  9. macrumors member

    #9
    Try this instead of the stdIn.nextDouble line
    Code:
    a = stdIn.next().charAt(0);
     
  10. macrumors newbie

    #10
    I then got another error. It must be something else. Thank you though. Anyone please...
     
  11. macrumors newbie

    #11
    Plz help doWhile loop question

    I need help making my program repeat and or end. I give the user the option to run the program again or to end it. When I in a y for yes or n for n. I get this error message.

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at Driver.main(Driver.java:34)


    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Driver 
    {
    	public static void main(String[] args) 
    	{
    		int z;
    		Double a;
    		Double y;
    		Double n;
    		Scanner stdIn = new Scanner(System.in);
    	    Random rand = new Random();
    	    int x = rand.nextInt(100)+1;
    	    do
    	    {
    	    	
    	    	System.out.println("Please guess a random number:");
    	    	z = stdIn.nextInt();
    	    	while(z != x)
    	    	{
    	    		if(z>x)
    	    		{
    	    			System.out.println("Too High. Guess Again:");
    	    			z = stdIn.nextInt();
    	    		}
    	    		else if(z<x)
    	    		{
    	    			System.out.println("Too Low. Guess Again:");
    	    			z = stdIn.nextInt();
    	    		}
    	    	}
    	    	System.out.println("Thats Correct!! To play again press y or n to exit.");
    		    a = stdIn.nextDouble();
    	    	
    	    }while('a' == 'y');
    	    
    		
    	    System.out.println("Thanks for playing!");
    	 }
    	   
    }
     
  12. macrumors 601

    HiRez

    #12
    It's been a long time since I've used Java, but what is this code supposed to be doing?

    Code:
    Double y = y;
    Double n = n;
    It looks like you're assigning something to itself (when it doesn't exist yet). Do you want to assign the integer value of the characters 'y' and 'n'? Why are you using a Double type? Maybe I don't understand it but that assignment code seems fishy to me.
     
  13. macrumors 6502a

    #13
    Is this your third thread on the same little program? :confused:

    I don't see any reason for the following:

    Double y
    Double n

    You don't need those two references, they don't seem to used anywhere.

    Then the next issue is why are you reading the response to your question as a double?

    and lastly you have a problem in the conditional ('a' == 'y'). That is comparing the literal 'a' against a literal 'y', which will always be false.

    What you probably meant was (a == 'y'). But you really probably want (a.equals("y")) with 'a' as a String.

    Or use an InputStreamReader instead to read a single char.
     
  14. macrumors newbie

    #14
    Help plz one more time

    I can enter 1 or 2 to end the program but it always ends the program and goes with the last "thanks for playing" I cannot get it to repeat the program

    Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class Driver 
    {
    	public static void main(String[] args) 
    	{
    		
    		int a;
    		int y = 1;
    		int z = 0;
    		Scanner stdIn = new Scanner(System.in);
    	    Random rand = new Random();
    	    int x = rand.nextInt(100)+1;
    	    do
    	    {
    	    	
    	    	System.out.println("Please guess a random number:");
    	    	z = stdIn.nextInt();
    	    	while(z != x)
    	    	{
    	    		if(z>x)
    	    		{
    	    			System.out.println("Too High. Guess Again:");
    	    			z = stdIn.nextInt();
    	    		}
    	    		else if(z<x)
    	    		{
    	    			System.out.println("Too Low. Guess Again:");
    	    			z = stdIn.nextInt();
    	    		}
    	    	}
    	    	System.out.println("Thats Correct!! To play again press 1 or 2 to exit.");
    		    a = stdIn.nextInt();
    	    	
    	    }while(a == 'y');
    	    
    		
    	    System.out.println("Thanks for playing!");
    	 }
    	   
    }
     
  15. macrumors G5

    gnasher729

    #15
    I think at some point you should really try to figure out a few things yourself. But could you please answer the following questions:

    1. What is the difference between

    a

    and

    'a'

    and

    "A"

    2. What is the meaning of double and what is the meaning of Double?

    If you can answer these questions, then I think you will get a lot lot further, and you don't need to do "programming by guessing" anymore.
     
  16. macrumors 6502a

    #16
    Dude, no one is going spend the time to chase down every thread you're creating to help you.

    Stick to one thread, please. So people can get the context of what you're trying to do. Plus at this rate you'll have 20 threads created tonight.

    The reason your program always ends is because the conditional is still (a='y') but now you've switched to ints (1 or 2).

    You need (a=1) or (a=2). It's not clear whether you want the user to type 1 or 2 to continue.
     
  17. jsw
    Moderator emeritus

    jsw

    #17
    <threads merged>
     

Share This Page