8 Replies - 8774 Views - Last Post: 14 August 2009 - 04:42 PM Rate Topic: -----

#1 MrCrake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-February 09

Trying to read integers using the Scanner class

Posted 21 February 2009 - 07:49 AM

Hello Internet.

Just a small (I hope) question. I'm trying to read an ANSI text file, which contains comma-delimited characters - similar to this.

12,34,21
34,65,67

Then stick them into a fixed-size array. The problem is, I can read the first line of numbers, but I can never get it to read past to the second line. I can only think it's reading the next line and throwing a hissy-fit because it's not an integer. Code here...

public void readFile2Data(String fileName) throws FileNotFoundException
	{
		File dataFile = new File(fileName);
		Scanner firstScanner = new Scanner(dataFile).useDelimiter(",");
		int i = 0;
		
		while(firstScanner.hasNext())
		{
		   myFilesArray[i][0] = firstScanner.nextInt();
		   System.out.println(myFilesArray[i][0]);
		   i++;
		}
		firstScanner.close();
	}



The text file is this if it helps;

65,60,55
70,60,65
80,78,82
40,50,60
90,100,90
40,60,80
65,45,70
45,52,62
35,40,35
67,63,65



I've tried using various characters (such as \r\n) as delimiters, but the result is always the same;

inputmismatchexception
null in java.util.scanner


Any tips would be awesome...

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to read integers using the Scanner class

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Trying to read integers using the Scanner class

Posted 21 February 2009 - 08:30 AM

you need both , and \n to act as delimitors so should scanner be?
		Scanner firstScanner = new Scanner(System.in).useDelimiter("[,\n]");
		while(firstScanner.hasNextInt())
		{


not if you are reading ints the test should be hasNextInt()
Was This Post Helpful? 0
  • +
  • -

#3 MrCrake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-February 09

Re: Trying to read integers using the Scanner class

Posted 21 February 2009 - 08:37 AM

Thanks. I tried that though and it simply grabs the first 2 numbers and leaves it at that (presumably because it can't read the third integer in the row as an...integer. I've tried a load of delimiter characters and I don't think the answer lies there.
Was This Post Helpful? 0
  • +
  • -

#4 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Trying to read integers using the Scanner class

Posted 21 February 2009 - 08:48 AM

View PostMrCrake, on 21 Feb, 2009 - 02:37 PM, said:

Thanks. I tried that though and it simply grabs the first 2 numbers and leaves it at that (presumably because it can't read the third integer in the row as an...integer. I've tried a load of delimiter characters and I don't think the answer lies there.

I just tried this program
public static void main(String s[]) throws FileNotFoundException
{
		Scanner firstScanner = new Scanner(new File("data")).useDelimiter("[,\n]");
		while(firstScanner.hasNextInt())
		{
		   int i = firstScanner.nextInt();
		   System.out.println("number " + i);
		}
}


on the file data
65,60,55
70,60,65
80,78,82
40,50,60
90,100,90
40,60,80
65,45,70
45,52,62
35,40,35
67,63,65


and it gave me
number 65
number 60
number 55
number 70
number 60
number 65
number 80
number 78
number 82
number 40
number 50
number 60
number 90
number 100
number 90
number 40
number 60
number 80
number 65
number 45
number 70
number 45
number 52
number 62
number 35
number 40
number 35
number 67
number 63
number 65


Was This Post Helpful? 1
  • +
  • -

#5 MrCrake  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-February 09

Re: Trying to read integers using the Scanner class

Posted 21 February 2009 - 09:05 AM

+1 kudos for the effort, but the result here is still the same. Very weird.

number 65
number 60



The only things I can think of are the format of the file (mine's ANSI. Tried saving it to Unicode, but it doesn't read anything then) is different here or...nah, I got nothing. You used those numbers in a file right? As mine reads as;

Scanner firstScanner = new Scanner(new File("infile2.txt")).useDelimiter("[,\n]");


Was This Post Helpful? 0
  • +
  • -

#6 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Trying to read integers using the Scanner class

Posted 21 February 2009 - 11:14 AM

View PostMrCrake, on 21 Feb, 2009 - 03:05 PM, said:

+1 kudos for the effort, but the result here is still the same. Very weird.

number 65
number 60



The only things I can think of are the format of the file (mine's ANSI. Tried saving it to Unicode, but it doesn't read anything then) is different here or...nah, I got nothing. You used those numbers in a file right? As mine reads as;

Scanner firstScanner = new Scanner(new File("infile2.txt")).useDelimiter("[,\n]");


I think the problem is the end of line character(s). I use a Unix editor which only uses \n (0x0a) as the end of line. I assume you used a Windows editor which use \n\r (0x0a 0x0d) as end of line. Try
		Scanner firstScanner = new Scanner(new File("data.txt")).useDelimiter("[,\\s]+");


\\s is whitespace which includes \n and \r
Was This Post Helpful? 0
  • +
  • -

#7 goodealsnow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-July 09

Re: Trying to read integers using the Scanner class

Posted 14 August 2009 - 12:34 PM

good job it works perfect after saving it as unicode.
But how to go about reading files with numbers like

1.55523,1899,1.899
2.344,29999.192,22

The asnwer is:
replace hasNextInt() for hasNextDouble()
replace nextInt() for nextDouble()

you also have to put double i = ....

This post has been edited by goodealsnow: 14 August 2009 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10369
  • View blog
  • Posts: 38,408
  • Joined: 27-December 08

Re: Trying to read integers using the Scanner class

Posted 14 August 2009 - 12:41 PM

Try reading in each line, and parsing it yourself. Like so:

String x = scan.nextLine(); 
int[] temp = parseLine(x);

public int[] parseLine(String x){
  x.replace("," , " "); //replace commas w/Strings
  StringTokenizer y = (StringTokenizer)x;
  int[] temp = new int[y.countTokens()];

  for(int i = 0; y.hasMoreTokens && i < temp.length; i++){
	  temp[i] = Integer.parseInt(y.nextToken());
	 }
}



Note that you will need the StringTokenizer class, which is in the java.util package. Hope this helps. Good luck!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Trying to read integers using the Scanner class

Posted 14 August 2009 - 04:42 PM

OK for the Nth time
When reading specific data type from a file using a Scanner you have to also read the <end of line>
So always read the full line as a String then read the specific datatype from that line using another Scanner
while(fileScanner.hasNext()) {
	String line = fileScanner.nextLine();
	Scanner lineScanner = new Scanner(line), ",";
	while(lineScanner.hasNext()) {
	  int x = lineScanner.nextInt();
	  ....
	}
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1