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:
- Exception in thread “main” java.util.NoSuchElementException
- NoSuchElementException with Java.Util.Scanner
- Rock, Paper, Scissors Game Java
- Why am I getting InputMismatchException?
- Scanner vs. BufferedReader
- What does Scanner input = new Scanner(System.in) actually mean?
- What’s the difference between next() and nextLine() methods from Scanner class?
- How do I use a delimiter with Scanner.useDelimiter in Java?
- What does Scanner input = new Scanner(System.in) actually mean?
- java.util.NoSuchElementException – Scanner reading user input
- 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
- Read next word in java
- What does hasNext() in a While do?
- javac is not recognized as an internal or external command, operable program or batch file [closed]
- What is a StackOverflowError?
- How to uninstall Eclipse?
- How do I determine whether an array contains a particular value in Java?
- How can I solve Exception in thread “main” java.lang.NullPointerException error [duplicate]
- Why am I getting a NoClassDefFoundError in Java?
- How to avoid “ConcurrentModificationException” while removing elements from `ArrayList` while iterating it? [duplicate]
- Using Pairs or 2-tuples in Java [duplicate]
- Is there a way to convert all comments into javadoc comments? [Eclipse/Java]
- Reverse a string in Java
- Bad Operand Types for Binary Operator “>”?
- Error occurred during initialization of boot layer FindException: Module not found
- Non-static variable cannot be referenced from a static context
- How to use multiple classes in java in one file?
- Java: Multiple class declarations in one file
- Why do JVM arguments start with “-D”?
- How to split a string in Java
- Printing out a linked list using toString
- Received fatal alert: handshake_failure through SSLHandshakeException
- Best way to Format a Double value to 2 Decimal places
- Collision resolution in Java HashMap
- “Content is not allowed in prolog” when parsing perfectly valid XML on GAE
- How can I concatenate two arrays in Java?
- Difference between spring @Controller and @RestController annotation
- Sort a single String in Java
- Difference between slf4j-log4j12 and log4j-over-slf4j
- Java, “Variable name” cannot be resolved to a variable
- What is Java String interning?
- UnsatisfiedDependencyException: Error creating bean with name
- Random shuffling of an array
- How to convert int[] to Integer[] in Java?
- Why am I getting a “; expected” error?
- How/where to download openjdk/openjre for windows [duplicate]
- copy a 2d array in java
- Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException
- Generating all permutations of a given string
- imageio.IIOException: Can’t read input file
- Difference between OpenJDK and Adoptium/AdoptOpenJDK
- Compile failed; see the compiler error output for details
- Java: Rotating Images
- Invalid character constant in java
- How to pass a function as a parameter in Java?
- ResultSet exception – before start of result set
- java sort using anonymous class
- How to fix the Hibernate “object references an unsaved transient instance – save the transient instance before flushing” error
- channel 3: open failed: connect failed: Connection refused
- Java Currency Number format
- How to create a generic array in Java?
- Print a boolean value with printf
- Meaning of @Resource annotation
- How to solve error lossy convertion from int to char?
- What does “error: ‘.class’ expected” mean and how do I fix it
- Printing the stack values in Java
- Import a custom class in Java
- How do I get the size of a java.sql.ResultSet?
- Java: How to set Precision for double value?
- Any way to declare an array in-line?
- java.io.StreamCorruptedException: invalid stream header: 54657374
- How to cast ArrayList<> from List<>
- Best way to “negate” an instanceof
- How to send HTTP request in java?
- How to take input as String with spaces in java using scanner
- what is the difference between doGet() and doPost() in term of the flow?
- Java 3D Game Engine?
- How to cast ArrayList<> from List<>
- Using insertion sort for descending order?
- What is the home directory for JDK?
- java.lang.ClassNotFoundException: org.postgresql.Driver, Android
- Java – Change int to ascii
- Adding JPanel to JFrame
- Javadoc @author tag good practices
- A fatal error has been detected by the Java Runtime Environment: SIGSEGV, libjvm
- java.lang.IllegalStateException: Failed to introspect Class
- ShoppingCart.Java Program Assignment
- White spaces are required between publicId and systemId
- Http status 401 This request requires HTTP authentication (). in tomcat 6
- Get integer value of the current year in Java
- How to make the division of 2 ints produce a float instead of another int?
- Java Console Prompt for ENTER input before moving on [duplicate]
- Can’t load IA 32-bit .dll on a AMD 64-bit platform
- Android Studio 2.2.3: Gradle project sync failed. Basic functionality (e.g. editting, debugging) will not work properly
- How to draw a circle with given X and Y coordinates as the middle spot of the circle?
- In Java, how do I parse XML as a String instead of a file?
- Can’t seem to disable Java Automatic Update