简体   繁体   中英

How to fix this exception mismatch error?

Introduction:

I am trying to create a controllable loop for a program and I use a flag for such thing. Although redundant to the question, the program takes any number and says if it is integer or decimal, if decimal shows up the decimal and float part.

At the bottom of it, I manage the while's flag . If true, the loop restarts, if false, end the program.

Problem: If I input n or N, it does what it has to do. But, if I input s or S. It does not.

I used:

My try at not using that many if statements in the next:

         bool = !(scan.hasNext("N") || scan.hasNext("n"));
         bool = (scan.hasNext("S") || scan.hasNext("s"));

The full code if someone has a better solution or helps anyone:

public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);


    int ent = 0;
    double dec = 0;
    boolean bool = true;



    while(bool == true){

    System.out.print("Introduce un numero: ");
    if (scan.hasNextInt() == true)
    {
        ent = scan.nextInt();
        System.out.println("El numero es entero");
    }
    else {dec = scan.nextFloat();
          System.out.println("El numero es decimal");

          //System.out.print();


        String realNumber = Double.toString(dec); 

        String[] mySplit = realNumber.split("\\.");

        BigDecimal entero = new BigDecimal(mySplit[0]);
        BigDecimal real = new BigDecimal(realNumber);
        BigDecimal fraction = real.subtract(entero);

        System.out.println(String.format("Entero : %s\nDecimales: %s", entero.toString(),fraction.toString().substring(0,4)));
    }  

         System.out.println("Quieres continuar? S/s o N/n"); 
         bool = !(scan.hasNext("N") || scan.hasNext("n"));
         bool = (scan.hasNext("S") || scan.hasNext("s"));


    }
    }
}

I expect that if I input s or S. It starts again asking me a number not "java.util.InputMismatchException"

You're using this form of hasNext() :

/**
 * Returns true if the next token matches the pattern constructed from the
 * specified string. The scanner does not advance past any input.
 *
 * <p> An invocation of this method of the form <tt>hasNext(pattern)</tt>
 * behaves in exactly the same way as the invocation
 * <tt>hasNext(Pattern.compile(pattern))</tt>.
 *
 * @param pattern a string specifying the pattern to scan
 * @return true if and only if this scanner has another token matching
 *         the specified pattern
 * @throws IllegalStateException if this scanner is closed
 */
public boolean hasNext(String pattern)  {
    return hasNext(patternCache.forName(pattern));
}

which is used to get input with a specific Pattern .
You only want to get the user's response as "S" or "N", so use nextLine() :

    System.out.println("Quieres continuar? S/s o N/n");
    boolean gotit = false;
    while (!gotit) {
        String response = scan.nextLine().toLowerCase().trim();
        bool = response.equals("s");
        gotit = (response.equals("n") || response.equals("s"));
    }

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