Bulota
Bulota

Reputation: 9

Printing last name + first name - but it shows error (Java)

So I have made a code for user to enter their first and last name; then exhanging the two position.

for example. Bulota Babi becomes Babi, Bulota

this is my code

public class trial {
    public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        String fname = input.substring(0, input.indexOf(0));
        String lname = input.substring(1, input.indexOf(input.length()));

        System.out.println(lname +","+ fname);
    }

}

im not sure whats wrong with it, this is the error message

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1967)
    at trial.main(trial.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Upvotes: 1

Views: 388

Answers (5)

shivam
shivam

Reputation: 491

The String.indexOf function works on the ASCII value. As you are passing 0 in indexOf function it returns value -1.

After that you are doing subString on input string having range (0,-1).

As Mentioned in java docs for java.lang.StringIndexOutOfBoundsException , java.lang.StringIndexOutOfBoundsException Exception will come to indicate that an index is either negative or greater than the size of the string.

So your exception is coming from input.substring function

Upvotes: 0

Scary Wombat
Scary Wombat

Reputation: 44834

Better to just use String.split

public static void main(String [] args) {
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine();

    String arr[] = input.split (" ");
    if (arr.length > 1)
        System.out.println(arr[1] +","+ arr[0]);
}

With your solution, it is trying to find a char with the ascii value 0 which of course is returning -1 as it does not exist - hence your substring is failing

edit

To answer your comment below

         String in = "John Appleseed William";
         String arr[] = in.split (" ");
         if (arr.length == 2) {
             System.out.println(arr[1] + ", " + arr[0]);
         }
         if (arr.length == 3) {
             System.out.println(arr[2] + ", " + arr[0] + " " + arr[1]);
         }

Upvotes: 3

Tran Ho
Tran Ho

Reputation: 1500

Let say you have an input string "Bulota Babi".

String fname = input.substring(0, input.indexOf(" "));
String lname = input.substring(input.indexOf(" "), input.length());

Upvotes: 1

Jay Smith
Jay Smith

Reputation: 2480

The problem is in substring method. It needs start and end positions of string to cut out from:

public static void main(String args[]) throws IOException {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();

        int coma = input.indexOf(' ');
        String fname = input.substring(0, coma);
        String lname = input.substring(coma + 1, input.length());

        System.out.println(lname + "," + fname);

    }

Upvotes: 0

DamCx
DamCx

Reputation: 1047

The input.indexOf(0) command is looking for the index of the char which has the ASCII value 0. As it is not found, it returns the value -1.

The indexOf is made to look for the place of a character in a String. You can't use it by giving it a number, unless it is the character ASCII value

Upvotes: 0

Related Questions