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

Thread: Unable to use Same scanner twice

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Post Unable to use Same scanner twice

    I have been messing around to learn about java
    and improving my simple calculator
    I used a scanner Once which took doubles
    And when i tried to use the Scanner again in later part of program to take another input, it gave me runtime inputmismatch error
    Are we supposed to make different references for different inputs?
    here is the code
    import java.util.*; /* we used the premade classes and sub classes*/
    public class spine {
    	public static void main(String[] args) {
    		System.out.println("enter the numbers followed with a String to end the cycle");
    		Scanner TAKER = new Scanner(System.in); //making object to acess Scanner clas
    		List<Double> TAKER2 = new ArrayList<Double>(); //makiubg object of List class = new sub class arraylist
    		try { //to catch input error
    		while (TAKER.hasNext()) {
    			TAKER2.add(TAKER.nextDouble()); //this is when i take the first input
    		}
    		}
    		catch (InputMismatchException e) //this is the error we are catching
    		{
    		}
    		//NOW WE TRANSFER THE NO FROM TAKER2 OBJECT TO AN REAL  ARRAY
    		Double array[] = new Double[TAKER2.size()]; //setting size of array
    		TAKER2.toArray(array);                     //transfering the nos
    		System.out.println("ok cool lets proceed");
    			System.out.println("enter 1 for addition, 2 - subtracton, 3 - multiplication, 4 - division.");
    	int choices = TAKER.nextInt();	//this is when i take the 2nd input and this is where it crashes
    	}
    	}

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Unable to use Same scanner twice

    You can't input String. You are only allowed to input value with datatype int or double.
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Unable to use Same scanner twice

    I tried to input Integers not strings.
    (int choices =TAKER.nextInt()

  4. #4
    Junior Member
    Join Date
    Apr 2019
    Posts
    25
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Unable to use Same scanner twice

    you can use String token = in.nextLine(); replace nextInt and nextDouble
    and then int value=Integer.parseInt(token) and double value= Double.parseDouble(token)

    Here Example:

    en.verejava.com/?id=20009865410847

  5. The Following User Says Thank You to veretimothy For This Useful Post:

    kukupie123 (May 5th, 2019)

  6. #5
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Unable to use Same scanner twice

    Quote Originally Posted by kukupie123 View Post
    I tried to input Integers not strings.
    (int choices =TAKER.nextInt()
    But I tested your code, it crashed once you enter String, no?

    Output

    run:
    enter the numbers followed with a String to end the cycle
    123
    123w
    ok cool lets proceed
    enter 1 for addition, 2 - subtracton, 3 - multiplication, 4 - division.
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at javaapplication3.Kreis.main(Kreis.java:33)
    /home/seng/.cache/netbeans/8.2/executor-snippets/run.xml:53: Java returned: 1
    BUILD FAILED (total time: 8 seconds)
    Whatever you are, be a good one

  7. #6
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Unable to use Same scanner twice

    Yes it crashes the 2nd time I try to use the scanner and when it crashes even when I input any integer just like it crashed when you input 4
    Can you mind explaining me why? I am new to Java and would like to have clear knowledge of basics

  8. #7
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Unable to use Same scanner twice

    Add TAKER.next(); after System.out.println("enter 1 for addition, 2 - subtracton, 3 - multiplication, 4 - division.");
    Last edited by John Joe; May 5th, 2019 at 10:34 AM.
    Whatever you are, be a good one

  9. The Following User Says Thank You to John Joe For This Useful Post:

    kukupie123 (May 5th, 2019)

  10. #8
    Junior Member
    Join Date
    Mar 2019
    Location
    India
    Posts
    18
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Unable to use Same scanner twice

    Quote Originally Posted by veretimothy View Post
    you can use String token = in.nextLine(); replace nextInt and nextDouble
    and then int value=Integer.parseInt(token) and double value= Double.parseDouble(token)

    Here Example:

    en.verejava.com/?id=20009865410847
    ty i will give this a try

    --- Update ---

    Quote Originally Posted by John Joe View Post
    Add TAKER.next(); after System.out.println("enter 1 for addition, 2 - subtracton, 3 - multiplication, 4 - division.");
    this actually solved it ty. Mind explaining me why it didnt work earlier
    but worked when we added
    TAKER.next() before adding the code to take input

  11. #9
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    268
    My Mood
    Amused
    Thanks
    8
    Thanked 18 Times in 18 Posts

    Default Re: Unable to use Same scanner twice

    The problem is with the TAKER.nextInt(); method as it only reads the int value. So when compiler continue reading with TAKER.next(),the java.util.Scanner.next() method will finds and returns the next complete token from scanner. A complete token is preceded and followed by input that matches the delimiter pattern.
    Whatever you are, be a good one

  12. #10
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Unable to use Same scanner twice

    Please don't do this:

    catch (InputMismatchException e) { //this is the error we are catching
    }

    As you are not catching anything. At a minimum, do this:

    catch (InputMismatchException e) {
        e.printStackTrace();
    }

    And don't use 'TAKER' but use 'taker.' You may want to read the Java Naming Conventions in my signature.

    Regards,
    Jim

Similar Threads

  1. Unable to compile
    By all_spark in forum Java Theory & Questions
    Replies: 1
    Last Post: February 9th, 2014, 01:35 PM
  2. Unable to authenticate!
    By aknessy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 24th, 2013, 01:22 PM
  3. Unable to view date
    By Rajiv in forum Web Frameworks
    Replies: 1
    Last Post: August 17th, 2011, 09:03 AM
  4. Unable to Drop the Text.
    By shimmer in forum AWT / Java Swing
    Replies: 1
    Last Post: July 4th, 2011, 09:13 AM
  5. Unable to insert records
    By javaprogrammer11 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: March 24th, 2010, 03:16 AM

Tags for this Thread