The problem occurs when line
is empty (e.g. ""
). Then it doesn’t have a character at index 0, hence your error.
To fix, you can check the length of line
before you use charAt
:
System.out.println(line); char x; if (line.length() < 1) { System.out.println("next line is empty"); } else { x = line.charAt(0); while((line.charAt(0)!='/')&&(Character.isWhitespace(x)==false)) { line = inputFile.nextLine(); if (line.length() < 1) { System.out.println("next line is empty"); break; } x = line.charAt(0); System.out.println(line); System.out.println(x); } }
Related Posts:
- How do I convert a String to an int in Java?
- How to format strings in Java
- Java – Convert integer to string [duplicate]
- What is a StackOverflowError?
- How to split a string in Java
- How do I compare strings in Java?
- How to convert a char to a String?
- What is the proper way to handle a NumberFormatException when it is expected?
- How to convert/parse from String to char in java?
- Reverse a string in Java
- Reverse a string in Java
- Java String Split by “|”
- Convert String to double in Java
- Exception in thread “main” java.util.NoSuchElementException
- Java String new line
- How to check if a String contains another String in a case insensitive manner in Java?
- Java split string to array [duplicate]
- String concatenation: concat() vs “+” operator
- How to remove the last character from a string?
- Java string to date conversion
- How to replace � in a string
- Change date format in a Java string
- What are all the escape characters?
- Converting String to “Character” array in Java
- JavaFX – Exception in Application start method?
- Best way to convert an ArrayList to a string
- JavaFX – Exception in Application start method? [duplicate]
- Best way to convert an ArrayList to a string
- How to split a String by space
- String interpolation in Java 14 or 15
- Given final block not properly padded
- What is an AssertionError? In which case should I throw it from my own code?
- EOFException – how to handle?
- How can I prevent java.lang.NumberFormatException: For input string: “N/A”?
- How to remove single character from a String
- Can I catch multiple Java exceptions in the same catch clause?
- How to parse this string in Java?
- How to convert any Object to String?
- How to split a string in Java
- Exception in thread “main” java.util.NoSuchElementException: No line found
- “NoClassDefFoundError: Could not initialize class” error
- Illegal Escape Character “\”
- Can I multiply strings in Java to repeat sequences?
- What does regular expression \\s*,\\s* do?
- unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
- How to convert a Binary String to a base 10 integer in Java
- Left Hand Side of an Assignment must be a Variable CharAt
- How to capitalize the first letter of a String in Java?
- catDog string problem at Codingbat.com
- Java way to check if a string is palindrome
- Return string Input with parse.string
- What is the difference between String.subString() and String.subSequence()
- How do I apply the for-each loop to every character in a String?
- Java – removing first character of a string
- Exception in thread “main” java.lang.UnsupportedClassVersionError: a (Unsupported major.minor version 51.0)
- Java compressing Strings
- Explain the use of a bit vector for determining if all characters are unique
- Explain the use of a bit vector for determining if all characters are unique
- “NoClassDefFoundError: Could not initialize class” error
- “NoClassDefFoundError: Could not initialize class” error
- Convert array of strings into a string in Java
- Why do I get an UnsupportedOperationException when trying to remove an element from a List?
- Sort a single String in Java
- Why do I get “Exception; must be caught or declared to be thrown” when I try to compile my Java code?
- How to remove single character from a String
- How to remove single character from a String
- What is Java String interning?
- What is Java String interning?
- What is Java String interning?
- Why is there no String.Empty in Java?
- Java split string to array
- Using Enum values as String literals
- What is the easiest/best/most correct way to iterate through the characters of a string in Java?
- What is difference between Errors and Exceptions?
- Causes of getting a java.lang.VerifyError
- How to create a custom exception type in Java?
- int cannot be converted to string?
- How to check if my string is equal to null?
- what is Ljava.lang.String;@
- Convert String to int array in java
- Converting String Array to an Integer Array
- How to sort an array of objects in Java?
- How to sort an array of objects in Java?
- How to compress a String in Java?
- java.lang.NumberFormatException: empty String
- How to check if a String is numeric in Java
- How to replace a substring of a string
- Converting A String To Hexadecimal In Java
- Exception in thread “main” java.lang.ArithmeticException: / by zero
- Immutable class?
- Java can’t find file when running through Eclipse
- Converting a string to an integer on Android
- FXML Load exception
- Simple way to repeat a string
- Java end of file
- Unclosed Character Literal error
- Java: how to initialize String[]?
- Regular Expressions on Punctuation
- Add String Array to ArrayList
- How to extract a substring using regex