Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

split the string..

Posted on 2009-04-24
23
Medium Priority
?
328 Views
Last Modified: 2013-11-23
I had the string as below....

JENNINGS

is it possible to split as below...using string tokenizer..

J E N N I N G S



0
Comment
Question by:shragi
  • 12
  • 10
23 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 24229338
Try
String[] letters = s.split("(?<!^)");

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24229390
no, but the toCharAyyay() method will do it for you:

char[] chars = s.toCharArray();

0
 

Author Comment

by:shragi
ID: 24230203

input file is of form..
% JENNINGS ROBOT

here first name with  8 letters so
temp_char[8] will get nothing in the below...code....

so its giving an error...like below...

java.lang.ArrayIndexOutOfBoundsException: 8
      at readf.main(readf.java:78)

what should I do to below code so that the SOP's

print JENNINGS as J E N N I N G S
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
 
 
public class readf {
	
	public static void main(String args[])
	{
	  String[][] valuePairs;
	  String names = "null";
      ArrayList<String> temp1=new ArrayList<String>(60000);
      ArrayList<String> temp2=new ArrayList<String>(60000);
      ArrayList<String> temp3=new ArrayList<String>(60000);
      ArrayList<String> temp4=new ArrayList<String>(60000);
      ArrayList<String> temp5=new ArrayList<String>(60000);
      ArrayList<String> temp6=new ArrayList<String>(60000);
      ArrayList<String> temp7=new ArrayList<String>(60000);
      ArrayList<String> temp8=new ArrayList<String>(60000);
      ArrayList<String> temp9=new ArrayList<String>(60000);
      ArrayList<String> temp10=new ArrayList<String>(60000);
      ArrayList<String> temp11=new ArrayList<String>(60000);
      ArrayList<String> temp12=new ArrayList<String>(60000);
      ArrayList<String> temp13=new ArrayList<String>(60000);
      ArrayList<String> temp14=new ArrayList<String>(60000);
      ArrayList<String> temp15=new ArrayList<String>(60000);
      ArrayList<String> temp16=new ArrayList<String>(60000);
      ArrayList<String> temp17=new ArrayList<String>(60000);
      ArrayList<String> temp18=new ArrayList<String>(60000);
      ArrayList<String> temp19=new ArrayList<String>(60000);
      ArrayList<String> temp20=new ArrayList<String>(60000);
      ArrayList<String> temp21=new ArrayList<String>(60000);
      ArrayList<String> temp22=new ArrayList<String>(60000);
      ArrayList<String> temp23=new ArrayList<String>(60000);
      ArrayList<String> temp24=new ArrayList<String>(60000);
      ArrayList<String> temp25=new ArrayList<String>(60000);
      ArrayList<String> temp26=new ArrayList<String>(60000);
      
          try             
          {
        	  FileInputStream fstream = new FileInputStream("c:/inputs.txt");
              DataInputStream datastream = new DataInputStream(fstream);
              BufferedReader br = new BufferedReader(new InputStreamReader(datastream));
              String strLine;
              while ((strLine = br.readLine()) != null)
              {
            	  String temp_strs[] = strLine.split(" ");
                  temp1.add(temp_strs[0]);
                  temp2.add(temp_strs[1]);
                  temp3.add(temp_strs[2]);  
              }
              datastream.close();
          }
          catch (Exception e)
          {
        	  //file not found exception...
              e.printStackTrace();
          }
         try
         {
        	 for(int i=0; i<temp2.size(); i++)
             {
        		 String temp_char[] = temp2.get(i).split("(?<!^)");
        		 temp4.add(temp_char[0]);
        		 temp5.add(temp_char[1]);
        		 temp6.add(temp_char[2]);
        		 temp7.add(temp_char[3]);
        		 temp8.add(temp_char[4]);
        		 temp9.add(temp_char[5]);
        		 temp10.add(temp_char[6]);
        		 temp11.add(temp_char[7]);
        		 temp12.add(temp_char[8]);
        		 temp13.add(temp_char[9]);
        		 temp14.add(temp_char[10]);
        		 temp15.add(temp_char[11]);
        		 String temp_chars[] = temp3.get(i).split("(?<!^)");
        		 temp16.add(temp_chars[0]);
        		 temp17.add(temp_chars[1]);
        		 temp18.add(temp_chars[2]);
        		 temp19.add(temp_chars[3]);
        		 temp20.add(temp_chars[4]);
        		 temp21.add(temp_chars[5]);
        		 temp22.add(temp_chars[6]);
        		 temp23.add(temp_chars[7]);
        		 temp24.add(temp_chars[8]);
        		 temp25.add(temp_chars[9]);
        		 temp26.add(temp_chars[10]);
        		 
        		 System.out.println(temp4.get(i) + " " + temp5.get(i) + " " +temp6.get(i) +
  				 " " + temp7.get(i) + " " +temp8.get(i) + " " +temp9.get(i) + " " +temp10.get(i) + " " +temp11.get(i) 
  				 + " " +temp12.get(i) + " " +temp13.get(i) + " " +temp14.get(i) + " " +temp15.get(i));
        		 System.out.println(temp16.get(i) + " " + temp17.get(i) + " " +temp18.get(i) +
          	" " + temp19.get(i) + " " +temp20.get(i) + " " +temp21.get(i) + " " +temp22.get(i) + " " +temp23.get(i) 
          	 + " " +temp24.get(i) + " " +temp25.get(i) + " " +temp26.get(i));
                		 
        		 
}
 
}
         catch (Exception e)
         {
        	 //file not found exception...
             e.printStackTrace();
         }
	}
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 92

Expert Comment

by:objects
ID: 24230212
don't use split() its inefficient and unnecessary, toCharArray() will give you what you need.
0
 

Author Comment

by:shragi
ID: 24230221
here it is JENNINGS ..
so u may ask me to write till temp_char[9] only
but sometimes the name may be ...
RANGI


0
 
LVL 92

Expert Comment

by:objects
ID: 24230238
you can check the length of the array to determine how many chars''
eg.

if (chars.length>9) temp.add(Character.toString(chars[9]));

instead of temp1, temp2, temp3 you may find it simpler to use an array of Lists. That would allow you to process the characters in a loop.

0
 
LVL 92

Expert Comment

by:objects
ID: 24230251
what is it you are trying to do?
0
 

Author Comment

by:shragi
ID: 24230270
then how can I print them....

a I don't know how many characters I will write everything inSOP
so if
chars.length<9 then chars[9] will not be added with anything....
so tem9.get(i) is nothing... and its returning an error...
this time I used chararray....

