检测空输入(JAVA)

时间:2016-06-13 22:57:08

标签: java java.util.scanner

所以我的程序必须计算一个人在扫描仪方法中输入的更改量,但是,如果a)输入不是数字而b)如果缺少输入,则必须弹出两个错误。我对后者有很多麻烦。

import java.util.InputMismatchException;
import java.util.Scanner;
public class MakingChange 
{
public static void main(String[] args)
{

    Scanner input = new Scanner(System.in);  // Reading from System.in
    System.out.print("How much money do you have: ");

    double amountInDollars = 0;

        try {
                amountInDollars = input.nextDouble();
        } catch (InputMismatchException e) {
                System.out.println("INVALID"); //print invalid
        return;
        }

        if (input.equals(" ")){
            System.out.println("INVALID");
            return;
        }


        int amountInPennies = (int) Math.round(amountInDollars * 100);
        amountInPennies = (int) (Math.round(amountInPennies / 5.0)  * 5);

        //toonies
        int numberofToonies = (int)(amountInPennies / 200.00);
        amountInPennies = amountInPennies % 200;
        //loonies   
        int numberofLoonies = (int) (amountInPennies / 100.00);
        amountInPennies = amountInPennies % 100;
        //quarters
        int numberofQuarters = (int)(amountInPennies / 25.00);
        amountInPennies = amountInPennies % 25;
        //dimes      
        int numberofDimes = (int)(amountInPennies / 10.00);
        amountInPennies = amountInPennies % 10;
        //nickels  
        int numberofNickels = (int)(amountInPennies / 5.00);

System.out.println("toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels); 

}   
}

所以预期应该是“INVALID”但是目前,所有返回的都是一个空格。 谢谢!

4 个答案:

答案 0 :(得分:1)

检查以下代码,我添加了评论。

<string name="email">$message = \'&lt;center&gt;
   &lt;table width="100%" cellpadding="0" cellspacing="0"&gt;
   &lt;tr&gt;
  &lt;td align="center" bgcolor="#FFFFFF"&gt;
    &lt;table width="620px" cellpadding="0" cellspacing="0"&gt;
    &lt;tr&gt;
        &lt;td width="400px"  bgcolor="#000000"  style="color:#FFFFFF; text-

decoration:none; float:left;font-size:10px;margin: -18px 3px -1px 11px; font-weight:bold; padding:2px; text-decoration:none; padding-left:0px;"&gt;For Support &lt;span style="text-decoration:none; color:#FFFFFF;"&gt;enquiry@makeintern.com&lt;/span&gt;&lt;/td&gt;
&lt;td width="220px" bgcolor="#000000"&gt;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr style="background-color:#737373;"&gt;
&lt;td width="290" align="left" valign="middle"&gt;&lt;a href="http://makeintern.com" target="_blank"&gt;&lt;img src="http://www.makeintern.com/images/makeintern.jpg" width="136" border="0" alt="MakeIntern" style="display:block; padding-left:0px;"&gt;&lt;/a&gt;&lt;/td&gt;
&lt;td width="290" align="right" style="text-align:right; font-family:Arial, Segoe UI,Helvetica Neue, Helvetica, sans-serif; font-size:18px; line-height:18px; color:#FFFFFF; font-weight:normal; padding-right:10px;"&gt;Contact Form Enquiry !&lt;br&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td height="12px" colspan="2"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td colspan="2" align="center" style="line-height:16px; font-family:arial; font-size:12px; color:#888888; text-align:left;"&gt;
&lt;table cellspacing="0" cellpadding="0"&gt;
&lt;tr&gt;
        &lt;td width="200px" style="font-weight:bold;"&gt;Name: &lt;/td&gt;
        &lt;td width="100px"&gt;:&lt;/td&gt;
        &lt;td&gt;\'.$name.\'&lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr&gt;
        &lt;td width="200px" style="font-weight:bold;"&gt;Email: &lt;/td&gt;
        &lt;td width="100px"&gt;:&lt;/td&gt;
        &lt;td&gt;\'.$email.\'&lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr&gt;
        &lt;td width="200px" style="font-weight:bold;"&gt;Contact: &lt;/td&gt;
        &lt;td width="100px"&gt;:&lt;/td&gt;
        &lt;td&gt;\'.$mobile.\'&lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr&gt;
        &lt;td width="200px" style="font-weight:bold;"&gt;Message:  &lt;/td&gt;
        &lt;td width="100px"&gt;:&lt;/td&gt;
        &lt;td&gt;\'.$message.\'&lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr&gt;
        &lt;td width="200px" style="font-weight:bold;"&gt;Date-Time:  &lt;/td&gt;
        &lt;td width="100px"&gt;:&lt;/td&gt;
        &lt;td&gt;\'.$current_date.\'&lt;/td&gt;
        &lt;/tr&gt;


        &lt;/table&gt;

        &lt;br/&gt;&lt;br/&gt;

        Thanks and Regards &lt;br/&gt;
        MakeIntern Team &lt;br/&gt;
        Account Manager - MakeIntern Cell &lt;br/&gt;
        www.makeintern.com&lt;br/&gt;
        enquiry@makeintern.com&lt;br/&gt;&lt;br/&gt;
        &lt;b&gt;If any query Please call us on : 011-45544188&lt;/b&gt;&lt;br/&gt;
        &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
        &lt;td height="12px"&gt;&lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr&gt;
        &lt;td colspan="2"&gt;&lt;img src="http://www.myhotbooking.com/images/email-template/bar.png"&gt;&lt;/td&gt;
        &lt;/tr&gt;

