7 Replies - 6230 Views - Last Post: 27 November 2010 - 10:42 PM Rate Topic: -----

#1 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Scanner class to read the file

Posted 27 November 2010 - 01:50 PM

Hi, i have a question regarding reading integer from the file where there are also characters.

The sample file might look like this:

height = 234,
goals = 23,
defeats = 4,
numbers = 22,

So im using nested Scanners. One to read the line and second one to read that particular line BUT how can i get the numbers out. The Scanner class methods hasNextInt() does not seem to work. Any feedback welcomed.

Is This A Good Question/Topic? 0
  • +

Replies To: Scanner class to read the file

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Scanner class to read the file

Posted 27 November 2010 - 02:47 PM

what do you mean by nested Scanners?
can you post your code? might be easier to debug :)
Was This Post Helpful? 0
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 11,469
  • Joined: 20-September 08

Re: Scanner class to read the file

Posted 27 November 2010 - 02:52 PM

You could read that into a Properties instance, although you'd need to swallow the commas
Was This Post Helpful? 0
  • +
  • -

#4 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Scanner class to read the file

Posted 27 November 2010 - 03:00 PM

I think the easiest way of doing this would be to use regex. Well this would be the approach I would take and maybe some others on here. Below is a simple template to get the numbers for you - it is incomplete. You certainly wont need two scanners, valuable memory will be wasted plus it's not really good for two Scanners to access the same file at the same time.

To get the other numbers, you will have to include a while loop which I will leave for you to do; use the concept I am using with replaceAll() to get the 'height', 'goals' and etc.. from the text file. Hint - have a look at trying to replace each character which is not a word character with blank. This may be of use - Java Regex Cheat Sheet, which I advise to bookmark :).

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;


public class template {

	public static void main (String [] args){

		//Initialise variables
		Scanner s = null;
		String line = "";
		int num = 0;
		//Read file with Scanner
		try {
			s = new Scanner(new File("sample"));
		//If file not found, throw exception.
		} catch (FileNotFoundException e) {

			e.printStackTrace();
		}
		// Now read each line if its available.
		if(s.hasNextLine()){

			line = s.nextLine();
		}
		/*Replace each non digit with "" and then typecast
		to an integer if we can.*/
		try {
			num=Integer.parseInt(line.replaceAll("[^0-9]", ""));
			/*If we can't, throw an exception. */
		} catch (NumberFormatException e){
			System.err.println("Cannot cast");
		}
		//Simply print.
		System.out.println(num);

	}
}


Was This Post Helpful? 1
  • +
  • -

#5 darek9576  Icon User is offline

  • D.I.C Lover

Reputation: 198
  • View blog
  • Posts: 1,689
  • Joined: 13-March 10

Re: Scanner class to read the file

Posted 27 November 2010 - 03:03 PM

Sorry.. thats my code.


		File file = new File("a.txt");
		
		Scanner fileScanner = new Scanner(file);
		while(fileScanner.hasNextLine()){
			String line = fileScanner.nextLine();
			Scanner s = new Scanner(line);
			
			while(s.hasNextInt()){
				System.out.print(s.nextInt());
			}
		}
	}



Was This Post Helpful? 0
  • +
  • -

#6 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: Scanner class to read the file

Posted 27 November 2010 - 03:04 PM

@japanir,

I believe he means two instances of the Scanner class which I see quite a few people do which really is a bad idea.

   //Initialise variables
   Scanner s = null;
   Scanner s1 = null;
  //Read file with Scanner
   try {
	s = new Scanner(new File("sample"));
	s1 = new Scanner(new File("sample"));
        //...


Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2727
  • View blog
  • Posts: 11,469
  • Joined: 20-September 08

Re: Scanner class to read the file

Posted 27 November 2010 - 04:20 PM

Or instead of Properties, use ResourceBundle. Save the data as 'x.properties' and try the following:

import java.util.ResourceBundle;


public class CommaProps {
    private ResourceBundle bundle;

    public CommaProps() {
        bundle = ResourceBundle.getBundle("x");
    }

    public static void main(String[] args) {
        CommaProps cp = new CommaProps();
        System.out.println(cp.getProperty("goals"));
    }

    public String getProperty(String key) {
        String result = bundle.getString(key);

        return (result == null) ? null : result.substring(0, result.length() -
            1);
    }
}



This post has been edited by g00se: 27 November 2010 - 04:21 PM

Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

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

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

Re: Scanner class to read the file

Posted 27 November 2010 - 10:42 PM

View Postjapanir, on 27 November 2010 - 03:47 PM, said:

what do you mean by nested Scanners?
can you post your code? might be easier to debug :)

One Scanner to read lines from the file as String
One Scanner to parse the read lines

if all the lines are in the format
xxx = 123,
The easiest way is to read line by line using a Scanner
then use the String class split() method on " ,"
and then Integer.parseInt() on split[2]
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1