 System.out.println(temp4.get(i) + " " + temp5.get(i) + " " +temp6.get(i) +
                                   " " + temp7.get(i) + " " +temp8.get(i) + " " +temp9.get(i) + " " +temp10.get(i) + " " +temp11.get(i)
                                   + " " +temp12.get(i) + " " +temp13.get(i) + " " +temp14.get(i) + " " +temp15.get(i));
0
 
LVL 92

Expert Comment

by:objects
ID: 24230283

if (temp4.size()>i) System.out.println(temp4.get(i)+" ");
if (temp5.size()>i) System.out.println(temp5.get(i)+" ");
...
0
 

Author Comment

by:shragi
ID: 24230287
I had input file of below format..

% JENNINGS RUTE
& RUTHYGE  YUT
% HHYTGRE TYG

I want output file as
J E N N I N G S  R U T E, %
R U T H Y G E  Y U T,  &
H H Y T G R E T Y G, %

so I read the input file... into 3 array list...
temp1 contains %
temp2 contains JENNINGS
temp3 contains RUTE

again I splitted temp2 using chararray char[] to form J E N N I N G S
smilarly temp3 into charl[]
finally want to print
char[] charl[] and temp1

so if I can print it to console I can write it to file....
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
 
 
public class readf {
	
	public static void main(String args[])
	{
	  String[][] valuePairs;
	  String names = "null";
      ArrayList<String> temp1=new ArrayList<String>(60000);
      ArrayList<String> temp2=new ArrayList<String>(60000);
      ArrayList<String> temp3=new ArrayList<String>(60000);
      ArrayList<String> temp4=new ArrayList<String>(60000);
      ArrayList<String> temp5=new ArrayList<String>(60000);
      ArrayList<String> temp6=new ArrayList<String>(60000);
      ArrayList<String> temp7=new ArrayList<String>(60000);
      ArrayList<String> temp8=new ArrayList<String>(60000);
      ArrayList<String> temp9=new ArrayList<String>(60000);
      ArrayList<String> temp10=new ArrayList<String>(60000);
      ArrayList<String> temp11=new ArrayList<String>(60000);
      ArrayList<String> temp12=new ArrayList<String>(60000);
      ArrayList<String> temp13=new ArrayList<String>(60000);
      ArrayList<String> temp14=new ArrayList<String>(60000);
      ArrayList<String> temp15=new ArrayList<String>(60000);
      ArrayList<String> temp16=new ArrayList<String>(60000);
      ArrayList<String> temp17=new ArrayList<String>(60000);
      ArrayList<String> temp18=new ArrayList<String>(60000);
      ArrayList<String> temp19=new ArrayList<String>(60000);
      ArrayList<String> temp20=new ArrayList<String>(60000);
      ArrayList<String> temp21=new ArrayList<String>(60000);
      ArrayList<String> temp22=new ArrayList<String>(60000);
      ArrayList<String> temp23=new ArrayList<String>(60000);
      ArrayList<String> temp24=new ArrayList<String>(60000);
      ArrayList<String> temp25=new ArrayList<String>(60000);
      ArrayList<String> temp26=new ArrayList<String>(60000);
      
          try             
          {
        	  FileInputStream fstream = new FileInputStream("c:/inputs.txt");
              DataInputStream datastream = new DataInputStream(fstream);
              BufferedReader br = new BufferedReader(new InputStreamReader(datastream));
              String strLine;
              while ((strLine = br.readLine()) != null)
              {
            	  String temp_strs[] = strLine.split(" ");
                  temp1.add(temp_strs[0]);
                  temp2.add(temp_strs[1]);
                  temp3.add(temp_strs[2]);  
              }
              datastream.close();
          }
          catch (Exception e)
          {
        	  //file not found exception...
              e.printStackTrace();
          }
          valuePairs=new String[2][temp1.size()];
          //now print the array to output file
         try
         {
        	 for(int i=0; i<temp2.size(); i++)
             {
        		 char[] chars = temp2.get(i).toCharArray();
                 // System.out.println(chars[0]);
                		 
        		 //String temp_char[] = temp2.get(i).split("(?<!^)");
        		 if (chars.length>0) temp4.add(Character.toString(chars[0]));
        		 if (chars.length>1) temp5.add(Character.toString(chars[1]));
        		 if (chars.length>2) temp6.add(Character.toString(chars[2]));
        		 if (chars.length>3) temp7.add(Character.toString(chars[3]));
        		 if (chars.length>4) temp8.add(Character.toString(chars[4]));
        		 if (chars.length>5) temp9.add(Character.toString(chars[5]));
        		 if (chars.length>6) temp10.add(Character.toString(chars[6]));
        		 if (chars.length>7) temp11.add(Character.toString(chars[7]));
        		 if (chars.length>8) temp12.add(Character.toString(chars[8]));
        		 if (chars.length>9) temp13.add(Character.toString(chars[9]));
        		 if (chars.length>10) temp14.add(Character.toString(chars[10]));
        		 if (chars.length>11) temp15.add(Character.toString(chars[11]));
        		 
        		 char[] charl = temp3.get(i).toCharArray();
                 if (chars.length>0) temp16.add(Character.toString(charl[0]));
        		 if (chars.length>1) temp17.add(Character.toString(charl[1]));
        		 if (chars.length>2) temp18.add(Character.toString(charl[2]));
        		 if (chars.length>3) temp19.add(Character.toString(charl[3]));
        		 if (chars.length>4) temp20.add(Character.toString(charl[4]));
        		 if (chars.length>5) temp21.add(Character.toString(charl[5]));
        		 if (chars.length>6) temp22.add(Character.toString(charl[6]));
        		 if (chars.length>7) temp23.add(Character.toString(charl[7]));
        		 if (chars.length>8) temp24.add(Character.toString(charl[8]));
        		 if (chars.length>9) temp25.add(Character.toString(charl[9]));
        		 if (chars.length>10) temp25.add(Character.toString(charl[10]));
        		 
        		 
        		 
        		 System.out.println(temp4.get(i) + " " + temp5.get(i) + " " +temp6.get(i) +
          				 " " + temp7.get(i) + " " +temp8.get(i) + " " +temp9.get(i) + " " +temp10.get(i) + " " +temp11.get(i) 
          				 + " " +temp12.get(i) + " " +temp13.get(i) + " " +temp14.get(i) + " " +temp15.get(i));
        		 System.out.println(temp16.get(i) + " " + temp17.get(i) + " " +temp18.get(i) +
          	" " + temp19.get(i) + " " +temp20.get(i) + " " +temp21.get(i) + " " +temp22.get(i) + " " +temp23.get(i) 
          	 + " " +temp24.get(i) + " " +temp25.get(i) + " " +temp26.get(i));
                		 
        		 
             }
}
         catch (Exception e)
         {
        	 //file not found exception...
             e.printStackTrace();
         }
	}
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 24230288
do you really need the intermediate lists?

char[] chars = s.toCharArray();
for (char c : chars) {
   System.out.print(c+" ");
}

(could also use a StringBuilder here)


PS. should be print, not println in previous comment

0
 
LVL 92

Expert Comment

by:objects
ID: 24230299
             while ((strLine = br.readLine()) != null)
              {
                    String temp_strs[] = strLine.split(" ");
                  for (String s : temp_strs)
                  {
                      System.out.print(addSpaces(s));
                      System.out.print(" ");
                  }
                  System.out.println();
               }



0
 

Author Comment

by:shragi
ID: 24230306
but how to write to another file.. if I don't store in intermediate lists..
0
 
LVL 92

Expert Comment

by:objects
ID: 24230308
Use a FileWriter instead of System.out

