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();
System.out.println(nmbrs);
// char[] textArray = new char[3];
// nmbrs.getChars(9, 12, textArray, 0);
// System.out.println(textArray);
int difx;
difx=Integer.parseInt(nmbrs);
System.out.println(difx+1);
Can anyone see what I'm doing wrong?
Again, thanks for the help guys ;)
no probs, here's the source and results
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ReadTextFile
{
public static void main(String[] args)
{
File file = new File("c:\\myjava\\test.txt");
StringBuffer contents = new StringBuffer();
BufferedReader reader = null;
try
{
reader = new BufferedReader(new FileReader(file));
String text = null;
while ((text = reader.readLine()) != null)
{
contents.append(text)
.append(System.getProperty("line.separator"));
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (reader != null)
{
reader.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
String text = contents.toString();
System.out.println(text);
char[] textArray = new char[3];
text.getChars(9, 12, textArray, 0);
System.out.println(textArray);
int difx;
difx=Integer.parseInt(text);
System.out.println(difx+1);
}
}
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)
at java.lang.Integer.parseInt(Integer.java:459)
at java.lang.Integer.parseInt(Integer.java:497)
at ReadTextFile.main(ReadTextFile.java:57)
Interactive Session Ended
What do you think?