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?
- What does Java option -Xmx stand for? [duplicate]
- How do I convert a String to an int in Java?
- What does Java option -Xmx stand for? [duplicate]
- Using prevNext Modx Addon
- Is there an invisible character that is not regarded as whitespace?
- Problem with gif with transparent background
- :wq! command in vim
- how to set the background color of the status bar during the launching phase [duplicate]
- Finding white rectangle in an image
- api-ms-win-crt-runtime-l1-1-0.dll is missing when opening Microsoft Office file [closed]
- 1000 * 60 * 60 * 24 * 30 results in a negative number [duplicate]
- How to convert nanoseconds to seconds using the TimeUnit enum?
- When is K 1024 and when is it 1000?
- Search for words with telephone numbers from 2-3-4 tree
- What is a MIME type?
- Logitech/LGHUB Lua – Loop with break
- are there dictionaries in javascript like python?
- How can I convert MP3 file to a Base64 encoded string? [closed]
- Understanding The Modulus Operator %
- Understanding the main method of python [duplicate]
- Dial pad to get phone number (with Android button images)
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- How is the AND/OR operator represented as in Regular Expressions?
- TypeError: only integer scalar arrays can be converted to a scalar index with 1D numpy indices array
- Istio Ingress resulting in “no healthy upstream”
- Using or ‘|’ in regex [duplicate]
- How to format strings in Java
- Why is it not possible to fake an IP address?
- How to open a “-” dashed filename using terminal?
- What is the difference between x86 and x64
- && (AND) and || (OR) in IF statements
- Substring in excel
- How to use the toString method in Java?
- What is a LAMP stack?
- What is a NullPointerException, and how do I fix it?
- What is a sanity test/check
- What is the difference between POST and PUT in HTTP?
- What exactly is Apache Camel?
- What is the difference between POST and PUT in HTTP?
- Unable to find valid certification path to requested target – error even after cert imported
- How to find Google’s IP address?
- What exactly is a VBO in OpenGL?