Java code - Handling exceptions - HELP!!!

Thread Starter

krow

Joined May 25, 2010
49
Hello guys,

A few days ago I posted a similar question about a Java code, here's one more, I hope you can help me out.

The problem I have is this, when you enter a character the program is supposed to loop the whole thing AND run the scanner asking for data until the user (assuming the user is quite dumb) finally enters an integer value, I'm getting an endless loop I don't know why and the program won't run the scanner a second time, why's that?


import java.util.*;

public class tryCatch {
public static void main (String args[]){
Scanner ms = new Scanner(System.in);
int number;
int x=1;

do{
try{

System.out.println("Enter your number: ");
number = ms.nextInt();
System.out.println("Your number is: " + number);
x=2;

}

catch(InputMismatchException f){
System.out.println("Error! integer values, only!");

}

}while(x==1);
}
}
 

SgtWookie

Joined Jul 17, 2007
22,230
Just adding CODE tags to preserve your formatting.
Hello guys,

A few days ago I posted a similar question about a Java code, here's one more, I hope you can help me out.

The problem I have is this, when you enter a character the program is supposed to loop the whole thing AND run the scanner asking for data until the user (assuming the user is quite dumb) finally enters an integer value, I'm getting an endless loop I don't know why and the program won't run the scanner a second time, why's that?


Rich (BB code):
import java.util.*;

public class tryCatch {
        public static void main (String args[]){
            Scanner ms = new Scanner(System.in);
            int number;
            int x=1;

            do{
            try{

            System.out.println("Enter your number: ");
            number = ms.nextInt();
            System.out.println("Your number is: " + number);
            x=2;

        }

        catch(InputMismatchException f){
            System.out.println("Error! integer values, only!");

        }

        }while(x==1);
}
}
 

Thread Starter

krow

Joined May 25, 2010
49
I forgot to mention I'm new to Java, been learning for like 3 weeks now so whoever can help me, please do it as if you were teaching a 10-year old boy :)
 

kubeek

Joined Sep 20, 2005
5,795
try this:
Rich (BB code):
package javaapplication1;

import java.util.*;


public class JavaApplication1 {

    public static void main(String args[]) {
        Scanner ms;
        int number;

       while(true) {
            try {
                ms = new Scanner(System.in);
                System.out.println("Enter your number: ");
                number = ms.nextInt();
                System.out.println("Your number is: " + number);


            } catch (InputMismatchException f) {
                System.out.println("Error! integer values, only!");
            }

        }
    }
}
and please next time use the right click -> format, and use it often.
 

Thread Starter

krow

Joined May 25, 2010
49
Thanks Kubeek,

Well I'm pretty new to programming and I didn't know anything about the format thing when you copy and paste a code.

I fixed the problem by the way, I just added "ms.next();" in the Catch and it worked the way I wanted, thanks anyway.
 
Top