简体   繁体   中英

infinite while loop; input validation; don't know what to do to make it read an if before input is placed what should I do?

I'm pretty new at programming so sorry for bad explanations and/or badly written code...

Anyways, i'm currently working on an assignment using " method overload " and I want to be able to get a double or an int from the user.

My first code worked, so I decided to add an else to the end of the last if statement that checked for bad input and reiterated the entire code. The problem is that while the code works fine when you put numbers the first time, if you put in bad input it will start the while loop and keep re-iterating the code block regardless of any input. I know that it is just ignoring the if statements because their is no previous input of any sort(or anything stopping it?). I need some way of the code checking getting the input and then checking whether it is an integer or double before declaring the variable type...Or if there is some better method please share... if you need any more knowledge reply to this post or whatever...here Also, its not finished, call it a draft if you

public class LoanRates {
    public static void main(String[] args){

        Scanner in = new Scanner(System.in);
        boolean x = false;
        while(x = true){
            System.out.println("please enter a number");
            x = false;
            if (in.hasNextInt()){
                int rate = 0;           
                rate = getNumbers(rate, in);
                System.out.println(rate);
                break;
            } else if( in.hasNextDouble()){
                double rate = 0;
                System.out.println("please enter a number");
                rate = getNumbers(rate, in);
                System.out.println(rate);
                break;
            }else {                 
            System.out.print("please enter a number");}         
        }   
        System.out.print("did it work?");
    }

    public static double getNumbers(double rate, Scanner in){
        rate =in.nextDouble();
        return rate;
    }

    public static int getNumbers(int rate, Scanner in){
        rate = in.nextInt();
        return rate;
    }
}

You are using while(x = true) then it is always true

first you must initialize x variable to true . Otherwise it will not go inside the while loop boolean x = true;

try while(x == true) and make sure to change the value of variable x to false when it is necessary Try like this. But it is better to pass the double or int value rather than passing the scanner reference to the getNumbers methods

package test;

import java.util.Scanner;
import javax.swing.JOptionPane; 

public class LoanRates {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("Please enter a number");
            String input = in.next();
            try {
                int rate = Integer.parseInt(input);
                // you can use the rate variable and change your method to accept the input value
                System.out.println(getNumbers(rate));
                break;
            } catch (NumberFormatException e) {
                try {
                    double rate = Double.parseDouble(input);
                    // you can use the rate variable and change your method to accept the input value
                    System.out.println(getNumbers(rate));
                    break;
                } catch (NumberFormatException ex) {
                    System.out.println("Invalid number.");
                }
            }
        }
        System.out.print("did it work?");
    }

    public static double getNumbers(double rate) {
        // rate calculation logic
        return rate;
    }

    public static int getNumbers(int rate) {
        // rate calculation logic
        return rate;
    }
}

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