with Scanner
you need to check if there is a next line with hasNextLine()
so the loop becomes
while(sc.hasNextLine()){ str=sc.nextLine(); //... }
it’s readers that return null on EOF
ofcourse in this piece of code this is dependent on whether the input is properly formatted
Related Posts:
- What is an instance variable in Java?
- super() in Java
- What’s the syntax for mod in java
- What does the Java assert keyword do, and when should it be used?
- Exception in thread “main” java.util.NoSuchElementException
- How do I make a delay in Java?
- Casting variables in Java
- NoSuchElementException with Java.Util.Scanner
- max value of integer
- Rock, Paper, Scissors Game Java
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- What is the difference between == and equals() in Java?
- What does “?” mean in Java?
- Why am I getting InputMismatchException?
- Java – No enclosing instance of type Foo is accessible
- Why I am getting java.lang.AbstractMethodError errors?
- fix java.net.SocketTimeoutException: Read timed out
- Scanner vs. BufferedReader
- Validating input using java.util.Scanner
- How to override equals method in Java
- What does Scanner input = new Scanner(System.in) actually mean?
- java.text.ParseException: Unparseable date
- What’s the difference between next() and nextLine() methods from Scanner class?
- java.util.NoSuchElementException: No line found
- How do I use a delimiter with Scanner.useDelimiter in Java?
- Java – String cannot be converted to int
- java.io.IOException: Broken pipe
- Even though JRE 8 is installed on my MAC -” No Java Runtime present,requesting to install ” gets displayed in terminal
- Reason for the exception java.lang.VerifyError: Bad type on operand stack
- The specified DSN contains an architecture mismatch between the Driver and Application. JAVA
- Downcasting in Java
- Java “params” in method signature?
- How do I time a method’s execution in Java?
- What does Scanner input = new Scanner(System.in) actually mean?
- java.util.NoSuchElementException – Scanner reading user input
- repaint() Method in Java
- Exception in thread “main” java.lang.reflect.InvocationTargetException
- How to set String’s font size, style in Java using the Font class?
- How to use the command update-alternatives –config java
- java array error “array required but int found”
- this: Cannot use this in static context
- What’s the difference between SoftReference and WeakReference in Java?
- How to draw lines in Java
- NoClassDefFoundError in Java: com/google/common/base/Function
- Is there an equivalent to the Scanner class in C# for strings?
- Reading a .txt file using Scanner class in Java
- Using a do-while loop to check a User’s input in Java
- Multiple delimiters in Scanner class of Java
- “Cannot be resolved to a type” when attempting to use Scanner
- In Java, what is a shallow copy?
- Good way to encapsulate Integer.parseInt()
- Eclipse IDE – Error: Build path specifies execution environment Java SE 1.7
- Java: Not a statement
- ArrayList of int array in java
- How to get the filename without the extension in Java?
- Read next word in java
- What does hasNext() in a While do?
- How can I upgrade to Java 1.8 on an Amazon Linux Server?
- Sort an array in Java
- How to solve could not create the virtual machine error of Java Virtual Machine Launcher?
- Java String Split by “|”
- Could not reserve enough space for object heap
- STDERR? What is it? What are its common uses?
- Possible causes of Java VM EXCEPTION_ACCESS_VIOLATION?
- Handling file renames in Git
- Print ArrayList
- Unsupported major.minor version 52.0 [duplicate]
- What is a StringIndexOutOfBoundsException? How can I fix it?
- Exception in Application start method java.lang.reflect.InvocationTargetException
- Non-static variable cannot be referenced from a static context
- Python Key Error=0 – Can’t find Dict error in code
- java.lang.ClassCastException
- org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘MyController’:
- Can I multiply strings in Java to repeat sequences?
- Java ArrayList replace at specific index
- Exception in thread “main” java.lang.OutOfMemoryError: Java heap space
- What is the best math library to use with java?
- What’s up with Java’s “%n” in printf?
- cannot make a static reference to the non-static field
- Double cannot be dereferenced?
- Encrypt Password in Configuration Files?
- Cassandra “no viable alternative at input”
- Java, Simplified check if int array contains int
- Converting a string to an integer on Android
- What is method hiding in Java? Even the JavaDoc explanation is confusing
- Error: class X is public should be declared in a file named X.java
- How to determine why a Greasemonkey script is not running [duplicate]
- Parsing JSON string in Java
- java attempting to assign weaker access privilege error
- What is stdole2.tlb
- Getting a “no interface expected here” when I try to subclass ServiceConnection [duplicate]
- How to determine day of week by passing specific date?
- set background color: Android
- In the following SAS statement, what do the parameters “noobs” and “label” stand for?
- Uploaded successfully to Heroku Local but getting 503 error once pushed to Heroku live
- Class JavaLaunchHelper is implemented in two places
- Access Default Widget's Controls in Customizer Pane
- Using Javascript to Set Redirect Page in Contact Form 7
- Change css for certain user roles
- Block any other domains from using my server