problem in stringtokenizer

Put your problem here if it does not fit any of the other categories.

problem in stringtokenizer

Postby Raghav_s » Sat Oct 10, 2009 7:56 am

problem in StringTokenizer

String str="one.1.two.2.three.3"

Stringtokenizer tok=new Stringtokenizer(str);
while(st.hasmoreTokens())
{
sb.append(st.nextToken);

}

Sustem.out.println(sb.toString());

output:

one
1
two
2
three
3

but i need output like this:
one
two
three

i want to cut the values 1,2,3

Thanks in Advance
Raghav.S
Raghav_s
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Sep 23, 2009 11:25 am
Location: Chennai

Top

Postby padde » Sat Oct 10, 2009 10:21 am

Two things..
1. your code does nothing.. check that first next time
2. please use code tags for posting code

I have two solutions for you.. but i am sure there are better solutions out there

First solution.. based on your provided code
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String str="one.1.two.2.three.3";
  2. StringBuffer sb = new StringBuffer();
  3. boolean switcher = false;
  4. StringTokenizer st = new StringTokenizer(str);
  5.  
  6. while(st.hasMoreTokens()) {
  7.     switcher = !switcher;
  8.     if(switcher) sb.append(st.nextToken(".")+"n"); else st.nextToken(".");
  9. }
  10. Log.v("", sb.toString());
  11.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Second solution.. one line string regex stuff
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String str="one.1.two.2.three.3";
  2. Log.v("",str.replaceAll(".d.", "n").replaceAll(".d", ""));
  3.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

thanks Aj..i need one more

Postby Raghav_s » Sat Oct 10, 2009 10:41 am

Hi padde thanks for ur kind reply......

Here i need one more ......

string str="one.1.two.2.three.3"

here i wil display one ,two,three in the list successfully by ur above code....

wat i need means when i click the one in listview it should display value "1"(corresponding value) and again if i

click two in list it should display "2".....how i retrieve the integer values from above string....

pls reply its very urgent.....

Thanks in Advance,

Raghav.S
Raghav_s
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Sep 23, 2009 11:25 am
Location: Chennai

Postby padde » Sat Oct 10, 2009 12:01 pm

Please provide more code.. no clue what list you are talking about..
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

here is my code.....

Postby Raghav_s » Sat Oct 10, 2009 12:12 pm

here i m using List Activity

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  str="1.one.2.two.3.three";
  2.  
  3.         StringBuffer sb = new StringBuffer();
  4.  
  5.         boolean switcher = true;
  6.  
  7.         StringTokenizer st = new StringTokenizer(str);
  8.  
  9.  
  10.  
  11.          while(st.hasMoreTokens()) {
  12.  
  13.             switcher = !switcher;
  14.  
  15.             if(switcher) sb.append(st.nextToken("|")+"\n");
  16.  
  17.             else st.nextToken("|");
  18.  
  19.         }
  20.  
  21.        
  22.  
  23.         String[] s1= sb.toString().split("\n");
  24.  
  25.      
  26.  
  27.        
  28.  
  29.            setListAdapter(new ArrayAdapter<String>(this,
  30.  
  31.                 //android.R.layout.simple_list_item_multiple_choice,s));
  32.  
  33.                            android.R.layout.simple_list_item_1,s1));
  34.  
  35.         getListView().setTextFilterEnabled(true);
  36.  
  37.  
  38.  
  39.  protected void onListItemClick(ListView parent, View v, int position, long id) {
  40.  
  41.        
  42.  
  43.         super.onListItemClick(parent, v, position, id);
  44.  
  45.        
  46.  
  47.          
  48.  
  49.        
  50.  
  51.    
  52.  
  53.         //{
  54.  
  55.                 if(position==0)
  56.  
  57.                 {
  58.  
  59.                 }
  60.  
  61.                
  62.  
  63.        
  64.  
  65.         else if(position == 1)
  66.  
  67.         {
  68.  
  69.                
  70.  
  71.         }
  72.  
  73.        
  74.  
  75.     }
  76.  
  77.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Raghav_s
Junior Developer
Junior Developer
 
Posts: 18
Joined: Wed Sep 23, 2009 11:25 am
Location: Chennai

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 20 guests