简体   繁体   中英

Having Java be able to read one line from a text file and print it

With this, I'm trying to figure out a way in order to have the program read from my "grades.txt" text file and print each line one by one when the input is matched from the user. So for example, if the grade entered is "93" it will print out "A" from the text file.

I have tried the fileByteStream , BufferedReader , readAllLines , and Scanner . I did end up being able to print the "grades.txt" but not the actual content of the file.

Here is my code:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Scanner;

public class Main {

    private static Object Paths;

    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);
        Scanner txtscanner = new Scanner(new File("grades.txt"));


        //grades {0.0, 60.0, 70.0, 77.0, 80.0, 83.0, 87.0, 90.0, 93.0, 100.0};
        //letter grades {"F", "D", "C", "C+", "B-", "B", "B+", "A-", "A"};

        while (txtscanner.hasNextLine()) {
            System.out.println("What is your grade?");
            int grade = scnr.nextInt();

            if (grade > 100) {
                System.out.println("Invalid input");
                System.out.println("Please enter another number");
            }



            else if ((grade >= 93.00 && grade < 100)) {
                String line9 = Files.readAllLines(Paths.get("file.txt")).get(9);
                System.out.println(9);
                break;

            }
            else if (grade >= 90.0 && grade < 93) {
                System.out.print("Your grade is ");
                break;
            }
            else if (grade >= 87.0 && grade < 90) {
                System.out.print("Your grade is ");
                break;
            }
            else if (grade >= 83.0 && grade < 87) {
                System.out.print("Your grade is ");
                break;
            }
            else if (grade >= 80 && grade < 83) {
                System.out.print("Your grade is ");
                break;
            }
            else if (grade >= 77 && grade < 80) {
                System.out.print("Your grade is ");
                break;
            }
            else if (grade >= 70 && grade < 77) {
                System.out.print("Your grade is ");
                break;
            }
            else if (grade >= 60 && grade < 70) {
                System.out.print("Your grade is ");
                break;
            }
            else if (grade <= 59 && grade > 0) {
                System.out.print("Your grade is ");
                break;// write your code here
            }
        }
    }
}

You don't put a break to end an if clause, the completed curly braces take care of that. The breaks that you have added are causing it to pop out of the while loop. Since you have one on every path of your if .. else if .. else, then you will never process more than the first line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM