Anyone know how to take info from a txt file and convert it to a char array?
I'm really sorry guys, In my head this seems like the easiest thing in the world to do, but I cannot get it to work?
Can anyone suggest how?
me = noob
spoonfeeder for hire,....
I will gladly spoon feed this:
Use any Reader subclass that has a readLine();
That goes to a String, which is subject to all manner of sane approaches. You just do it one line at a time.
sorry, I was being a little too basic huh..?
Thanks to all the guys that wrote something constructive on here ;)
But it was my fault for not asking the question correctly. It was more like half of a question. Here's my dilemna:
I have a txt file filled with hundreds and thousands of digits and no spaces.
I can read from the text file no probs and have been using get chars to select the digits I need. But I can't figure out how to convert the newly formed character string into an integer. It won't accept Integer.parseInt ..well it compiles ok, but always throws up an error when it runs.
Am I doing something blatantly obvious or is their a trick to converting either a char array or a string from a text file into an integer?
I'm just using the following lines to test if it works.
String nmbrs = contents.toString();
// char textArray = new char;
// nmbrs.getChars(9, 12, textArray, 0);
Can anyone see what I'm doing wrong?
Again, thanks for the help guys ;)
no probs, here's the source and results
public class ReadTextFile
public static void main(String args)
File file = new File("c:\\myjava\\test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
} catch (FileNotFoundException e)
} catch (IOException e)
if (reader != null)
} catch (IOException e)
String text = contents.toString();
char textArray = new char;
text.getChars(9, 12, textArray, 0);
And the error outputs:
The current directory is: @MyProjects
61541116546878646541321357687461351354638541351864 1 <-- this is the full number in my text file
468 <-- the getChars numbers
Exception in thread "main" java.lang.NumberFormatException: For input string: "6154111654687864654132135768746135135463854135186 41
at java.lang.NumberFormatException.forInputString(Num berFormatException.java:48)
Interactive Session Ended
What do you think?