Robbie
Robbie

Reputation: 3

java how to separate words in a string and store each seperate word in a variable

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);]

    }

Upvotes: 0

Views: 10884

Answers (4)

Bijaya Bidari
Bijaya Bidari

Reputation: 341

It will be better to parse the input string using something like this.

int wordCount=0;
String words[MaxPossiWords];
int j=0;
char ch;
for(int i=0;i<sent.length();i++) {
    ch = sent.charAt(i);
    if( ch == ' '){
    words[wordCount++]= new String(curCharArray);
    j=0;
    }
    curCharArray[j++]=sent.charAt(i);
    }
//finished parsing the line to words.
for(int i=0;i<wordCount;i++)
   System.out.println(words[i]+", ");

This way make the parsing little bit dynamic. If you want more dynamism, you can use ArrayList collection instead of String array of words.
Happy Coding.

Upvotes: 0

poodle
poodle

Reputation: 107

Here is one example: Supposing that you store the input into a string named 'blah'. you can do this:

    String blah = "lass ias sfsf sfsfs sfsfs sfsdfs sfsdfs sfsdfs jj";

    String [] parts = blah.split(" "); //all parts stored in an array

            //Printing the array to show you the array content. 
    for(String s : parts) { 
        System.out.println("parts : " + s);         
    }

Upvotes: 0

TheKojuEffect
TheKojuEffect

Reputation: 21081

String[] nameParts = name.split(" ");
for(String part: nameParts) {
    // Use part as required
}

Check out String.split();

Upvotes: 0

Andromeda
Andromeda

Reputation: 1368

//your initialization 
List<String> names = new ArrayList<String>();
 Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter Full Product Name");
    name = keyboard.nextLine();
    names = Arrays.asList(name.split(" "));
   //your code

Upvotes: 1

Related Questions