11 Replies - 1436 Views - Last Post: 14 February 2012 - 01:02 PM Rate Topic: -----

#1 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

best way to read in text file?

Posted 14 February 2012 - 10:46 AM

So I'm pretty new to Java and I need to read in a text file. The format of the file is
B = 10
C = 9
D = 8



I'm going to be putting these values into HashMap putting the characters as the key and the numbers as the value. I've been reading about scanners but I don't know how exactly to implement them
Is This A Good Question/Topic? 0
  • +

Replies To: best way to read in text file?

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,536
  • Joined: 05-May 05

Re: best way to read in text file?

Posted 14 February 2012 - 10:49 AM

So what's your question? Use Scanner.nextLine to read in each line, and use the constructor with a File arg to create it. See the docs.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10668
  • View blog
  • Posts: 39,615
  • Joined: 27-December 08

Re: best way to read in text file?

Posted 14 February 2012 - 10:50 AM

Didn't you already use a Scanner in one of your older projects? And this one?
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7884
  • View blog
  • Posts: 13,398
  • Joined: 19-March 11

Re: best way to read in text file?

Posted 14 February 2012 - 10:54 AM

View Postmastapro, on 14 February 2012 - 12:46 PM, said:

I've been reading about scanners but I don't know how exactly to implement them


That's interesting to know. Do you have a question?
Was This Post Helpful? 1
  • +
  • -

#5 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: best way to read in text file?

Posted 14 February 2012 - 10:59 AM

View Postmacosxnerd101, on 14 February 2012 - 10:50 AM, said:

Didn't you already use a Scanner in one of your older projects? And this one?


This is both me and my brother's accounts. But yea he said that it wasn't the same format as mine. This is what I wrote so far from what I found online
class Program9 {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileReader("test.txt"));
        Hashtable<String,Double> map = new Hashtable<String,Double>();   
        while (scanner.hasNextLine()) {
            String[] columns = scanner.nextLine().split(" = ");
            map.put(columns[0], columns[1]);
        }
        System.out.println(map);
    }
}



for some reason netbeans is saying there is no suitable method found for put on line 7
Was This Post Helpful? 0
  • +
  • -

#6 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1155
  • View blog
  • Posts: 2,536
  • Joined: 05-May 05

Re: best way to read in text file?

Posted 14 February 2012 - 11:04 AM

Your trying to send a String where a Double is expected.

  map.put(columns[0], Double.parseDouble(columns[1]));

Was This Post Helpful? 2
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7884
  • View blog
  • Posts: 13,398
  • Joined: 19-March 11

Re: best way to read in text file?

Posted 14 February 2012 - 11:05 AM

Quote

for some reason netbeans is saying there is no suitable method found for put on line 7


That'll be because your map is <String, Integer> and you're putting <String, String>

EDIT: aaand I'm too slow

This post has been edited by jon.kiparsky: 14 February 2012 - 11:06 AM

Was This Post Helpful? 1
  • +
  • -

#8 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: best way to read in text file?

Posted 14 February 2012 - 11:06 AM

View Postblackcompe, on 14 February 2012 - 11:04 AM, said:

Your trying to send a String where a Double is expected.

  map.put(columns[0], Double.parseDouble(columns[1]));

ah ok yes that was it. thanks a lot
Was This Post Helpful? 0
  • +
  • -

#9 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: best way to read in text file?

Posted 14 February 2012 - 11:15 AM

now i'm getting this error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "A - B"


it compiles fine though
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7884
  • View blog
  • Posts: 13,398
  • Joined: 19-March 11

Re: best way to read in text file?

Posted 14 February 2012 - 11:17 AM

"A - B" is not a number, so you can't parse it to an int.
Was This Post Helpful? 0
  • +
  • -

#11 mastapro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 133
  • Joined: 19-September 11

Re: best way to read in text file?

Posted 14 February 2012 - 11:24 AM

View Postjon.kiparsky, on 14 February 2012 - 11:17 AM, said:

"A - B" is not a number, so you can't parse it to an int.



oh wow i'm an idiot.. so apparently one of the keys is C and the value for it is A-B.... i'm not even sure how i'm supposed to do that. I'm guessing I have to somehow get the values from A and B and perform the operation then plug it back into C? Or is there some other way in java?
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,768
  • Joined: 20-September 08

Re: best way to read in text file?

Posted 14 February 2012 - 01:02 PM

You can probably use http://docs.oracle.c...Properties.html

Properties.load is what you want. Properties is already a Map of keys to values

Using ResourceBundle makes it even easier. If you put your original posting into resource 'x.properties':

import java.util.ResourceBundle;

public class KeyVal {
    public static void main(String[] args) {
        ResourceBundle b = ResourceBundle.getBundle("x");
        System.out.println(b.getString("B"));
    }
}

This post has been edited by g00se: 14 February 2012 - 01:08 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1