             PrintWriter out = new PrintWriter(new FileWriter("out.txt"));
             while ((strLine = br.readLine()) != null)
              {
                  String temp_strs[] = strLine.split(" ");
                  for (String s : temp_strs)
                  {
                      out.print(addSpaces(s));
                      out.print(" ");
                  }
                  out.println();
               }

0
 

Author Comment

by:shragi
ID: 24230309
FileWriter output = new FileWriter("c:/output.txt");
BufferedWriter bw = new BufferedWriter(output);
for(int i=0; i<temp3.size(); i++)
             {
             bw.write(temp2.get(i)+ " " + temp3.get(i)+","+temp1.get(i));
                   bw.newLine();                  
             }
                   bw.close();       



         if i write the above code I get the output in output.txt as
JENNINGS RUTH, %

but I want to write as
J E N N I N G S  R U T E, %
0
 
LVL 92

Expert Comment

by:objects
ID: 24230317
see the code I posted above, you can write the output directly while u read without need for intermediate lists

0
 

Author Comment

by:shragi
ID: 24230321
one more thing I said that I want to write output as
J E N N I N G S  R U T E, %
 but how to include EMPTY between firstname and lastname

J E N N I N G S EMPTY R U T E, %
0
 
LVL 92

Expert Comment

by:objects
ID: 24230328
            PrintWriter out = new PrintWriter(new FileWriter("out.txt"));
             while ((strLine = br.readLine()) != null)
              {
                  String temp_strs[] = strLine.split(" ");
                  for (int i=0; i
0
 

Author Comment

by:shragi
ID: 24230329
and what is printwriter I knew filewriter..
is print writer an variable..
0
 
LVL 92

Expert Comment

by:objects
ID: 24230341
0
 

Author Comment

by:shragi
ID: 24230363
how about addSpaces(S)  its showing an error for me..
0
 

Author Comment

by:shragi
ID: 24230366
hey dude u modified code with below line
  if (i==0) out.print("EMPTY ");

but this print EMPTY at the starting but I want in the middle of firstname and lastname...
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 24230372
> how about addSpaces(S)  its showing an error for me..

you'll need to add that method using the code I posted earlier to add spaces to a string
A StringBuilder is useful for that

public String addSpaces(String s)
{
   StringBuilder sb = new StringBuilder();
   char[] chars = s.toCharArray();
   for (char c : chars) {
      sb.append(c);
      sb.append(" ");
   }
   return sb.toString().trim();
}

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question