Parsing?

I have the following code:

String Num_of_record;

String Rec_id;

Rec_id = "1,2,3,4,5"

Num_of_record = ?

* I need to find the total amount of commas in the string

and add one to it.

* In this example Rec_id should = 4 + 1

4 being the number of commas

A sample of this would be the best. I also thank all the people that are helping me overcome these stumbling blocks. Thanks :0)

[444 byte] By [grasshopper3] at [2007-9-26 1:53:09]
# 1

Do you want to count the number of commas, or do you really want to know how many fields you have there?

Check out the docs for StringTokenizer

[code]

String Rec_id = '1,2,3,4,5';

StringTokenizer st = new StringTokenizer (rec_id, ",");

System.out.println ("There are " + st.countTokens + " in " + Rec_id);

ldadams at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...
# 2

Do you want to count the number of commas, or do you really want to know how many fields you have there?

Check out the docs for StringTokenizer

String Rec_id = '1,2,3,4,5';

StringTokenizer st = new StringTokenizer (rec_id, ",");

System.out.println ("There are " + st.countTokens + " in " + Rec_id);

sorry... clipped off the end of my comment, first time...

ldadams at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...
# 3

String st = "1,2,3,4,5";

int numCommas = 0;

// create a copy of the string

String temp = (String)st.clone();

while( temp.indexOf(',') != -1)

{

numCommas++;

temp = temp.substring(temp.indexOf(',')+1);

}

schillj at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...
# 4

StringTokenizer is way overkill. Why would you want to make your processor index up a string into tokens when all you want to do is count some characters.

The String example is also extremely inefficient. It's ludicrous to instantiate a new String for every comma you find.

Here's an efficient example that will compile and run as-is.

class CountCommas {

static public void main(String[] s) {

if (s.length == 0) System.exit(1);

int count = 0, i1, i = 0;

while ((i1 = s[0].substring(i).indexOf(',')) != -1) {

count++;

i += ++i1;

};

System.out.println(count + " commas");

}

}

blaine.simpson at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...
# 5

Oops. I sent my first cut of my script instead of the last. (That version's about as inefficient as the other String example someone wrote).

This example just moves the index down the existing String. No constructors at all.

class CountCommas {

static public void main(String[] s) {

if (s.length == 0) System.exit(1);

int count = 0, i = 0;

while ((i = s[0].indexOf(',', i) + 1) != 0) count++;

System.out.println(count + " commas");

}

}

blaine.simpson at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...
# 6
Yep.I was going to say that your first example was just as bad (using substring). I never saw that String.indexOf(int ch, int fromIndex) method in the API. Handy...
schillj at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...
# 7

class CountCommas {

static public void main(String[] s) {

if (s.length == 0) System.exit(1);

int count = 0, i = 0;

while ((i = s[0].indexOf(',', i)) != -1) count++;

System.out.println(count + " commas");

}

}

Isn't this slightly better? Removing the addition every while loop...:P

schillj at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...
# 8
Unfortunately, it will cause an infinite loop, because each IndexOf call will now start from the position of the last found comma and will hence simply find it again and return the position of the first comma forever. (8-ODoug
DougDean at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...
# 9
Curses...foiled again...I'll shut up now :)
schillj at 2007-6-29 3:03:51 > top of Java-index,Archived Forums,Java Programming...