Java扫描程序限制整数< 1&字符串输入

时间:2015-03-22 23:59:17

标签: java java.util.scanner

试图找出如何让我的程序限制小于1的整数输入,并限制扫描仪中字符串的输入。这是我的代码:

import java.util.Scanner; // Import scanner object
import java.io.*; // Import for file and IOException

public class Distance {

    public static void main(String[] args) throws IOException {

        int distance;
        int speed, time;
        String filename;

        System.out.println("Welcome to Distance Calculator.");

        // Create a scanner keyboard for user input
        Scanner keyboard = new Scanner(System.in);

        // Vehicle speed
        System.out.print("Vehicle speed (MPH): ");
        speed = keyboard.nextInt();
        while (!keyboard.hasNextInt()) {
            System.out.print("Please enter a valid #: ");
            speed = keyboard.nextInt();
            if (speed < 1) {
                System.out.print("Please enter a # greater then 1: ");
                keyboard.nextInt();
            }
        }

        System.out.print("Time vehicle traveled (HR): ");
        while (!keyboard.hasNextInt()) {
            time = keyboard.nextInt();
            if (time < 1) {
                System.out.print("Please enter a valid time: ");
                speed = keyboard.nextInt();
            }
        }

        time = keyboard.nextInt();

        keyboard.nextLine(); // Consume next line

        // Get filename
        System.out.print("File name for saving: ");
        filename = keyboard.nextLine();

        // Open file
        String filePath = "C:/Users/Nik/Desktop/";
        PrintWriter outputFile = new PrintWriter(filePath + filename);

        outputFile.println("Hour        Distance Traveled");
        outputFile.println("-----------------------------");

        for (int hour = 1; hour <= time; hour++) {
            distance = (speed * hour);
            outputFile.println(hour + "\t\t\t" + (distance + " Mi"));
        }
        outputFile.close();
        System.out.print("Date written to " + filePath + filename);
    }
}

真的很感激帮助。

3 个答案:

答案 0 :(得分:0)

我认为这应该有效:

System.out.print("Vehicle speed (MPH): ");
speed = -1;
do {
    System.out.println("Please enter a valid integer greater than 1");
    if (keyboard.hasNextInt() {
        speed = keyboard.nextInt();
    }
} while (speed < 1)

我很确定问题来自你的while循环中的!,但是我想我已经成功清理了代码。

免责声明:我没有测试过这段代码,看它是否有效,但是我想我会试一试,希望有所帮助。

答案 1 :(得分:0)

嗯,我相信改变&#34; whiles&#34;有点像这样可能会像你想要的那样工作。当插入不是整数的东西时,它会处理输入问题,当插入非正整数时,它会处理正整数问题。我对它进行了测试,我觉得它应该如何应用。尝试一下。

public static void main(String[] args) throws IOException{
    int distance;
    int speed, time;
    String filename;

    System.out.println("Welcome to Distance Calculator.");

    // Create a scanner keyboard for user input
    Scanner keyboard = new Scanner(System.in);

    // Vehicle speed
    System.out.print("Vehicle speed (MPH): ");


    while (!keyboard.hasNextInt() || 
            ((speed = keyboard.nextInt()) < 1) ) {
        System.out.print("Please enter a valid #: ");
        keyboard.nextLine();
    }

    System.out.print("Time vehicle traveled (HR): ");

    while (!keyboard.hasNextInt() || 
            ((time = keyboard.nextInt()) < 1) ) {
        System.out.print("Please enter a valid #: ");
        keyboard.nextLine();
    }

    keyboard.nextLine(); // Consume next line

    // Get filename
    System.out.print("File name for saving: ");
    filename = keyboard.nextLine();

    // Open file
    String filePath = "C:/Users/Nik/Desktop/";
    PrintWriter outputFile = new PrintWriter(filePath + filename);

    outputFile.println("Hour        Distance Traveled");
    outputFile.println("-----------------------------");

    for (int hour = 1; hour <= time; hour++) {
        distance = (speed * hour);
        outputFile.println(hour + "\t\t\t" + (distance + " Mi"));
    }
    outputFile.close();
    System.out.print("Date written to " + filePath + filename);
}

答案 2 :(得分:0)

我的最终代码:

int distance;
        int speed = 0, time;
        String filename;
        boolean validInput = false; // Boolean for validating scanner input

        System.out.println("Welcome to Distance Calculator.");

        // Create a scanner keyboard for user input
        Scanner keyboard = new Scanner(System.in);

        // Vehicle speed
        System.out.print("Vehicle speed (MPH): ");

        // Method for validating user input
        while (validInput == false) {
            if (!keyboard.hasNextInt()) { // check if keyboard scanner !integer
                System.out.print("Please enter a valid #: "); // prompts user for valid input
                keyboard.nextLine(); // consumes next line
            }
            else {
                speed = keyboard.nextInt();
                if (speed < 1) { // validates if speed > 0
                    System.out.print("Please enter a value greater then 1: "); // prompts user for valid speed
                    keyboard.nextLine(); // consumes next line
                }
                else validInput = true; // if statements are passed then set bool to True and end loop
            }
        }