        &lt;tr&gt;
        &lt;td colspan="2" bgcolor="#737373" height="90px"&gt;
        &lt;table cellpadding="0" cellspacing="0" width="620px"&gt;
        &lt;tr&gt;
        &lt;td height="10px"&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
        &lt;td align="center" style="color:#FFFFFF;" height="20px"&gt;&lt;a href="http://www.makeintern.com/"&gt;www.makeintern.com &lt;/a&gt;| &lt;span style="color:#FFFFFF; text-decoration:none;"&gt;enquiry@makeintern.com&lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
        &lt;td align="center" valign="middle"&gt;
        &lt;a href="https://www.facebook.com/makeinterns" target="_blank"&gt;&lt;img src="http://www.myhotbooking.com/images/email-template/icon-facebook.png" width="26" height="26" border="0" alt="Facebook"&gt;&lt;/a&gt;&lt;a href="https://twitter.com/makeintern" target="_blank"&gt;&lt;img src="http://www.myhotbooking.com/images/email-template/icon-twitter.png" width="26" height="26" border="0" alt="Twitter"&gt;&lt;/a&gt;&lt;a href="https://www.linkedin.com/company/makeintern" target="_blank"&gt;&lt;img src="http://www.myhotbooking.com/images/email-template/icon-linkedin.png" width="26" height="26" border="0" alt="Linkedin"&gt;&lt;/a&gt;&lt;a href="https://plus.google.com/+makeintern" target="_blank"&gt;&lt;img src="http://www.myhotbooking.com/images/email-template/icon-google-plus.png" width="26" height="26" border="0" alt="Google+"&gt;&lt;/a&gt;&lt;/td&gt;


        &lt;/tr&gt;
        &lt;tr&gt;
        &lt;td align="center" style="color:#FFFFFF;"&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;/table&gt;
        &lt;/td&gt;
        &lt;/tr&gt;
        &lt;/table&gt;
        &lt;/td&gt;
        &lt;/tr&gt;
        &lt;/table&gt;
        &lt;/center&gt;\'</string>

答案 1 :(得分:0)

试试这个,应该可以正常使用。

Scanner input = new Scanner(System.in);  // Reading from System.in
    System.out.print("How much money do you have: ");

    double amountInDollars = 0;
    String emp = input.nextLine();

    try {

        // if the user doesn't enter anything and press enter or entered value is empty
        if (emp.isEmpty() || emp.equals(" ")) { 
            System.out.println("Invalid input");
            return;
        }

    }catch (InputMismatchException e){
       //catch the message
        e.printStackTrace();
    }
    // if string entered is not double
    boolean isnum = emp.chars().allMatch(Character::isAlphabetic);

    if (!isnum) { // if is a Number then we pass it in
        amountInDollars = Double.parseDouble(emp);
    } else {
       // we pass exception message to catch
        System.out.println("Invalid Input: Double values only please");
        return;
    }

    if (amountInDollars < 0) { // if the value is 'Negative'
        System.out.println("INVALID");
        return;
    }


    int amountInPennies = (int) Math.round(amountInDollars * 100);
    amountInPennies = (int) (Math.round(amountInPennies / 5.0)  * 5);

    //toonies
    int numberofToonies = (int)(amountInPennies / 200.00);
    amountInPennies = amountInPennies % 200;
    //loonies
    int numberofLoonies = (int) (amountInPennies / 100.00);
    amountInPennies = amountInPennies % 100;
    //quarters
    int numberofQuarters = (int)(amountInPennies / 25.00);
    amountInPennies = amountInPennies % 25;
    //dimes
    int numberofDimes = (int)(amountInPennies / 10.00);
    amountInPennies = amountInPennies % 10;
    //nickels
    int numberofNickels = (int)(amountInPennies / 5.00);

    System.out.println("toonies:" + numberofToonies + ";" + " loonies:" + numberofLoonies + ";" + " quarters:" + numberofQuarters + ";" + " dimes:" + numberofDimes + ";" + " nickels:" + numberofNickels);

让我知道它是否成功。

答案 2 :(得分:-1)

据我所知,您正在测试您的扫描仪对象是否等于字符串文字,这完全没有意义。 首先,使用任何扫描程序的nextXXX - 方法获取字符串。 然后,使用someString.equals("")检查该字符串是否为空,或者我认为someString.isEmpty()更好。

答案 3 :(得分:-1)

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    BigDecimal money = getBigDecimal(sc); // When using scanner as parameter your performance increases because you don't create new scanners every time if you use the scanner for different purposes or within a recursive function.
    System.out.printf("You have %(,.2f dollars%n", money); // Just an example of your output.
}

public static BigDecimal getBigDecimal(Scanner sc) {
    System.out.print("How much money do you have? ");
    if (sc.hasNextBigDecimal()) { // if a BigDecimal value is typed
        return sc.nextBigDecimal();
    }
    sc.next(); // else ignore input
    System.out.println("Please, type amount.");
    return getBigDecimal(sc); // recursion
}

为了钱,你最好使用BigDecimal(相信我,或者不要相信我)。此外,您可以使用BigDecimal函数进行计算。

https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial http://www.tutorialspoint.com/java/math/java_math_bigdecimal.htm