I need my scanner input to be broken down into separate words or wherever a space occurs. I have it somewhat working, however if the line has too many or too few words, i can't get it to work. Please help me. I have a Monday deadline. Here is my current code.
//import java.util.Scanner;
public class durodyne {
public static void main(String[] args) {
String name;
String part1;
String remain1;
String part2;
String part3;
String part4;
String part5;
String part6;
String part7;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Full Product Name");
name = keyboard.nextLine();
int space=name.indexOf(' ');
part1 = name.substring(0, space);
int space2 = name.indexOf(' ', space + 1);
int space3 = name.indexOf(' ', space2 + 1);
part2 = name.substring(space, space2);
int space4 = name.indexOf(' ', space3 + 1);
int space5 = name.indexOf(' ', space4 + 1);
part3 = name.substring(space2, space3);
int space6 = name.indexOf(' ', space5 + 1);
int space7 = name.indexOf(' ', space6 + 1);
part4 = name.substring(space3, space4);
part5 = name.substring(space4, space5);
part6 = name.substring(space5, space6);
part7 = name.substring(space6, space7);]
}
Best Answer