Infinite loop when unexpected variable type is input

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2516 Views - Last Post: 14 July 2010 - 07:06 PM Rate Topic: -----

#1 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 123
  • Joined: 25-May 09

Infinite loop when unexpected variable type is input

Posted 12 July 2010 - 08:11 PM

Hello

I have a problem, I am trying to prompt the user to enter a number so I have designed my function to continue to loop (& ask again for a number) if it receives invalid input.

So when my function prompts the user to enter a number & then the user enters a character, my function should output "Invalid input" then ask the user again for a number...

But what actually happens with my function when the user enters a character is that my program goes into an infinite loop.

Why does this happen & how can I fix my code to just output "Invalid input" then ask again for the user to input a number.

          Scanner in = new Scanner( System.in );
		boolean validInput = false;
		int playerDecision = -1;
		
		while ( !validInput )
		{
			try
			{
				System.out.printf("Enter a number: ");
			    playerDecision = in.nextInt();  
			    // If a char is entered here my program goes into an infinite loop
			    
			    if ( playerDecision < 10 )
			        
			    	validInput = true;
			}
			catch (Exception e)
			{
				System.out.println("Invalid input");
			}
			
		}


Is This A Good Question/Topic? 0
  • +

Replies To: Infinite loop when unexpected variable type is input

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 724
  • View blog
  • Posts: 4,470
  • Joined: 24-October 08

Re: Infinite loop when unexpected variable type is input

Posted 12 July 2010 - 08:15 PM

if the user enters a character, nextInt() method will through an exception, and that exception will be caught in the catch block, what you need to do in the catch statement is clear the scanner, so that it advances to the next input
        Scanner in = new Scanner( System.in );
boolean validInput = false;
int playerDecision = -1;

while ( !validInput )
{
	try
	{
		System.out.printf("Enter a number: ");
	    playerDecision = in.nextInt();  
	    // If a char is entered here my program goes into an infinite loop
	    
	    if ( playerDecision < 10 )
	        
	    	validInput = true;
	}
	catch (Exception e)
	{
		System.out.println("Invalid input");
                in.nextLine(); //right here
	}
	
}



hope this helps
Was This Post Helpful? 0
  • +
  • -

#3 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 123
  • Joined: 25-May 09

Re: Infinite loop when unexpected variable type is input

Posted 12 July 2010 - 08:49 PM

Thanks that worked :)

one other question how do I take user input that is just a char (not a string but just a char)?

I know if I want the user to input a string I use: in.next();
I know if I want the user to input a int I use: in.nextInt();
I know if I want the user to input a float I use: in.nextFloat();

but what do I use if I just want to take in one character?
Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 724
  • View blog
  • Posts: 4,470
  • Joined: 24-October 08

Re: Infinite loop when unexpected variable type is input

Posted 12 July 2010 - 09:11 PM

you could do the following
Scanner sc = new Scanner(System.in);
char c = sc.next().charAt(0);


Was This Post Helpful? 0
  • +
  • -

#5 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 123
  • Joined: 25-May 09

Re: Infinite loop when unexpected variable type is input

Posted 12 July 2010 - 09:17 PM

Thanks for the reply.

I could do that, but isn't there a way to take in just a char in Java?

Maybe if I dont use the Scanner but use BufferedReader()?
Was This Post Helpful? 0
  • +
  • -

#6 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Infinite loop when unexpected variable type is input

Posted 12 July 2010 - 09:19 PM

Edit:
Double post, my bad

This post has been edited by Luckless: 12 July 2010 - 09:20 PM

Was This Post Helpful? 0
  • +
  • -

#7 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Infinite loop when unexpected variable type is input

Posted 12 July 2010 - 09:19 PM

to get a single character, you have to take a substring:

import java.util.Scanner;


public class Test {
	
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		String c = scan.next();
		c = c.substring(0,1);
	}
}



Scanner has no method for chars that I know of
Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 724
  • View blog
  • Posts: 4,470
  • Joined: 24-October 08

Re: Infinite loop when unexpected variable type is input

Posted 12 July 2010 - 09:41 PM

View PostLuckless, on 13 July 2010 - 06:19 AM, said:

to get a single character, you have to take a substring:

import java.util.Scanner;


public class Test {
	
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		String c = scan.next();
		c = c.substring(0,1);
	}
}


substring will return a string, the user wants to obtain a char, using charAt() is more suitable here.

@OP: there's no method in Java that would just obtain a char from the user.. you could try this actually

char c = (char)System.in.read()


This post has been edited by mostyfriedman: 12 July 2010 - 09:41 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Infinite loop when unexpected variable type is input

Posted 13 July 2010 - 03:15 PM

View Postmostyfriedman, on 12 July 2010 - 10:41 PM, said:

@OP: there's no method in Java that would just obtain a char from the user.. you could try this actually

char c = (char)System.in.read()


I am afraid that can't work... we would see it's use a lot more often... InputStream.read() is an abstract method that should be overload by the classes that extend InputStream
Was This Post Helpful? 0
  • +
  • -

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 724
  • View blog
  • Posts: 4,470
  • Joined: 24-October 08

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 09:25 AM

i've actually tried it a few years ago and it worked, I would try it now to recheck but my laptop doesn't have jdk installed right now
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,960
  • Joined: 15-July 08

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 09:31 AM

View Postpbl, on 13 July 2010 - 05:15 PM, said:

View Postmostyfriedman, on 12 July 2010 - 10:41 PM, said:

@OP: there's no method in Java that would just obtain a char from the user.. you could try this actually

char c = (char)System.in.read()


I am afraid that can't work... we would see it's use a lot more often... InputStream.read() is an abstract method that should be overload by the classes that extend InputStream


pbl, it actually does work...Give it a shot!
        char c = (char)System.in.read();
        System.out.println(c);



Quote

input: jay
output: j

Was This Post Helpful? 1
  • +
  • -

#12 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 09:39 AM

mostlyfriedman, are you thinking about c instead of java? I know it can be done in C.

OP "one other question how do I take user input that is just a char (not a string but just a char)?"
Was This Post Helpful? 0
  • +
  • -

#13 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 724
  • View blog
  • Posts: 4,470
  • Joined: 24-October 08

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 11:04 AM

no, i wasn't thinking about C. Anyway Dogstopper already confirmed that its working.
Was This Post Helpful? 0
  • +
  • -

#14 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10179
  • View blog
  • Posts: 37,584
  • Joined: 27-December 08

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 11:08 AM

View PostNoobKnight, on 14 July 2010 - 12:39 PM, said:

mostlyfriedman, are you thinking about c instead of java? I know it can be done in C.

OP "one other question how do I take user input that is just a char (not a string but just a char)?"

Take a look at using the Scanner class to do such. You can pass System.in to its constructor, and get console input that way. It also allows for reading in most primitives except char, and some other types of Objects like BigDecimal. It is very versatile and has a ton of features.
Was This Post Helpful? 0
  • +
  • -

#15 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Infinite loop when unexpected variable type is input

Posted 14 July 2010 - 11:13 AM

View Postmostyfriedman, on 14 July 2010 - 12:04 PM, said:

no, i wasn't thinking about C. Anyway Dogstopper already confirmed that its working.


But, the OP wants to enter one character without using the "enter" key, not a string as Dogstopper tried.

input: jay
output: j
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2