Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How do I select lines from a file and output them?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
username123




PostPosted: Fri Nov 06, 2015 7:37 pm   Post subject: How do I select lines from a file and output them?

What the title says. Here's what I have so far:
Java:

Scanner input = new Scanner(System.in);
    String answer;
    String[] items;
    int categoryNumber = 7;
    int categoryLines = 4;
    File inventory = new File("inventory.txt");
    File monitors = new File("monitors.txt");
    File motherboards = new File("motherboards.txt");
    File cpus = new File("cpus.txt");
    File keyboards = new File("keyboards.txt");
    File harddrives = new File("harddrives.txt");
    File mice = new File("mice.txt");
    File cases = new File("cases.txt");
    File checkout = new File("checkout.txt"); //Checkout file
    File report = new File("report.txt"); //Report file
   
    System.out.println("Choose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
    answer = input.next();
       
    while(!answer.equals("N")||!answer.equals("n"))
    {
   
      if(answer.equals("Monitors")||answer.equals("monitors"))
      {
        //Displays the list of monitors
        BufferedReader br = new BufferedReader(new FileReader(monitors));
        String mon = null;
        while((mon = br.readLine()) != null)
        {
          System.out.println(mon);
        }
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }
      else if(answer.equals("Motherboards")||answer.equals("motherboards"))
      {
        //Displays the list of motherboards
        BufferedReader br = new BufferedReader(new FileReader(motherboards));
        String mb = null;
        while((mb = br.readLine()) != null)
        {
          System.out.println(mb);
        }
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }
      else if(answer.equals("CPUs")||answer.equals("cpus"))
      {
        //Displays the list of CPUs
        BufferedReader br = new BufferedReader(new FileReader(cpus));
        String cpu = null;
        while((cpu = br.readLine()) != null)
        {
          System.out.println(cpu);
        }
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }
      else if(answer.equals("Keyboards")||answer.equals("keyboards"))
      {
        //Displays the list of keyboards
        BufferedReader br = new BufferedReader(new FileReader(keyboards));
        String kb = null;
        while((kb = br.readLine()) != null)
        {
          System.out.println(kb);
        }
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }
      else if(answer.equals("HD/SSDs")||answer.equals("hd/ssds"))
      {
        //Displays the list of hard drives & solid state drives
        BufferedReader br = new BufferedReader(new FileReader(harddrives));
        String hdssd = null;
        while((hdssd = br.readLine()) != null)
        {
          System.out.println(hdssd);
        }
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }
      else if(answer.equals("Mice")||answer.equals("mice"))
      {
        //Displays the list of mice
        BufferedReader br = new BufferedReader(new FileReader(mice));
        String m = null;
        while((m = br.readLine()) != null)
        {
          System.out.println(m);
        }
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }
      else if(answer.equals("Cases")||answer.equals("cases"))
      {
        //Displays the list of computer cases
        BufferedReader br = new BufferedReader(new FileReader(cases));
        String c = null;
        while((c = br.readLine()) != null)
        {
          System.out.println(c);
        }
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }
      else if(answer.equals("N")||answer.equals("n"))
      {
        System.out.println("Enter the number of specific items you would like to buy.");
        int numItem = input.nextInt();
        for(int i = 0; i <= numItem; i++)
        {
          System.out.println("Enter the number of the item you would like to buy.");
          String itemNum = input.next();
         
        }
      }
      else
      {
        System.out.println("Invalid");
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }
    }

The files that are named after computer components are all identical to different 4-line sections of inventory.txt. I'm trying to replace the files with those sections.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Fri Nov 06, 2015 9:02 pm   Post subject: RE:How do I select lines from a file and output them?

The easiest way is to just load the entire file into memory line-by-line, storing each line in the correct variable. Read the entire monitors section and store it all in monitors variables. Then read the entire motherboards section and save it all in motherboards variables. Java also permits random access to files, which is a bit more involved, but you can read up on it here.
username123




PostPosted: Tue Nov 10, 2015 9:26 am   Post subject: Re: How do I select lines from a file and output them?

I tried another method by storing the lines in an array and displaying each section using for loops. When I run it, each line is replaced by the word "null".

Java:

Scanner input2 = new Scanner(inventory);
   
    for(int i = 0; i < 21; i++)
    {
      while(input2.hasNext())
      {
        lines[i] = input2.nextLine();
      }
    }
   
    System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
    answer = input.next();
   
    while(!answer.equals("N")||!answer.equals("n"))
    {   
      if(answer.equals("Monitors")||answer.equals("monitors"))
      {
        //Displays the list of monitors
        for(int i = 1; i < 4; i++)
        {
          System.out.println(lines[i]);
        }
        System.out.println("\nChoose a section between the following: monitors, motherboards, CPUs, keyboards, HD/SSDs, mice, and cases. Press N to stop browsing.");
        answer = input.next();
      }



inventory.txt
 Description:
Text file I used

Download
 Filename:  inventory.txt
 Filesize:  1.05 KB
 Downloaded:  151 Time(s)

Insectoid




PostPosted: Tue Nov 10, 2015 12:36 pm   Post subject: RE:How do I select lines from a file and output them?

For some reason, nextLine() isn't properly returning the string. According to the specification, the method should never return null and will throw exceptions if it doesn't find a line, so it's more likely that that line is never actually reached. Maybe input2.hasNext() never actually returns true.
username123




PostPosted: Tue Nov 10, 2015 5:02 pm   Post subject: Re: How do I select lines from a file and output them?

I'm pretty sure it does though. If I add System.out.println(lines[i]) after lines[i] = input2.nextLine, it works.
Insectoid




PostPosted: Tue Nov 10, 2015 5:10 pm   Post subject: RE:How do I select lines from a file and output them?

Okay, I've figured it out. Look at your for loop. On the first iteration, i = 0. Then you enter a while loop, which iterates over the entire file, repeatedly saving each line into lines[i]. i is still 0, so at the end of the while loop, lines[0] is given the last line in the file. lines[1] and up are all still unassigned, and therefore null. On the second iteration of the for loop, i=1. Then the while loop starts, and immediately exits, because you already got to the end of the file on the first iteration on the for loop. It does this 19 more times. When the for loop terminates, lines[0] contains the last line in the file, and lines[1] and up are still null.

I hope I explained myself well enough. The fix should be obvious if I made any sense.
username123




PostPosted: Tue Nov 10, 2015 5:42 pm   Post subject: RE:How do I select lines from a file and output them?

I got rid of the while loop, and it works perfectly now. Thanks a lot!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: