LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-22-2011, 12:57 AM   #1
shik28
LQ Newbie
 
Registered: Sep 2011
Location: India
Posts: 25

Rep: Reputation: Disabled
Retrieve numerical values from a text file


I have to retrieve numbers from a text file which is as shown in the attachment. I have written the following code snippet just to check if I am able to print the numbers in the file.
Code:
try {
	Scanner myFile = new Scanner(new FileReader("C:/Documents and Settings/shikhau/workspace/SanNumber/freelast.txt"));
	while (myFile.hasNext()) {
		i = myFile.nextInt();
		System.out.println(i);
				
	}
}
catch (FileNotFoundException e){
          System.out.println("Sorry! This file is not found");
}
I get the following exception:
Code:
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at java.util.Scanner.next(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
	at java.util.Scanner.nextInt(Unknown Source)
What do I do?
Attached Files
File Type: txt freelast.txt (271 Bytes, 4 views)

Last edited by shik28; 11-22-2011 at 12:58 AM. Reason: Sentence editing
 
Old 11-22-2011, 02:02 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
You're getting this exception because you're trying to get an int (with nextInt()) when the next token is not recognised as an integer. Have a look at the docs for Scanner here. In particular, hasNextInt() will let you see if the next token is an int and next() will let you skip any non-int input. Exactly how you want to do that depends on how strict/forgiving you want to be on the format of the configuration file.

Last edited by JohnGraham; 11-22-2011 at 02:03 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Octave - Load a text file with floating point values into a matrix Gavin Harper Programming 2 01-30-2011 12:23 AM
updating a text file (replacing the existing parameters with the new values) rastin_nz Programming 4 11-10-2010 02:10 AM
[SOLVED] Variables in text file, how to get their values when printing out the text? idaham Linux - General 2 04-14-2010 03:28 AM
Text file manipulation: Extracting specific rows according to numerical pattern CHARL0TTE Linux - Newbie 3 10-07-2009 07:14 AM
Retrieve a text file? InfinitePalindrome Linux - Newbie 1 06-23-2008 10:18 PM


All times are GMT -5. The time now is 04:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration