Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Output some data from a txt file?

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Output some data from a txt file?

    I have an assignment at school in which i have to make a program that reads a "Product number" and outputs the "Product Name". Those two pieces of info is located in a txt file, separated by a "," like this:

    10002,iron
    10003,gold
    10003,cobber

    Right now I'm able to read the txt file and store it in an array and output the entire array. This is my 2 classes:

    FileData (main)
    package textfiles;
    import java.io.IOException;
     
    public class FileData {
     
     
    	public static void main(String[] args) throws IOException {
     
    		String file_name = "/Users/jack_sogaard/Desktop/store.txt";
     
    		try {
    			ReadFile file = new ReadFile(file_name);
    			String[] aryLines = file.OpenFile();
     
    			int i;
    			for ( i=0; i < aryLines.length; i++ ) {
    			System.out.println( aryLines[ i ] ) ;
    			}
    		}
    		catch (IOException e) {
    			System.out.println(e.getMessage() );
    		}
    	}
    }

    And ReadFile
    package textfiles;
    import java.io.IOException;
    import java.io.FileReader;
    import java.io.BufferedReader;
     
    public class ReadFile {
    	private String path;
     
    	public ReadFile(String file_path) {
    		path = file_path;
    	}
     
    	public String[] OpenFile() throws IOException {
     
    		FileReader fr = new FileReader(path);
    		BufferedReader textReader = new BufferedReader(fr);
     
    		int numberOfLines = readLines();
    		String[] textData = new String[numberOfLines];
    		int i;
     
    		for (i=0; i < numberOfLines; i++) {
    			textData[i] = textReader.readLine();
    		}
    		textReader.close();
    		return textData;
    	}
     
    	int readLines() throws IOException {
     
    		FileReader file_to_read = new FileReader(path);
    		BufferedReader br = new BufferedReader(file_to_read);
     
    		String aLine;
    		int numberOfLines =0;
     
    		while (( aLine = br.readLine()) !=null) {
    				numberOfLines++;
    		}
    		br.close();
    		return numberOfLines;
    	}	
    }

    Any help would be appreciated

    /jaze


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Output some data from a txt file?

    Sounds like a job for a Map to me!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Output some data from a txt file?

    Do you have any specific java programming questions?
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Output some data from a txt file?

    what is a Map?
    My specific question is: how should i do this? can i create a list of 2 arrays, one containing numbers, and the other containing names and then the the index of the product number (etc. [1]) is corresponding to the index of the product name?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,937
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

    Default Re: Output some data from a txt file?

    Map is a java interface that has several classes that implement it. See the API doc for details.
    Basically you store objects in a Map using a key and can retrieve them using that key. For example: If the number is the key, the name would be the value.
    Using one of the Map classes will be easier than using two "parallel" arrays.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Output some data from a txt file?

    Okay, i've been trying some different things now and gone through a lot of different tutorials on this subject, and yes, mapping is the method i will use.

    I've written a class that reads the txt file puts it into columns and then out prints the map. Then a user is able to input the product number, and the class should print out the product name, but it only prints out "null" - where did i go wrong?

    Here is my class:
    import java.io.*;
    import java.util.*;
     
     
    class Test {
        public static void main(String[] args) throws FileNotFoundException {
            Scanner scanner = new Scanner(new FileReader("store.txt"));
     
            //Create HashMap
            HashMap map = new HashMap();
     
            while (scanner.hasNextLine()) {
                String[] columns = scanner.nextLine().split(",");
                map.put(columns[0], columns[1]);
            }
     
            //Prints the map, just to check if its ok
            System.out.println(map);
     
            //Takes input from user (Product Number)
            System.out.println("input product number");
            Scanner prodNoScan = new Scanner(System.in);
            String prodNo = prodNoScan.nextLine();
     
            //Get the value from the key "prodNo" and print put product name
            Object productName = map.get("prodNo");
            System.out.println("The product is:" +productName);
     
        }
    }

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Output some data from a txt file?

    Hello j4ze!
    I think the problem lies in this line
    Object productName = map.get("prodNo");
    With the above statement you are searching for a key named prodNo which doesn't exist. You actually want to search for prodNo which is entered by the user (ie 10001, etc.). Can you see the difference between
    map.get("prodNo");
    and
    map.get(prodNo);

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,541
    My Mood
    Hungover
    Thanks
    145
    Thanked 648 Times in 553 Posts

    Default Re: Output some data from a txt file?

    Quote Originally Posted by j4ze View Post
    what is a Map?
    First hit for googling Java map: The Map Interface (The Java™ Tutorials > Collections > Interfaces)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Output some data from a txt file?

    Thank you, that helped a lot. Yeah it makes a whole lot of sense to leave out the " ".
    Once again, thank you.

Similar Threads

  1. File Output
    By Nuggets in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 3rd, 2012, 09:55 AM
  2. Input output file help
    By peteyfresh12 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 2nd, 2011, 08:44 AM
  3. Replies: 1
    Last Post: April 7th, 2011, 02:32 PM
  4. Replies: 8
    Last Post: March 25th, 2011, 03:34 PM
  5. Output an int i to a file
    By NightFire91 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 17th, 2010, 11:45 AM