import java.util.Scanner; 

public class Inti{
	public static void main (String [] args){
		
	Scanner kb = new Scanner(System.in);
	
	System.out.println("enter name: ");
	String input = kb.nextLine();
	
	char name1 = input.charAt(0);
	int blank = input.indexOf(' '+1);
	char name2 = input.charAt(blank);
	int lastBlank = input.lastIndexOf(' '+1);
	char name3 = input.charAt(lastBlank);
	
	System.out.println(name1 + "\n" + name2 +"\n"+ name3 );
	 
	
	
	}
}

i keep getting a runtime error. the user is supposed to enter a first, middle, and last name and the program prints out the initials.

Recommended Answers

All 3 Replies

i keep getting a runtime error.

please copy the full text of the error message and paste it here. I can't see your screen from here, so I'll need your help so I can see what the problem is.
If you are on Windows:
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'

Paste here.

not sure what this line is doing:

int blank = input.indexOf(' '+1);

You are using '' + 1 as an argument to indexOf. if you are trying to find the index of ' ', i think you want just indexOf(' '); then do blank++; maybe on next line or do

int blank = input.indexOf(' ') + 1;

edit -- corrected code tags here

my guess is ''+1 is a search for the phrase " 1" i.e. a string of 2 chars, first is blank and second is the number 1.

Also a good idea is to catch when your indexOf does not find the char or returns -1, as that is good to know from a debugging standpoint. IndexOf can take 2 arguments, telling it to find something starting at a certain point, which lets you skip ahead past the first instance, but arguments are comma, , seperated, ' '+1 is probably read as a single argument of a string of 2 chars maybe " 1". But without running the code i cant tell for sure. In the c language when you do ' ' + 1 it literally adds one to the assci value of the space char and produces a new char. In java my understanding is the + sign is used for concatenation always.

Mike

thanks!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.