jzaunegger
jzaunegger

Reputation: 11

Seeing if a index is negaitve

I am writing a program for my structured programming class and we have to take a input string, break it into 3 substrings and then check to see if all three names are there. I have the string broken up at the spaces and now I just need to check that there are three spaces. My professor told us that we had to make sure that there is a space between each name. He told me to simply test if the index of the space character is -1, because if it is the space wont be there. I just cant find a way to test it without getting a "string index out of range" error. Any help would be much appreciated. This is the code I am using to test the input string.

System.out.println("Enter filer full name (First Middle Last):");
        Scanner input = new Scanner(System.in);
            String fullName = input.nextLine();
                int n = fullName.indexOf(' ');
                int m = fullName.indexOf(' ', n + 1);
                    String firstName = fullName.substring(0, n);
                    String middleName = fullName.substring(n + 1, m);
                    String lastName = fullName.substring(m + 1);

Upvotes: 0

Views: 67

Answers (5)

CodeWalker
CodeWalker

Reputation: 2378

Some names may or may not have a middle name as in John Doe while some names may have a middle name that comprises of more than 2 names as in John Smith Williams Joseph Doe.
Hence you can try this!

System.out.println("Enter filer full name (First Middle Last):");
Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
String name [] = fullName.split(" ");
if(name.length == 1) {
    System.out.println("You don't have spaces in the name");
}
else
   if(name.length == 2) {
     System.out.println("First Name: " + name[0]);
     System.out.println("Last Name: " + name[1]);
}else
    if(name.length > 2) {
     System.out.println("First Name: " + name[0]);
     System.out.print("Middle Name: ");
       for (int i = 1; i < (name.length-1) ; i++) {
            System.out.print(name[i] + " ");
       }
     System.out.println();
     System.out.println("Last Name: " + name[(name.length-1)]);
}


Output -

Enter filer full name (First Middle Last): John Smith Williams Joseph Doe First Name: John Middle Name: Smith Williams Joseph Last Name: Doe

Upvotes: 0

FlamingPickle
FlamingPickle

Reputation: 189

Before you take the substrings, useif(n >-1 && m > -1) to tell if you have the spaces. Your new code would look something like this

System.out.println("Enter filer full name (First Middle Last):");
Scanner input = new Scanner(System.in);
String fullName = input.nextLine();
int n = fullName.indexOf(' ');
int m = fullName.indexOf(' ', n + 1);
if(n>-1&&m>-1){
    String firstName = fullName.substring(0, n);
    String middleName = fullName.substring(n + 1, m);
    String lastName = fullName.substring(m + 1);
}else{
    System.out.println("Don't have spaces!");
}

Upvotes: 2

Vishnu
Vishnu

Reputation: 1534

You can use the indexOf() method for this.

Eg:

if(fullName.indexOf(' ') == -1){ // index of space is -1, means space is not present }

Upvotes: 1

Scary Wombat
Scary Wombat

Reputation: 44834

How about just using a split

 String fullname = "Scary Old Wombat";
 String [] names = fullname.split (" ");

 assert (names.length == 3);

Upvotes: 1

Logan
Logan

Reputation: 946

indexOf returns negative one if the input is not contained in the String. Also, String indices start at zero. So to test for it you would do something like this:

if (fullName.indexOf(" ") == -1)
{
  System.out.print("Space not contained in fullName");
} //end if

Upvotes: 1

Related Questions