Results 1 to 4 of 4
  1. #1
    doomking517 is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default while loops spam, help?

    Well I made a calculator program but if they enter a letter not a number my while loops start to spam, could anyone please show me what I have done wrong and how to fix it? (I know its probably something simple but it has me perplexed)

    my code:
    Java Code:
    package Calc1;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    public class Calc1 {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws IOException {
            // TODO code application logic here
    
            Boolean test2 = false;
            Scanner in = new Scanner(System.in);
            int a = 0;
            int b = 0;
            String sum;
           
                System.out.println("Please enter the first number you wish to calculate.");
                Boolean test = false;
                while (!test) {
                    try
                    {
                a = in.nextInt();
                test = true;
                } catch (Exception e)
                {
                    System.out.println("Invalid Number!");
                }
                }
                System.out.println("Please enter the second number you wish to calculate.");
                while (!test2)
                {
                    try {
                b = in.nextInt();
                test2 = true;
                }
                    catch (Exception e)
                    {
                        System.out.println("Invalid Number!");
                    }
                }
                BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
                System.out.println("please enter one of the following operators + - / *");
                sum = br2.readLine();
                if ("+".equals(sum))
                {
                int c = a + b;
                System.out.println(a + "+" + b + "=" + c);
                }
                if ("-".equals(sum))
                {
                int c = a - b;
                System.out.println(a + "-" + b + "=" + c);
                }
                if ("/".equals(sum))
                {
                int c = a / b;
                System.out.println(a + "/" + b + "=" + c);
                }
                if("*".equals(sum))
                {
                int c = a * b;
                System.out.println(a + "*" + b + "=" + c);
                }
        }
          
     }
    If you know how to stop it spamming "invalid number!" then please explain/show me how to fix it, thank you in advance.
    Last edited by doomking517; 06-13-2013 at 12:33 AM.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: while loops spam, help?

    Try adding test=true; after System.out.println("Invalid Number!");

    Or, alternatively, move the test=true you have after the a = in.nextInt(); to be before it, since the exception is being thrown from in.nextInt(), test is never assigned and therefore doesn't stop the loop.

    Another option is to use the break statement to break out of the loop when the input is invalid.

  3. #3
    doomking517 is offline Member
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: while loops spam, help?

    Thank you, that worked perfectly, I need to think more about how I arrange the code I write.
    however i should have been more specific, i also wanted it so that they could re enter the number after it fails. thanks
    Last edited by doomking517; 06-13-2013 at 12:49 AM.

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: while loops spam, help?

    For problems like this, I find it often helps to write the program in English/pseudo-code, then "translate" it to java code.

    But here's a little example I whipped up for you.

    Java Code:
    public static void main(String[] args)
    {
    	Scanner in = new Scanner(System.in);
    	while(true)
    	{
    		System.out.println("Enter number:");
    		try
    		{
    			int a = in.nextInt();
    			// do something with a
    			break;
    		}
    		catch(Exception e)
    		{
    			System.out.println("Invalid input");
    			in.next(); //consume input
    		}
    	}
    	in.close();
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •