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 7 of 7

Thread: Scanner.useDelimiter("\n") Problem

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

    Default Scanner.useDelimiter("\n") Problem

    Hey guys I am having some trouble when I use Scanner.UseDelimeter("\n") basically I have:

    Scanner s = new Scanner(System.in);
              s.useDelimiter("\n");
     
              int amount;
     
              System.out.println("Enter the amount needed: ")
              amount = s.nextInt();


    After I enter the integer for the amount, the program crashes. I use the delimiter for the scanner as I am using scanner.NextLine() later in the program.

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Scanner.useDelimiter("\n") Problem

    the program crashes.
    Please copy and paste the full text of the error message here.

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

    Default Re: Scanner.useDelimiter("\n") Problem

    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at DessertManager.main(DessertManager.java:39)

    Line 39 is amount = s.nextInt();

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Scanner.useDelimiter("\n") Problem

    java.util.InputMismatchException
    This says that the input being read was not an integer.
    What did you type into the console that caused this?

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

    Default Re: Scanner.useDelimiter("\n") Problem

    Just the number 1.

    If I remove the s.useDelimiter("\n");

    There is no problems, but I was told to use this to solve another problem, which is using nextLine() before a nextInt().

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Scanner.useDelimiter("\n") Problem

    I guess using the \n changes the way Scanner works.
    Here is what I tried and what I got:

           Scanner s = new Scanner(System.in);
           s.useDelimiter("\n");  //<<<<<<<<<<
           System.out.println("Enter the amount needed: ");
           System.out.println("hasNextInt=" + s.hasNextInt() + ", hasNext="  + s.hasNext());
           String line = s.nextLine();
           System.out.println("line=" + line + "<");
    /*
          D:\JavaDevelopment\Testing\ForumQuestions6>java TestCode5
          Enter the amount needed:
          1 2 the end
          hasNextInt=false, hasNext=true
          line=1 2 the end<
    */

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

    Default Re: Scanner.useDelimiter("\n") Problem

    Yeah the \n changes it. I might just not use it, and use my other solution to the problem I was having. Appreciate your help anyway!

Similar Threads

  1. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  2. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 07:13 AM
  3. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  4. [SOLVED] Is "Public void closeFile()" a problem in the program for AS-Level computing project
    By muffin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2009, 09:12 PM
  5. [SOLVED] "GridLayout" problem in Java program
    By antitru5t in forum AWT / Java Swing
    Replies: 3
    Last Post: April 16th, 2009, 10:26 AM