1

I'm whipping together a simple Java program to explain loops. I want each demonstration in a separate function. Right now, each function works fine, but only when the other isn't called. If I call both I get the following errors at run-time:

Please input a positive integer as the end value: 5
The summation is: 9
How many rows do you want your triangle to be?: Exception in thread "main" java.util.NoSuchElementException
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 loops.exercise2(loops.java:48)
at loops.main(loops.java:11)

Code:

import java.util.Scanner;

public class loops
{

public static void main( String args[] )
{
    exercise1();
    System.out.println();
    exercise2();
}

public static void exercise1()
{

    int limit;
    int i;
    int sum;

    Scanner keyboard = new Scanner(System.in);


    System.out.print ("Please input a positive integer as the end value: ");

    limit = keyboard.nextInt();

    i=1;
    sum = 0;

    while (i <= limit)
    {
        sum = sum + i;
        i = i + 2;          
    }

    System.out.print("The summation is: " + sum);

    keyboard.close();
}

public static void exercise2()
{
    int numRows, i, j;
    Scanner keyboard = new Scanner(System.in);

    System.out.print("How many rows do you want your triangle to be?: ");
    numRows = keyboard.nextInt();

    for(i=0; i<numRows; i++)
    {
        for(j=0; j<=i; j++)
        {
            System.out.print("*");              
        }
        System.out.println();
    }

    keyboard.close();
}

}

2
  • Tip: next time, use a shorter title! =) Jul 29, 2013 at 16:57
  • 1
    it's because both methods are waiting for the Scanner input (which is your console input) and appearantly intervene with each other
    – bas
    Jul 29, 2013 at 16:58

4 Answers 4

3

This is happening because when you close your Scanner, it also closes the input stream, which in this case is System.in. When you try to instantiate the Scanner in your execise2 method, the input stream is closed.

See this SO post...

https://stackoverflow.com/a/13042296/1246574

2
  • Thanks. This fixed the problem, but it doesn't create a new question: When would anyone want to close a Scanner? If the garbage collector makes sure there are no memory leaks, and closing the scanner means you can't use System.in anymore, when is closing it a good idea? Jul 29, 2013 at 19:58
  • You can use a Scanner with other types of input streams besides System.In, and in many cases it is desirable to close the underlying input stream when you're done with the Scanner. There's some examples at this link... docs.oracle.com/javase/tutorial/essential/io/scanning.html
    – Jim
    Jul 29, 2013 at 20:00
1

My guess is your Scanner classes are interfering with each other. exercise1 takes input from standard in then closes when it's done. Then exercise2 also tries to get input from standard in which is closed.

I would suggest you only make 1 Scanner and pass it as a parameter to both exercise1 and exercise2 and then close it after both calls.

1

Make a global scanner, and initiate it only once, then call its methods.

1

Try not calling keyboard.close();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.