split the string..

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



shragiAsked:
Who is Participating?
 
objectsCommented:
> 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
 
CEHJCommented:
Try
String[] letters = s.split("(?<!^)");

Open in new window

0
 
objectsCommented:
no, but the toCharAyyay() method will do it for you:

char[] chars = s.toCharArray();

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
shragiAuthor Commented:

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
 
objectsCommented:
don't use split() its inefficient and unnecessary, toCharArray() will give you what you need.
0
 
shragiAuthor Commented:
here it is JENNINGS ..
so u may ask me to write till temp_char[9] only
but sometimes the name may be ...
RANGI


0
 
objectsCommented:
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
 
objectsCommented:
what is it you are trying to do?
0
 
shragiAuthor Commented:
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
 
objectsCommented:

if (temp4.size()>i) System.out.println(temp4.get(i)+" ");
if (temp5.size()>i) System.out.println(temp5.get(i)+" ");
...
0
 
shragiAuthor Commented:
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
 
objectsCommented:
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
 
objectsCommented:
             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
 
shragiAuthor Commented:
but how to write to another file.. if I don't store in intermediate lists..
0
 
objectsCommented:
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
 
shragiAuthor Commented:
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
 
objectsCommented:
see the code I posted above, you can write the output directly while u read without need for intermediate lists

0
 
shragiAuthor Commented:
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
 
objectsCommented:
            PrintWriter out = new PrintWriter(new FileWriter("out.txt"));
             while ((strLine = br.readLine()) != null)
              {
                  String temp_strs[] = strLine.split(" ");
                  for (int i=0; i
0
 
shragiAuthor Commented:
and what is printwriter I knew filewriter..
is print writer an variable..
0
 
objectsCommented:
0
 
shragiAuthor Commented:
how about addSpaces(S)  its showing an error for me..
0
 
shragiAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.