participate


New To Java - Scanner works in JCreator but doesn't when run from Command Prompt?
This question is answered. Correct Answer available

<<   Back to Forum  |   Give us Feedback
This topic has 6 replies on 1 page.
SamColt
Posts:4
Registered: 11/3/09
Scanner works in JCreator but doesn't when run from Command Prompt?   
Nov 4, 2009 12:48 AM
 
 
Can someone tell me why I can run this program in JCreator with no problems, but when I run it with the Command prompt it throws a java.util.InputMismatchException if I input anything?

import java.util.Scanner;
 
public class tester {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in).useDelimiter("\n");
        System.out.println("Type either 1 or 2");
        int temp = in.nextInt();
        System.out.println("");
        if (temp == 1)
        	System.out.println("you typed 1");
        if (temp == 2)
        	System.out.println("you typed 2");
        else
        	System.out.println("you typed something else!");
    }
}
 
amp88
Posts:87
Registered: 12/22/08
Re: Scanner works in JCreator but doesn't when run from Command Prompt?   
Nov 4, 2009 1:46 AM (reply 1 of 6)  (In reply to original post )
 
 
SamColt wrote:
Can someone tell me why I can run this program in JCreator with no problems, but when I run it with the Command prompt it throws a java.util.InputMismatchException if I input anything?

 Scanner in = new Scanner(System.in).useDelimiter("\n"); 

Why are you explicitly specifying the new line character as the delimiter? Try running it without that...
 
Mrityunjay_Kumar
Posts:18
Registered: 5/23/07
Re: Scanner works in JCreator but doesn't when run from Command Prompt?   
Nov 4, 2009 4:07 AM (reply 2 of 6)  (In reply to original post )
 
 
Try this code it is running correctly on command prompt

import java.util.Scanner;
 
public class tester {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type either 1 or 2");
        int temp = in.nextInt();
        System.out.println("");
        if (temp == 1)
        	System.out.println("you typed 1");
        if (temp == 2)
        	System.out.println("you typed 2");
        else
        	System.out.println("you typed something else!");
    }
}
 
SamColt
Posts:4
Registered: 11/3/09
Re: Scanner works in JCreator but doesn't when run from Command Prompt?   
Nov 4, 2009 11:43 AM (reply 3 of 6)  (In reply to #1 )
 
 
I need the delimiter because it is an essential part to the actual program. Mostly I'm just wondering why it runs with JCreator but throws an error in the command prompt.
 
endasil
Posts:2,767
Registered: 10/12/07
Re: Scanner works in JCreator but doesn't when run from Command Prompt?   
Nov 4, 2009 11:50 AM (reply 4 of 6)  (In reply to #3 )
Correct
 
SamColt wrote:
I need the delimiter because it is an essential part to the actual program. Mostly I'm just wondering why it runs with JCreator but throws an error in the command prompt.
It's giving you that exception because from a Windows console, a newline is "\r\n". Because the scanner encounters the "\n", and you've explicitly set that as the delimiter, it thinks that "\r" must be part of the first token, and so it tries to parse it as part of an integer when clearly it's not. JCreator must send just "\n" to stdin when you press Enter.

If you just leave the Scanner at the default delimiter, it will correctly handle both "\n" and "\r\n" as newlines.
 
SamColt
Posts:4
Registered: 11/3/09
Re: Scanner works in JCreator but doesn't when run from Command Prompt?   
Nov 4, 2009 12:05 PM (reply 5 of 6)  (In reply to #4 )
 
 
endasil wrote:
It's giving you that exception because from a Windows console, a newline is "\r\n". Because the scanner encounters the "\n", and you've explicitly set that as the delimiter, it thinks that "\r" must be part of the first token, and so it tries to parse it as part of an integer when clearly it's not. JCreator must send just "\n" to stdin when you press Enter.

Thanks that helps a ton.
 
SamColt
Posts:4
Registered: 11/3/09
Re: Scanner works in JCreator but doesn't when run from Command Prompt?   
Nov 4, 2009 12:52 PM (reply 6 of 6)  (In reply to original post )
 
 
Easy fix keeping the "\n" delimiter:

Scanner in = new Scanner(System.in).useDelimiter("\n|\r\n");
 
This topic has 6 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums