0

So I created some code to check if the first letter of the word that the user enters (stored in the variable word) is a consonant or vowel. If it is neither it outputs saying it is neither. However, I am using nextLine() instead of next() to get input. I understand that next() will not take input until they enter a valid character besides a space, and I know that nextLine() will go to the else statement if they enter just spaces. However, in nextLine when the user just puts enter and does not enter any character, no spaces, the program crashes. I tried checking if the string was equal to null and then making it print out "test" if it was proven true, however for some reason whenever I press enter I still get an error. Below is my code:

 import java.util.Scanner;
public class WordStart {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.printf("Please enter a word: ");
        String word = in.nextLine();
        String lowerWord = word.toLowerCase();
        String x = lowerWord.substring(0,1);
        String test = null;
        String empty = new String();


        boolean vowel = x.equals("a")||x.equals("e")||x.equals("i")||
                        x.equals("o")||x.equals("u");
        boolean conc = x.equals("b")||x.equals("c")||x.equals("d")||x.equals("f")||
                        x.equals("g")||x.equals("h")||x.equals("j")||x.equals("k")||
                        x.equals("l")||x.equals("m")||x.equals("n")||x.equals("p")||
                        x.equals("q")||x.equals("r")||x.equals("s")||x.equals("t")||
                        x.equals("v")||x.equals("w")||x.equals("x")||x.equals("y")||
                        x.equals("z");


        if(vowel){
            System.out.printf("%s starts with a vowel.\n", word);
        }
        else if(conc){
            System.out.printf("%s starts with a consonant.\n", word);
        }
        else if(word.equals("")){
            System.out.println("testEmpty");
        }
        else if(word.isEmpty()){
            System.out.println("testNull");
        }
        else{
            System.out.printf("%s starts with neither a vowel nor a consonant.\n", word);
        }

    }

}

Basically, I am trying to check if the user just pressed enter without entering anything and call them out on it. What method, line of code would help me do that. I tried using word.equals(null), but the IDE said that it was never true. Thanks in advance.

The error code I get when I just press enter is as follows

run:
Please enter a word: 
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.substring(String.java:1963)
    at WordStart.main(WordStart.java:8)
C:\Users\Jordan\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 3 seconds)
6
  • use hasNext() , will tell you if anything is there in input stream or not Commented Sep 23, 2016 at 15:34
  • 2
    I bet you get IndexOutOfBoundsException - firstly you should think/read about it, secondly you should include this information in the question. Commented Sep 23, 2016 at 15:34
  • Btw y is a vowel. Commented Sep 23, 2016 at 15:36
  • @JaroslawPawlak thanks for the suggestion it is added and yes that is the error I get. I've been doing research and I have found stuff that said should work (which is what I put in my code) and also tried other things yet I keep getting the same error. Thanks for the heads up. Commented Sep 23, 2016 at 15:39
  • @JaroslawPawlak is definitely right, it's not a problem caused by null String (actually, pressing enter returns "\n" as a result) but the substring which throws StringIndexOutOfBoundsException Commented Sep 23, 2016 at 15:39

3 Answers 3

1

I think the problem lies with this line:

String x = lowerWord.substring(0,1);

If the string is empty (nextLine will never return a null string) it is impossible to take a substring. You should probably check that the string is > 0 characters long.

if(x.length > 0)
  String x = lowerWord.substring(0,1);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a bunch for the help. It is funny that I had previously done the correct thing, I just wasn't aware that the substring was the thing giving me the error and not that the string was null! Thanks.
1

Firstly note that word.equals("") and word.isEmpty() checks the same condition. So there is no need to use them both. To check whether is String null use if (word == null). Checking the null and the emptyness of the String should be the first one you do.

Secondly in case you skip the input (get the empty String ""), you get IndexOutOfBoundsException, because lowerWord.substring(0,1); has no chance to find that index.

So:

if (word != null && !word.isEmpty()) {
   // ...
}

Comments

0

Just check if it is null

if(variable==null)

should suffice

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.