0

I am having a problems when submiting my answer on https://www.thehuxley.com. When I run my code on Eclipse, everything goes OK, but on Huxley, I get this:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at HuxleyCode.main(HuxleyCode.java:12)

And here is the code itself:

import java.io.*;
import java.util.*;

public class HuxleyCode {
  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int menor = 0, pos = 0, entradas = 0, temp;

    entradas = in.nextInt();// WATCH OUT THIS LINE
    in = new Scanner(System.in);

    String valores = in.nextLine();

    entradas = 0;
    for (String val : valores.split(" ")) {
        temp = Integer.valueOf(val);

        if (entradas == 0) {
            menor = temp;
        } else if (temp < menor) {
            menor = temp;
            pos = entradas;
        }
        entradas++;
    }

    in.close();
    System.out.println("Menor valor: " + menor);
    System.out.println("Posicao: " + pos);
  }
}

Just to complement, in the line that i commented "WATCH OUT THIS LINE", if I remove that line, Scanner ignored nextInt() e jumps to NextLine(), causing this error:

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.valueOf(Integer.java:766)
at HuxleyCode.main(HuxleyCode.java:16)

Where is my mistake, why does not work on Huxley?

The expected input is:

10
1 2 3 4 -5 6 7 8 9 10

And output:

Menor valor: -5
Posicao: 4
Madson Paulo
  • 57
  • 1
  • 9
  • `in = new Scanner(System.in);` You don't need to re-initialize the Scanner everytime. You are probably losing the inputLine in the previous instance of the Scanner. – kaza Sep 11 '17 at 23:39
  • When you run this interactively(slow), the buffer is not loaded with all input. But in Huxley probably all the lines are fed at once into `System.in`, thus loading up the first instance of `Scanner` with all the input lines. – kaza Sep 11 '17 at 23:42
  • So, how should I fix this? Creating two Scanners does not seens to solve the problem :/ – Madson Paulo Sep 11 '17 at 23:47
  • Just remove this line `in = new Scanner(System.in);` – kaza Sep 11 '17 at 23:47
  • Like I said, removing that re-initialization makes the Scanner skip the in.nextInt() and go read the in.nextLine(), causing the error : Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:592) at java.lang.Integer.valueOf(Integer.java:766) at HuxleyCode.main(HuxleyCode.java:16) – Madson Paulo Sep 11 '17 at 23:52

1 Answers1

1
entradas = in.nextInt();
String valores ="";
while(in.hasNextLine())
    valores = in.nextLine();

Issue is that nextInt() does not set the position of the Scanner to beginning of next line, thus the first call will return the empty string. This is explained clearly here...
Can't use Scanner.nextInt() and Scanner.nextLine() together

One thing to note is that it works for you because, you reinitialzed the Scanner thus forcing it start from beginning of next line. But unfortunately this would not work with Huxley as they send input programmatically all at once and all lost by losing reference to the first Scanner.

Also the below should work

entradas = in.nextInt();
String valores = in.nextLine();//Get empty Str & Set pos of Scanner to beginning of next line
valores = in.nextLine();
kaza
  • 2,317
  • 1
  • 16
  • 25