-5

I am currently doing Problem 110106 from the programming challenges website. I am getting NoSuchElementException on the second 000 in the file below.

Below that is my code any help would be great.

1

299
492
495
399
492
495
399
283
279
689
078
100
000
000
000

Here's example:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class Problem110106 {

    int mainCounter = 0;
    int array[] = new int[10];

    public static void main(String[] args) throws FileNotFoundException, NoSuchElementException {
        // TODO Auto-generated method stub

        Problem110106 problem = new Problem110106();
        problem.start();
    }

    public void start() throws FileNotFoundException, NoSuchElementException {
        Scanner input = new Scanner(new BufferedReader(new FileReader("text.txt")));
        int numTestCases = Integer.parseInt(input.nextLine().trim());
        System.out.println(numTestCases);
        String blank = input.nextLine();
        System.out.println(blank);

        while (input.hasNext()) {

            int num = Integer.parseInt(input.next());
            if (input.next() == "") {

            } else {

                int first = num / 100;
                int second = (num / 10) % 10;
                int third = (num % 10);

                System.out.println(first);
                System.out.println(second);
                System.out.println(third);

                switch (first) {
                case 0:
                    break;
                case 1:
                    break;
                case 2:
                    set(first, second, third);
                    break;
                case 3:
                    add(first, second, third);
                    break;
                case 4:
                    multiply(first, second, third);
                    break;
                case 5:
                    set(first, second, third);
                    break;
                case 6:
                    add(first, second, third);
                    break;
                case 7:
                    multiply(first, second, third);
                    break;
                case 8:
                    set2(first, second, third);
                    break;
                }
            }
        }
    }

    public void set(int first, int second, int third) {
        array[second] = third;
        mainCounter++;
        // System.out.println("got here");
    }

    public void add(int first, int second, int third) {
        array[second] = array[second] + third;
        mainCounter++;
    }

    public void multiply(int first, int second, int third) {
        array[second] = array[second] * third;
        mainCounter++;
    }

    public void set2(int first, int second, int third) {
        // array[second] =
    }

    public void goTo() {
    }

}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
user2175076
  • 57
  • 3
  • 10

2 Answers2

0
while (input.hasNextInt()) {
    int num = Integer.parseInt(input.next());   
    if (!input.hasNext() || input.next().isEmpty()){
        break; // Jump out of the loop.
    } else {

Strings are normally compared as:

    input.next().equals("")
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

I think you problem may be here

while (input.hasNext()) {

      int num = Integer.parseInt(input.next());
      if (input.next() == "") {

The second line reads an input, but so does the next line. So when the loop is at the last element, the first line will read it, then the next line will try to read a next element that doesn't exist, hence NoSuchElementException.

And don't compare strings with ==. Use equals() or equalsIgnoreCase()

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720