-1

I have a text which I need to read word by word till ';' delimiter... I have searched in many places and also i have read some chapters but I can't find any method to use... Please help!

string to read i.e 32; Potatoe; Vegetables; 21.30; 12.20; 15 21 32 45;

Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
s33h
  • 183
  • 1
  • 8
  • 1
    possible duplicate of [Split Java String into Two String using delimiter](http://stackoverflow.com/questions/7787385/split-java-string-into-two-string-using-delimiter) – McDowell Mar 30 '14 at 12:32
  • http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split(java.lang.String) – Srinath Mandava Mar 30 '14 at 12:32
  • there is an exact duplicate of what you have asked here --> http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – Vishal Nair Mar 30 '14 at 12:36

5 Answers5

1
String s = "32; Potatoe; Vegetables; 21.30; 12.20; 15 21 32 45;";
String[] splittedWords;

splittedWords = s.split(";");

You can use the method split to seperate words along delimiters. It will return a list of Strings. If you want to parse the values in the string to an Integer you can use this:

for (String string : splittedWords)
    {
        if(string.matches("[^a-z \\.]+")==true)
        {
            int value = Integer.parseInt(string);

            System.out.println(value);
        }
    }

the only integer in your samplestring is 32, though. Thats why this code will only output "32".

Iconstrife
  • 133
  • 1
  • 6
  • I did that but I cannot parse from String to Int, I get an error while trying to convert with an enhanced for loop – s33h Mar 30 '14 at 18:15
  • 1
    thnx for answer again.. i tried several things included what u did above but nothing produced the desired output.For explanation: 32 is the article number while others are name, category, price and also quantities. I wanted to add quantities but i couldn't.. thnx again :) – s33h Mar 30 '14 at 21:08
  • No Problem... It certainly is possible to parse the other values as well, but that would require a little more sophisticated algorithm. – Iconstrife Mar 31 '14 at 19:14
0

Try this:

for ( String s : myOwnString.split(";") ){
    System.out.println(s);
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
SerCrAsH
  • 440
  • 5
  • 14
0

Java supports this very clearly, with the String.split() method. You can read the documentation here.

Example

String[] tokens = input.split(";");

// tokens contains each value.
christopher
  • 26,815
  • 5
  • 55
  • 89
0

You can use split method of String.

String string = "32; Potatoe; Vegetables; 21.30; 12.20; 15 21 32 45";
String[] split = string.split(";");

for(String s: split)
{
     System.out.println(s);
}

This will print:

32
Potatoe
Vegetables
21.30
12.20
15 21 32 45
Rahul Bobhate
  • 4,892
  • 3
  • 25
  • 48
  • Thnx I did this but I cant parse to an int for example: id = Integer.parseInt(s); I get an error – s33h Mar 30 '14 at 18:19
0
String text = "32; Potatoe; Vegetables; 21.30; 12.20; 15 21 32 45";
String[] words = new String[text.length()];
int initialIndex = 0,i=0;

while (initialIndex<text.length()) {
    words[i] = text.substring(initialIndex, text.indexOf(";"));
    i++;
    initialIndex = text.indexOf(";")+1;
    }

Now String of words contains all words in text. You can access by word.get(index);

asad
  • 23
  • 7