Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: I am a java newb and I am having a problem with my while loops.

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I am a java newb and I am having a problem with my while loops.

    I have been teaching myself java for fun. I found a site that gives assignments so I have been trying to teach myself by completing the programs assigned by the site. This assignment asked that I write a program asking the user to guess a random number. Anyways, this is like my 3rd day of teaching myself this stuff so I am basically feeling it out as I go. Can anybody tell me how to fix this? Or, perhaps more importantly, why this doesn't work? Any help would be appreciated.


    Thanks!!!

    The errors the program is giving me are all in the underlined line. They are:
    -Syntax error on token 'while' delete this token.
    -Syntax error on token 'boolean' delete


    Things I have tried:
    -I tried defining the booleans outside of the while loop.
    -I tried writing in public static void main whatever (I don't fully understand what that code does yet but it semeed to impact the way the boolean instances interacted.)

    Here is the code:

    import java.io.*;
    public class pickanumber {
     
     
    	int x = (int) ((Math.random()*10)+1)
     
     while [boolean rightguess1 = false];
     
    {	
     
    	try {
     
     
    	Reader instream = null;
    	BufferedReader userguess = new BufferedReader (instream);
     
    	System.out.println("Pick a whole number between 1 and 10");
    	String usguess = userguess.readLine();
    	int uguess1 = Integer.parseInt(usguess);
     
    	if (!(uguess1==x)) {
    	System.out.println("Try again");
    	boolean rightguess1 = false;
     
    	}
    	else {
    		boolean rightguess1 = true;
    	}}
    	catch (IOException err) {
    		System.out.println("Only whole numbers please");
    	}
    	System.out.println("Nice job.  The number was " + x);
     
    }
    	}


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Well, I did just figure out that if I remove the word boolean from the code inside of the while loop it connects the boolean at the top of the while loop to the booleans inside of the loop. The problem; however, remain unsolved.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    In future you need to copy and paste the full and exact error messages. They contain vital information. You also need to copy and paste the latest version of your code. Even if you make a small change. Remember we cannot see your screen and rely on you providing as much info as we need to solve your problem.

    Another thing, sometimes when the compiler spits out an error it might be on the previous line to the one it complains about. HINT HINT. I also just noticed that none of your code is inside a method.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    I will re-post next time. I did post the errors. They were:

    The errors the program is giving me are all in the underlined line. They are:
    -Syntax error on token 'while' delete this token.
    -Syntax error on token 'boolean' delete


    Can you point me to a good resource regarding methods? It sounds like I misunderstood something basic somewhere.

    Thanks!!!

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Here is the latest version of my code.

     
    import java.io.*;
    public class pickanumber {
     
     
    	int x = (int) ((Math.random()*10)+1)
      while [boolean rightguess1 = false];
     
    {	
     
    	try {
     
     
    	Reader instream = null;
    	BufferedReader userguess = new BufferedReader (instream);
     
    	System.out.println("Pick a whole number between 1 and 10");
    	String usguess = userguess.readLine();
    	int uguess1 = Integer.parseInt(usguess);
     
    	if (!(uguess1==x)) {
    	System.out.println("Try again");
    	 rightguess1 = false;
     
    	}
    	else {
    		break;
    	}}
    	catch (IOException err) {
    		System.out.println("Only whole numbers please");
    	}
    	System.out.println("Nice job.  The number was " + x);
     
    }
    	}

  6. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Quote Originally Posted by Deaththekid View Post
    I did post the errors.
    That's not an error message. This is an error message!
    Test.java:5: cannot find symbol
    symbol  : variable n
    location: class Test
            System.out.println(n);
                               ^
    1 error
    What you posted was a paraphrased version of the error message which usually omits valuable information. This is why we want people to post the EXACT error message they get.
    Improving the world one idiot at a time!

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    while [boolean rightguess1 = false];
    That is not even valid Java syntax.
    You are making an assignment and not a comparison. Beside when using booleans you can use them directly in your conditional statement.
    boolean bool = true;
    if(bool)
    if(! bool)
    Another problem may be obvious if I change your code slightly
    while [boolean rightguess1 = false]; {
    Notice anything out of place? (assuming all other errors have been fixed)
    Last edited by Junky; July 8th, 2011 at 02:37 AM.
    Improving the world one idiot at a time!

  8. #8
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Quote Originally Posted by Deaththekid View Post
    Can you point me to a good resource regarding methods? It sounds like I misunderstood something basic somewhere.
    Any decent book or online tutorial will explain methods.
    Improving the world one idiot at a time!

  9. #9
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: I am a java newb and I am having a problem with my while loops.

    Thread moved to - Loops & Control Statements
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  10. #10
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    I re-wrote my code but now I have a new error.

    Exception in thread "main" java.lang.NoSuchMethodError: main


    Here is my new code. Hopefully it will be easier to read:



    import java.io.*;
    //imports whatever I need for the buffer reader (not sure how that works yet)

    public class pickanumber {
    //declares class

    public void guessnum (boolean[] args) {

    int x = (int) ((Math.random()*10)+1);

    boolean whiletest = false;
    while (whiletest = false) {
    //declares whiletest to be false and begins a loop contingent upon that.
    //Making whiletest true ends the loop.

    System.out.println("Pick a whole number between 1 and 10");

    BufferedReader ubuff = new BufferedReader(new InputStreamReader(System.in));
    try {
    String usguess = ubuff.readLine();
    int i = Integer.parseInt(usguess);
    if (i==x) {
    System.out.println("Nice job the number was " + x);
    whiletest = true;
    }

    else {
    System.out.println("Sorry, try again");
    whiletest = false;
    }
    }


    catch (IOException ioe) {
    System.out.println("User input not valid.");
    System.out.println("All your base are belong to me.");
    }
    }



    }

    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    I have a new error.

    Exception in thread "main" java.lang.NoSuchMethodError: main
    Can you explain what you did to get this error message. Is it when you tried to execute your program?
    What command did you execute?

    The java command requires that the class you pass to it have a special method: main coded as:
    public static void main(String[] args) {
    // put code here to start the class going
    } // end main()

  12. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Yes. That's how I got the error message.

  13. #13
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Thanks for the tip. I finally got past the compiler. Now I can run the program, but when it runs it says program terminated. Also, there is an error on line 11 of my code. It gets past the compiler but it says "local variable whiletest is never read." I am very confused. My whole loop is built on that boolean so it is utilized. I guess I am not sure what read means. As far as I can tell the logic should work. Where am I going wrong? Here is my new code.



    import java.io.*;
    //imports whatever I need for the buffer reader (not sure how that works yet)

    public class pickanumber {
    //declares class

    public static void main(String[] args) {
    int x = (int) ((Math.random()*10)+1);

    boolean whiletest = false;
    while (whiletest = false) {
    //declares whiletest to be false and begins a loop contingent upon that.
    //Making whiletest true ends the loop.

    System.out.println("Pick a whole number between 1 and 10");

    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader ubuff = new BufferedReader (istream);
    try {
    String usguess = ubuff.readLine();
    int i = Integer.parseInt(usguess);
    if (i==x) {
    System.out.println("Nice job the number was " + x);
    whiletest = true;
    }

    else {
    System.out.println("Sorry, try again");
    whiletest = false;
    }
    }


    catch (IOException ioe) {
    System.out.println("User input not valid.");
    System.out.println("All your base are belong to me.");
    }
    }



    }

    }

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Please copy and paste the full text of the error message here.
    Also please wrap your posted code in code tags to preserve formatting.
    See: BB Code List - Java Forums

    while (whiletest = false) {
    This is an assignment statement not a comparison

  15. #15
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Hah I can't believe it was that simple. Thanks Norm! I fixed it. I did give you the full error message that eclipse gave me. That's all it said when I clicked on the little error icon.

    Here is the final code:


    import java.io.*;
    //imports whatever I need for the buffer reader (not sure how that works yet)

    public class pickanumber {
    //declares class

    public static void main(String[] args) {
    int x = (int) ((Math.random()*10)+1);

    boolean whiletest = false;
    while (whiletest == false) {
    //declares whiletest to be false and begins a loop contingent upon that.
    //Making whiletest true ends the loop.

    System.out.println("Pick a whole number between 1 and 10");

    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader ubuff = new BufferedReader (istream);
    try {
    String usguess = ubuff.readLine();
    int i = Integer.parseInt(usguess);
    if (i==x) {
    System.out.println("Nice job the number was " + x);
    whiletest = true;
    }

    else {
    System.out.println("Sorry, try again");
    whiletest = false;
    }
    }


    catch (IOException ioe) {
    System.out.println("User input not valid.");
    System.out.println("All your base are belong to me.");
    }
    }



    }

    }

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Please wrap your posted code in code tags to preserve formatting.
    See: BB Code List - Java Forums

    Most programmers would change this:
    while (whiletest == false) {
    to this
    while (!whiletest) {

    Actually it would be like this:
    boolean whileTest = true; // control while loop while testing
    while (whileTest) {
    then set to false when done testing.
    Last edited by Norm; July 9th, 2011 at 02:16 PM.

  17. #17
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Thanks for the tips. I will play with those to see if I can get my program working that way as well. Also, I found that I did still have an error. The catch (method?) whatever the term for that is was not working. I did some research and found out why. Here is the new completely and fully functional code.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class picknumadvanced {
    	//declares class
     
    public static void main(String[] args) { 	
    int x = (int) ((Math.random()*10)+1);
     
    boolean whiletest = false;
    while (whiletest  == false) {
    	//declares whiletest to be false and begins a loop contingent upon that.
    	//Making whiletest true ends the loop.
     
    	System.out.println("Pick a whole number between 1 and 10");
     
        InputStreamReader istream = new InputStreamReader(System.in);
        BufferedReader ubuff = new BufferedReader (istream);
        	try {
        		String usguess = ubuff.readLine();
        		int i = Integer.parseInt(usguess);		
        	 	if (i==x) {
        			System.out.println("Nice job the number was " + x);
        			whiletest = true;
        			 }
     
        	 	else {
        			System.out.println("Sorry, try again");
        			whiletest = false;
        			 }	   
        	    }
     
     
        	catch (NumberFormatException nfe) {
        		System.out.println("User input not valid.");
        		System.out.println("All your base are belong to me.");
     
        	}
        	catch (IOException ioe) {
        		System.out.println("You suck");
        	}
    }
     
     
     
    }
     
    }

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader ubuff = new BufferedReader (istream);
    These could be outside the loop. No need to create them every time you loop

  19. #19
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Here is the more refined version based on your reccomendations Norm. They let me remove a few lines of code.

    Thanks for all the help!

    On to the next challenge.

     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
     
    public class picknumadvanced {
    	//declares class
     
    public static void main(String[] args) { 	
    int x = (int) ((Math.random()*10)+1);
     
    boolean whiletest = true;
    while (whiletest) {
    	//declares whiletest to be false and begins a loop contingent upon that.
    	//Making whiletest true ends the loop.
     
    	System.out.println("Pick a whole number between 1 and 10");
     
        InputStreamReader istream = new InputStreamReader(System.in);
        BufferedReader ubuff = new BufferedReader (istream);
        	try {
        		String usguess = ubuff.readLine();
        		int i = Integer.parseInt(usguess);		
        	 	if (i==x) {
        			System.out.println("Nice job the number was " + x);
        			whiletest = false;
        			 }
     
        	 	else {
        			System.out.println("Sorry, try again");
     
        			 }	   
        	    }
     
     
        	catch (NumberFormatException nfe) {
        		System.out.println("User input not valid.");
        		System.out.println("All your base are belong to me.");
     
        	}
        	catch (IOException ioe) {
        		System.out.println("You suck");
        	}
    }
     
     
     
    }
     
    }

  20. #20
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Quote Originally Posted by Norm View Post
    InputStreamReader istream = new InputStreamReader(System.in);
    BufferedReader ubuff = new BufferedReader (istream);
    These could be outside the loop. No need to create them every time you loop
    Ok so once I create the buffreader I only need to reference it via string inside of the loop?

  21. #21
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Err, to amend that statement... well. How exactly does the buffreader work?

  22. #22
    Junior Member
    Join Date
    Jul 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Also what sort of error would trigger this catch statement?
    catch (IOException ioe) {
    System.out.println("You suck");
    }

    Java required that I have an input output exception like this but I can't figure out what type of error would trigger it.

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: I am a java newb and I am having a problem with my while loops.

    Do a Google. Anytime you try to read something it is possible for there to be something going wrong and throwing an exception.

Similar Threads

  1. [SOLVED] newb help..
    By Hallowed in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 05:15 PM
  2. Replies: 2
    Last Post: January 7th, 2011, 09:10 PM
  3. Replies: 10
    Last Post: September 6th, 2010, 05:48 PM
  4. Java Loops help please?
    By Sarah-Perkin in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2009, 02:52 PM
  5. How to Use different Java loops
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: August 28th, 2009, 04:10 AM