0

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);
jzaunegger
  • 11
  • 3

5 Answers5

2

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!");
}
FlamingPickle
  • 199
  • 1
  • 3
  • 14
  • If I use this method would I still be able to print the firstName, middleName, and lastName later on? Sorry if thats a dumb question I am really new to programming – jzaunegger Feb 26 '16 at 05:12
  • @jzaunegger If there's not two spaces, there aren't 3 names, so no, you cannot print them later (they don't exist). --- If you meant "can I use the 3 variables after the `if` statement ends", then yes, you can change the code to allow that, but what would you like the 3 variables to contain if only 2 names were present? If only 1 name was present? – Andreas Feb 26 '16 at 05:13
  • Sorry I meant if there are two spaces, I need to print them later and the IDE is saying it cant resolve them to a variable, so I was wondering if that was possible – jzaunegger Feb 26 '16 at 05:16
  • @jzaunegger What do you mean? Are the spaces being added? Can you show us the error or your code? That will be helpful. – FlamingPickle Feb 26 '16 at 05:17
  • Also @andreas the full comment did not load for me, but yes i would like to use the three variables after the if statement. So how would I go about initializing the three variables after that. Would I have to move the rest of my code inside the braces of the if statement? – jzaunegger Feb 26 '16 at 05:18
  • Exception in thread "main" java.lang.Error: Unresolved compilation problems: firstName cannot be resolved to a variable middleName cannot be resolved to a variable lastName cannot be resolved to a variable at IncomeTax.main(IncomeTax.java:116) – jzaunegger Feb 26 '16 at 05:19
  • @jzaunegger Are you saying you would like to refer to firstname, middlename, and lastname outside the if statement? If so, there are two ways. 1- place all the code inside the if block 2 - declare the three variables outside. However, if the second step is dependent on there being spaces, the code should probably go inside. – FlamingPickle Feb 26 '16 at 05:20
  • @FlamingPickle Alright I'll just move everything to inside the if block. Thanks for all the help guys – jzaunegger Feb 26 '16 at 05:22
  • @jzaunegger No problem. If this worked for you, could you please accept the answer? – FlamingPickle Feb 26 '16 at 05:29
1

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
Logan
  • 926
  • 1
  • 10
  • 18
1

How about just using a split

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

 assert (names.length == 3);
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

You can use the indexOf() method for this.

Eg:

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

Vishnu
  • 1,516
  • 1
  • 10
  • 15
0

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

CodeWalker
  • 2,281
  • 4
  • 23
  • 50