lb1021
lb1021

Reputation: 123

Printing contents of file based on user input

Trying to print a file based off the user's input as mentioned in the title. Basically, my program has been altered from one that I previously created which reads data from a file, so I know that the file has been imported correctly (not the problem).

The problem I have is that I'm trying to make the program print the entirety of the .txt file if the user chooses a specific number, in this case '1'. My current code so far is:

import java.io.FileReader;
import java.util.Scanner;

public class InputOutput {
    public static void main(String[] args) throws Exception {
        // these will never change (be re-assigned)
        final Scanner S = new Scanner(System.in);
        final Scanner INPUT = new Scanner(new FileReader("C:\\Users\\JakeWork\\workspace\\Coursework\\input.txt"));

        System.out.print("-- MENU -- \n");
        System.out.print("1: Blahblahblah \n");
        System.out.print("2: Blahblahblah \n");
        System.out.print("Q: Blahblahblah \n");
        System.out.print("Pick an option: ");

        if (S.nextInt() == 1) {
            String num = INPUT.nextLine();
            System.out.println(num);
        }

I feel as if my if statement is totally off and I'm heading in the entire wrong direction, could anyone point me in the right and give me a helping hand?

Upvotes: 2

Views: 282

Answers (3)

user1231232141214124
user1231232141214124

Reputation: 1349

You're close, but not quite there.

You a reading the user input correctly, but now you need the file contents in a loop.

if(S.nextInt() == 1) {
    while (INPUT.hasNextLine()) {
        System.out.println(INPUT.nextLine());
    }
}

This will keep looking as long as the file contents hasNextLine

You can safely remove the String option = S.next();

Also, just a small bit of naming convention nitpicking, don't use all upper case letters for variable names unless they are meant to be static. Also, the first letter of a variable is generally lower case.

Upvotes: 2

Richard Tyregrim
Richard Tyregrim

Reputation: 597

Also, for menu scenarios like this, consider using a switch statement, then place a call to the menu-printing (that you move to a separate method) in the default case, so that if you enter something wrong, you can reprint the menu choices. Also, the switch statement is more readable (arguably) than a bunch of if's, like this:

int option = S.nextInt();

    switch(option) {
        case 1 : 
            while(INPUT.hasNextLine()) {
                System.out.println(INPUT.nextLine());
            }
           break; 

        case 2 :
            //Do stuff
            break; 

        default :
            //Default case, reprint menu?

    }
}

Upvotes: 0

Ramanlfc
Ramanlfc

Reputation: 8354

  if (S.nextInt() == 1) {
    // check if there is input ,if true print it
    while((INPUT.hasNextLine())
            System.out.println(INPUT.nextLine());

     }

Upvotes: 2

Related Questions