1

I need to load (int) data from file. New line separates different data so it's important to know where the new line is. I can use

string=readln(); 

and then I have whole line in that string. Then I can

string.trim("//s+") 

which chooses words(numbers) from string.

I can parse them to int:

int x = parseInt(string.trim("//s+").toString() );

Which should work (in theory) but it doesn't! There is Java.lang.something, but no number! How can I do it?

6
  • There is Java.lang.something? What does that mean? Commented Nov 22, 2011 at 0:27
  • Can you show us your whole program? What errors/exceptions are you getting? Commented Nov 22, 2011 at 0:27
  • There's no String#trim(String) in java. Please edit your question. Commented Nov 22, 2011 at 0:29
  • Don't write Java off the cuff into questions. It will always be wrong. Always. In this case, what is readln()? String.trim(String)? Do you mean Integer.parseInt? Commented Nov 22, 2011 at 0:34
  • sorry, i can't reproduce that code now :/ Tyler's answer seems reasonable Commented Nov 22, 2011 at 0:43

1 Answer 1

3

I like to use a set of two Scanners. One to read in the file line by line, assigning each new line to a temporary String, and another to read the line and gather the ints. Something like this:

Scanner sc = new Scanner(inputfile);
Scanner scanLine;
String line;
while(scanLine.hasNextLine())
{
line = sc.nextLine(); 
scanLine = new Scanner(line);
firstInt = scanLine.nextInt();
...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.