-2

Trying to do some Command Line Arguments for a program. Next int is clearly present but Scanner throws NoSuchElement exception. With or without new line/whitespace skip, trim() on string before it, etc.

try {
    String s = String.valueOf(scan.next().charAt(0));
    String t = String.valueOf(scan.next().charAt(0));
    if ((s+t).trim() != "-s") {
        throw new IOException ("Size must be allocated with “-s”.");
    }
    System.out.println("Got ''-s'' successfully.");
    } catch (IOException SizeFormatIncorrect) {
}
try {
    //scan.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); - with or
    //without, gives same problem
    size = scan.nextInt();
    System.out.println("Got " + size);
} catch (NumberFormatException NotInt) 
{ }



Input:
-s 200 or -s 2
OR
-s\n200 or -s\n2

Output:
Enter variables to test call:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
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 HelloWorld.main(HelloWorld.java:28)

//(Line 28 is size = scan.nextInt())
//If I add 
if (!scan.hasNextInt()){
        System.out.println("We have :" + scan.nextLine().getBytes());
 }

I get output: We have :[B@42a57993

So whoever wants to tell me and everyone else reading how to cast whatever it is Scanner thinks it's reading in to an Int. (Integer.parseInt() wouldn't work)

VeggieMan
  • 1
  • 2
  • And yes, I properly allocated scan with Scanner scan = new Scanner(System.in) – VeggieMan Jul 20 '19 at 20:25
  • Possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – George Z. Jul 20 '19 at 20:29
  • No it isn't, I didn't close the Scanner stream before this point. This is all running inside Main. I only provided the select few lines since that's the only problem, the strings seem to read in fine. – VeggieMan Jul 20 '19 at 20:36
  • To get better help take your time and create proper [MCVE] (a.k.a. [SSCCE](http://sscce.org)). Explain what you expect it to do. Very often while creating such example people find cause of their problems so it is definitely worth your time (either *you* will find issue or will get code which others could use to help you). In case of `Scanner` you can simulate finite input by providing it directly as string in Scanner's constructor like `new Scanner("foo 123\nbar 321");`. – Pshemo Jul 20 '19 at 21:22
  • Why are you using `.getBytes()` at `System.out.println("We have :" + scan.nextLine().getBytes());`? This method returns byte array which represents *encoded* string returned by `nextLine()` like `[65,54,86,...]` but java doesn't simply print content of array but `TypeInfo@hashcode` of array. If you want to print its content see [What's the simplest way to print a Java array?](https://stackoverflow.com/q/409784). – Pshemo Jul 20 '19 at 21:34
  • Because I am TRYING to get the DATA TYPE of whatever it thinks it's returning, and then CAST. IT. This is a debug print statement. Maybe you should tell me what that is, or how to find it via a secondary cast, so I can be done here. When I ran a while loop on every character before white space, then trim(), then cast to int, it gave me a 0. Maybe if nobody can answer the question, it implies it's a GOOD QUESTION and should be upvoted? – VeggieMan Jul 20 '19 at 21:36
  • "Maybe if nobody can answer the question, it implies it's a GOOD QUESTION and should be upvoted?" no, it may also imply that people can't understand or reproduce your problem, possibly because of limited information provided (that is why we *require* form questions about debugging problems to provide [mcve]). Anyway class which can tell you what type of data string (or its token) can be parsed into is precisely `Scanner` and its `hasNext*TYPE_NAME*` methods like `hasNextInt()`. If such method returns `false` then there is no data which matches that type. – Pshemo Jul 20 '19 at 21:49
  • This is the minimum reproducible example, it's easier to look at then all my imports and var declarations above as there is nothing wrong with them. It returns type String. Integer.parseInt() won't process such even after trim(). It has an int, it is not reading it as such, and it is not casting, which is an issue. The line size = Integer.parseInt(scan.next().trim()); returns with same exception. – VeggieMan Jul 20 '19 at 21:55

1 Answers1

0

I properly allocated scan with Scanner scan = new Scanner(System.in)

Then Scanner reads from System.in (standard input). Not the command line. You could concatenate your arguments into a String and then use that as a source for your Scanner. Like,

Scanner scan = new Scanner(String.join(" ", args));

You will also need to change (at least)

(s+t).trim() != "-s"

because that is not a good way to compare String(s). You need something like

!(s+t).trim().equals("-s")
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I'm not running it from Command Line, I'm testing it via https://www.tutorialspoint.com/compile_java_online.php with the given input. It seems to either read in "-s" fine or skip it until it reads an int. When I changed my constructor to what you have, errors start at Line 18, where String s is allocated, meaning it can't find the first char now. – VeggieMan Jul 20 '19 at 20:31
  • Why are you "not running it from Command Line" if you are "Trying to do some Command Line Arguments for a program"? Please try running from the command line. – Elliott Frisch Jul 21 '19 at 01:07
  • This is me testing the algorithm which has clearly failed. That's the difference between the IDE and the Command Line. – VeggieMan Jul 21 '19 at 02:00