Results 1 to 9 of 9
  1. #1
    dav9999 is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default [SOLVED] getting values from a text file

    I have a great big text file that looks like this:

    jackson=4000000
    monroe=70000000
    lincoln=4777749
    etc....

    i'm trying to get java to return the value after the = sign.

    So if my program asks "Which street value are you interested in?" and let's say they type in jackson, it will return the value 4000000.


    I've tried a bunch of different methods but I can't seem to get it to work.

  2. #2
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Hello Dav9999 and welcome to the Java Forums.

    I suggest using the Java Split function and storing each value into an Array so they can be called later.

    Do you understand or do you need help further?
    Did this post help you? Please me! :cool:

  3. #3
    dav9999 is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default it is a huuuuge text file...

    it has about 3000 lines of name=value in it. can i still do that?

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.io.*;
    
    public class SplittingText {
        public static void main(String[] args) {
            String target = "lincoln";
            String path = "splittingText.txt";
            try {
                File file = new File(path);
                BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                    new FileInputStream(file)));
                String line;
                while((line = br.readLine()) != null) {
                    if(line.indexOf(target) != -1)
                        break;
                }
                String[] s = line.split("\\=");
                System.out.println("value for " + target +
                                   " is: " + s[1]);
                br.close();
            } catch(IOException e) {
                System.out.println("read error: " + e.getMessage());
            }
        }
    }
    splittingText.txt
    Java Code:
    jackson=4000000
    monroe=70000000
    lincoln=4777749

  5. #5
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    Yeah Java can handle that no problem.

    Ah HardWired you love it!! ;)
    Did this post help you? Please me! :cool:

  6. #6
    dav9999 is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default

    hardwired, thanks I see how that works now, but can the String target be taken from user input?

  7. #7
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Thumbs up

    Yeah Dav it can...

    Java Code:
    System.out.println("Which Street?:");
    	      
    // Opens up standard input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    String target = null; 
    	      
    // Read the Street name from the command-line
    try { 
    target = br.readLine(); 
    } catch (IOException ioe) { 
    System.out.println("IO error reading command line input");
    System.exit(1); 
    }
    Did this post help you? Please me! :cool:

  8. #8
    dav9999 is offline Member
    Join Date
    Mar 2008
    Posts
    4
    Rep Power
    0

    Default

    how would I include this code in the above?? I tried it like this, but it didn't work

    import java.io.*;

    public class SplittingText {
    public static void main(String[] args) {
    System.out.println("Which Street?:");

    // Opens up standard input
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String target = null;
    String path = "splittingText.txt";
    try {
    target = br.readLine();
    } catch (IOException ioe) {
    System.out.println("IO error reading command line input");
    System.exit(1);
    }
    String[] s = line.split("\\=");
    System.out.println("value for " + target +
    " is: " + s[1]);
    br.close();
    } catch(IOException e) {
    System.out.println("read error: " + e.getMessage());
    }
    }

  9. #9
    Bluefox815 is offline Member
    Join Date
    Feb 2008
    Location
    Oregon, USA
    Posts
    49
    Rep Power
    0

    Default

    this is how.

    Java Code:
    import java.io.*;
    
    public class SplittingText {
        public static void main(String[] args) {
            // initialization
            BufferedReader bi = new BufferedReader(new InputStreamReader(System.in)); 
            String target = null;
            String path = "splittingText.txt";
            try {
                File file = new File(path);
                BufferedReader br = new BufferedReader(
                                    new InputStreamReader(
                                    new FileInputStream(file)));
                // get user input
                System.out.println("Which street?:");
                try { 
                    target = bi.readLine(); 
                } catch (IOException ioe) { 
                    System.out.println("IO error reading command line input");
                    System.exit(1); 
                }
                String line;
                while((line = br.readLine()) != null) {
                    if(line.indexOf(target) != -1)
                        break;
                }
                String[] s = line.split("\\=");
                System.out.println("value for " + target +
                                   " is: " + s[1]);
                br.close();
            } catch(IOException e) {
                System.out.println("read error: " + e.getMessage());
            }
        }
    }
    Also, since you're new, if you were wondering how to get the above box use [/code] and [code] only reversed the other way (Mine are backwards so there isn't a box)
    Last edited by Bluefox815; 04-01-2008 at 02:58 AM.

Similar Threads

  1. [SOLVED] Reading a text file into an Array
    By DonCash in forum New To Java
    Replies: 13
    Last Post: 01-25-2011, 01:51 AM
  2. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 05:01 PM
  3. [SOLVED] How to read a file and compare Array values
    By DonCash in forum Advanced Java
    Replies: 2
    Last Post: 04-02-2008, 03:22 PM
  4. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  5. Converting text file(.txt) to JPG file(.jpg) in java
    By javadeveloper in forum Advanced Java
    Replies: 0
    Last Post: 11-09-2007, 05:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •