-1

I'm having issues utilizing the scanner for int input. In the getInputs method, if I do not inlcude the if (in.hasNextInt()) ... I get error:

 Exception in thread "main" java.util.NoSuchElementException
 at java.util.Scanner.throwFor(Scanner.java:907)
 at java.util.Scanner.next(Scanner.java:1530)
 at java.util.Scanner.nextInt(Scanner.java:2160)
 at java.util.Scanner.nextInt(Scanner.java:2119)
 at Solution.getInputs(Solution.java:30)
 at Solution.main(Solution.java:7)   

However, with the line included I get stuck in an infinite loop since despite however many times I enter a number into the console it never evaluates that there exists an int so int n never changes value. I've tried searching and everyone says to use if (in.hasNextInt()) but cannot find a solution for breaking the infinite loop. I'm lost and can't figure it out. I've tried conditioninig

while(in.hasNextInt()) {
  n = in.nextInt();
  if (n > 0) break;
}

but this just skips over the loop. Honestly I'm lost.

 import java.util.*;

 public class Solution{

    public static void main(String[] args) {
        int t = getTestCases();
        int[] inputs = getInputs(t);
        int[] solutions = getSolutions(inputs);
        for (int i = 0; i < solutions.length; i++) {
            System.out.println(solutions[i]);
        }
    }

    static int getTestCases() {
        int t = getNumber();
        while(t <= 0 && t > 15) {
            t = getNumber();
        }
        System.out.println("t = " + t);
        return t;
    }

    static int[] getInputs(int t) {
        int[] inputs = new int[t];
        for (int i = 0; i < t; i++) {
            int n = getNumber();
            while (n <= 0) {
                n = getNumber();
            }
            inputs[i] = n;
        }
        return inputs;
    }

    static int[] getSolutions(int[] inputs) {
        int[] solutions = new int[inputs.length];
        for (int i = 0; i < solutions.length; i++){
            solutions[i] = findDigits(inputs[i]);
        }
        return solutions;
    }

    static int findDigits(int n) {
        int total = 0;
        String str = "" + n;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            int d = Character.getNumericValue(ch);
            if (d != 0) {
                if (n % d == 0)  total++;
            }
        }
        return total;
    }

    static int getNumber() {
        Scanner in = new Scanner(System.in);
        int n = -1;
        if(in.hasNextInt()) n = in.nextInt();
        in.close();
        return n;
    }
}
chonchon
  • 11
  • 2
  • 2
    possible duplicate of [Java: Infinite loop using Scanner in.hasNextInt()](http://stackoverflow.com/questions/1794281/java-infinite-loop-using-scanner-in-hasnextint) – Paco Abato Dec 18 '14 at 07:21

1 Answers1

1

Why are you creating and closing Scanners each time through the loop?

Create one Scanner at the start of the method, and close it just before you return. That should sort you out.

static int[] getInputs(int t) {
    Scanner in = new Scanner(System.in);
    int[] inputs = new int[t];
    for (int i = 0; i < t; i++) {
        int n = 0;
        while (n <= 0) {
            if (in.hasNextInt()) n = in.nextInt();
        }
        inputs[i] = n;
    }
    in.close();
    return inputs;
}
Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38
  • I did that as random trial and error trying to get it to work. But removing the scanner from the loop doesn't change the infinite loop. To make sure I tried it again but to no avail. – chonchon Dec 18 '14 at 08:17
  • Ah, of course. The problem is in your while loop. If `in.hasNextInt()` ever becomes false, this becomes an infinite loop. (can you see why?) I would suggest you make this a little simpler: write a method that prompts for input until an int is entered, and returns that int. Call that once for each slot you need to fill. – Jon Kiparsky Dec 18 '14 at 08:30
  • I went ahead and changed the method call so every time an number is neeed it calls a method with a new scanner. This does not solve it : /It works for the first instance (for int t= getnumber()) but not for n. I updated the code in the original post to reflect the change. Essentially the getnumber method inside the while call is constantly returning -1. – chonchon Dec 18 '14 at 08:51