0

I have a string which has delimiter in it. I want to know the best way to replace the delimiter with a new line. I have had various issues with using the String Tokenizer the main problem being NoSuchElementException. Basically my approach thus far is to retrieve data from a database Once this has been achieved I store each of the records in a string String question = c.getString(1); Here is the string tokenizer StringTokenizer st = new StringTokenizer(question,"<ENTER>"); I loop through the tokens using a while loop

 while (st.hasMoreTokens()) {
                        System.err.println(st.nextToken());
                       // quest.setText(String.valueOf(st.nextToken("<ENTER>")));

            }

Working example in code

String in = "What is the output of: <ENTER><ENTER>echo 6 % 4;"; 

            in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
            String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");

            for(int i=0;i<mSplitted.length;i++)
            {
                System.out.println("values: "+mSplitted[i]);
                quest.setText(String.valueOf(mSplitted[i]));
}

xml code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/quest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:text="TextView" />


    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.08"
        android:text="@string/hello" />

    <Button
        android:id="@+id/Next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

output

enter image description here

al23dev
  • 845
  • 2
  • 8
  • 17

2 Answers2

2

can't you use split() of String.

String in = "<ENTER>title=Java-Samples<ENTER>" + 
            "<ENTER>author=Emiley J<ENTER>" + 
            "<ENTER>publisher=java-samples.com<ENTER>" + 
            "<ENTER>copyright=2007<ENTER>"; 

in=in.substring(in.indexOf("<ENTER>")+7,in.lastIndexOf("<ENTER>"));
String[] mSplitted= in.replaceAll("<ENTER><ENTER>", "<ENTER>").split("<ENTER>");
String mFinal="";

for(int i=0;i<mSplitted.length;i++)
{
   System.out.println("values: "+mSplitted[i]);
   mFinal= mFinal+ mSplitted[i];
}
quest.setText(mFinal);
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • I made the change and added in the new code. It is only returning copyright = 2007 and not the other parts of the string – al23dev Jun 09 '12 at 06:39
  • 1
    See again, its doing perfectly fine. – Mohammed Azharuddin Shaikh Jun 09 '12 at 07:49
  • i just rebuilt the project with the exact code that you gave me and only got the last fragement of of the string that is split – al23dev Jun 09 '12 at 08:41
  • just cross verify by changing – Mohammed Azharuddin Shaikh Jun 09 '12 at 08:45
  • i tried your edited answer. I also included the information from your comment. I added the android:singleLin="false" but i still only get copyright = 2007. could you post your exact code you used to create your result for the TextView. I want to see if you have any issues. I have used exactly the same code as you. I copy and pasted it. – al23dev Jun 09 '12 at 12:47
  • it worked but it doesnt work with different strings What is the output of: echo 6 % 4 i need the code to be more generic. For example the string here returns a blank – al23dev Jun 09 '12 at 16:07
0

The reason behind NoSuchElementException is that you are checking whether your Tokenizer has more tokens, but if it does, then you are reading two tokens at the same time.

Documentation says hasMoreTokens tests if there are more tokens available from this tokenizer's string. If this method returns true, then a subsequent call to nextToken() with no argument will successfully return a token.

That means hasMoreTokens is only sure about the next token but not the token next to that!

Your condition should be..

while(st.hasMoreTokens()) { 
              String key = st.nextToken(); 
              String val="";
              if(st.hasMoreTokens())
                val = st.nextToken();  
            System.out.println(key + "\n" + val); 
        }

As your condition will work for only even number of elements.

Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
  • for some reason only the last part of the string is displayed copyright=2007 the other parts are not displayed. – al23dev Jun 09 '12 at 06:46