Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-22-2011, 12:57 AM   #1
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.
try {
	Scanner myFile = new Scanner(new FileReader("C:/Documents and Settings/shikhau/workspace/SanNumber/freelast.txt"));
	while (myFile.hasNext()) {
		i = myFile.nextInt();
catch (FileNotFoundException e){
          System.out.println("Sorry! This file is not found");
I get the following exception:
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Unknown Source)
	at 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, 7 views)

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

Rep: Reputation: 139Reputation: 139
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.


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:06 AM.

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