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]
- 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?
- Java String Split by “|”
- Convert String to double in Java
- Exception in thread “main” java.util.NoSuchElementException
- Java String new line
- Java string to date conversion
- What are all the escape characters?
- JavaFX – Exception in Application start method?
- 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
- EOFException – how to handle?
- 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?
- 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?
- 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()
- 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
- “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?
- 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?
- 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?
- 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?
- java.lang.NumberFormatException: empty String
- 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: how to initialize String[]?
- Regular Expressions on Punctuation
- How to extract a substring using regex
- difference between equals() and hashCode()
- How can I check if a single character appears in a string?
- Java String to SHA1
- Replace a character at a specific index in a string?
- Determine if a String is an Integer in Java [duplicate]
- How to convert a String to CharSequence?
- Connection Java – MySQL : Public Key Retrieval is not allowed
- How do I convert a String to a BigInteger?
- How can I remove a substring from a given String?
- Java get String CompareTo as a comparator object
- How to use regex in String.contains() method in Java
- How to swap String characters in Java?
- Get an OutputStream into a String
- Difference between string object and string literal
- How to convert a String into an ArrayList?
- Throwing multiple exceptions in Java
- java: use StringBuilder to insert at the beginning
- How do I get the last character of a string?
- Cannot invoke toString() on the primitive type int
- How to compare character ignoring case in primitive types
- Official reasons for “Software caused connection abort: socket write error”
- convert string to arraylist
in java - How can I read a text file in Android?
- Append a single character to a string or char array in java?
- String is immutable. What exactly is the meaning?
- Occurrences of substring in a string
- Quotation marks inside a string
- Exception is never thrown in body of corresponding try statement
- How can I catch all the exceptions that will be thrown through reading and writing a file?
- Fastest way to put contents of Set
to a single String with words separated by a whitespace? - How to know if a given string is substring from another string in Java