This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
    Bookmark Topic Watch Topic
  • New Topic

split the data  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Report post to moderator
hi guys

i have a test file like raju.txt

naga raju rama raju

using java.util.Scanner we can split like


naga
raju
rama
raju


the code is


public class FileRead1 {

public static void main(String arg[])
{
try
{
File f=new File("test.txt");
Scanner ss=new Scanner(f);

while(ss.hasNext())
{
String hh=ss.next();

System.out.println(""+hh);

//System.out.println(""+hh);
}
}catch(Exception e){}
}

}
}
what my idea is how to split the file like 5,8,10 charatcers like and store it in array .


give the idea.

thanks
naga
 
Marshal
Posts: 59092
180
  • Mark post as helpful
  • send pies
  • Report post to moderator
Plese use code tags round quoted code; it makes it easier to read.

You would have to
  • Read the file line by line; each line is returned as a String.
  • (Optional): Append the line to a StringBuilder object, if you wish to split the whole file rather than splitting each line
  • Get the length of the String you wish to split
  • Divide the length by the number of letters you want in each piece
  • Set up an array of appropriate length
  • Use a for loop to go along the String and get substrings.
  • I think that would work for what you are suggesting.
     
    author & internet detective
    Marshal
    Posts: 38249
    623
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Report post to moderator
    Please don't post the same question multiple times.
     
    Ever since I found this suit I've felt strange new needs. And a tiny ad:
    Rocket Oven Kickstarter - from the trailboss
    https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
      Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!