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 4 of 4

Thread: Read text file, Prompt User to input name and List the year and rank for the name from the data set

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Read text file, Prompt User to input name and List the year and rank for the name from the data set

    Hello,
    This is my text file:

    |RANK| 1950| 1994| 1998|
    | 1| JIN| KIM| JOHN|
    | 2| JIM| KIM| RON|
    | 3| FRED|CRAIG| JIN|
    | 4| JOHN| MARK| DON|

    I would like the program to output as follows:

    Enter name: > John

    Year (1998) Ranked 1

    Year (1950) Ranked 4

    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.util.ArrayList;
     
    public class JavaApplication1 {
     
        public static void main(String[] args) {
            //file instance
            java.io.File file = new java.io.File("boys-names.txt");
     
            try{
     
            Scanner input = new Scanner(file);
            //create scanner object
            Scanner in = new Scanner(System.in);
            System.out.print("Enter name:> ");
     
            String myname = in.nextLine(); //read user input
     
            ArrayList<String> yearList = new ArrayList<>();
            ArrayList<String> rankList = new ArrayList<>();
            //populate both arraylist from file        
     
            ArrayList<String> rows = new ArrayList<>();
            ArrayList<ArrayList<String>> cols = new ArrayList<ArrayList<String>>();
            //Populate the matrix of names from file into cols;
     
            String[] fullData;
            while(input.hasNextLine()){                             
            //split data 
            fullData = input.nextLine().split("\\|");
     
            for(int x=0; x<rows.size(); x++)
            for(int y =0; y<cols.get(x).size(); y++){
            String currName = rows.get(x);
            if(currName.equalsIgnoreCase(myname))
                System.out.println("Year (" + yearList.get(y) + ") Ranked " + rankList.get(x));
            }                
            }
     
            }catch(FileNotFoundException e){
                System.out.println("Error the file cannot be found");
            }
        }    
    }

    This is what I've done so far but all I'm getting is build successful and nothing about the text file gets printed so something is definitely wrong. Please help!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read text file, Prompt User to input name and List the year and rank for the name from the data set

    nothing about the text file gets printed
    What is shown on the console when the program is executed? Please copy the full contents of the console and paste it here.

    Add some more print statements to the program to show where it is executing and to show the values of variables as their contents are changed.
    For example print out the value of fullData after a line is read into it. Use the Arrays class's toString method:
    System.out.println("fullData= "+ java.util.Arrays.toString(fullData));
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Read text file, Prompt User to input name and List the year and rank for the name from the data set

    Hey, This is what happens when I run the file
    Untitled.jpg

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Read text file, Prompt User to input name and List the year and rank for the name from the data set

    Please copy the contents and paste it here. No images. Text can not be copied from an image to include in a response.
    Most images are too small to read.

    Did you add the print statements I suggested? I don't see any print outs in that image.
    Add several print statements, say one every 3 statements to show where the code is executing.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to read different data segments from a text file?
    By alutchman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 18th, 2013, 01:35 PM
  2. Trying to input a text file but the program won't read it.
    By Spanky_10 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 27th, 2013, 11:50 AM
  3. Allow user input to edit text file??
    By dannyyy in forum Java Theory & Questions
    Replies: 2
    Last Post: April 6th, 2011, 06:53 AM
  4. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  5. Read data from text file
    By yroll in forum Algorithms & Recursion
    Replies: 4
    Last Post: December 31st, 2009, 01:40 AM