Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8274653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:47:19+00:00 2026-06-08T07:47:19+00:00

public class Calculator { public static void main(String[] args) { boolean isValid = false;

  • 0
public class Calculator
{

    public static void main(String[] args)
    {
        boolean isValid = false;
        Scanner myScanner = new Scanner(System.in);
        String customerType = null;
        System.out.print("Customer Type? (C/R) ");
        customerType = myScanner.next();
        while (isValid == false)
        {
            System.out.print("Enter Subtotal: ");

            if (myScanner.hasNextDouble())
            {
                double sobTotal = myScanner.nextDouble();
                isValid = true;
            }
            else
            {
                System.out
                        .println("Hay! Entry error please enter a valid number");
            }
            myScanner.nextLine();
        }
    }
}

Hi, im new to java, as usual im trying out a few things in the Scanner Class.

is there a way to see the input of the scanner?
Cuz i have a problem here with the code above as you’ll see. this is the output of my console window after i entered wrong data. instead of numbers i entered KKK so can somebody explain me why i got this error message 2 times?

"this is the console" 
Customer Type? (C/R) R
Enter Subtotal: KKK
Hay! Entry error please enter a valid number
Enter Subtotal: Hay! Entry error please enter a valid number
Enter Subtotal: 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:47:20+00:00Added an answer on June 8, 2026 at 7:47 am

    The problem is you’re calling scanner.nextdouble() which will work fine if and when there is a double in the input. When there is not a double in the input your input that was “KKK” is ignored by the call to nextDouble() and your error message is displayed then when you call nextLine() that same input “KKK” is still there waiting wo when you loop back throught the while the “KKK” is then passed back to your program and since it is still not a double you get the repeated error message.

    Try this:

        boolean isValid = false;
        Scanner myScanner = new Scanner(System.in).useDelimiter("\\n");
        String customerType = null;
        System.out.print("Customer Type? (C/R) ");
        customerType = myScanner.next();
        while (!isValid)
        {
            System.out.print("Enter Subtotal: ");
    
            if (myScanner.hasNextDouble())
            {
                double sobTotal = myScanner.nextDouble();
                isValid = true;
            }
            else
            {
                System.out.println("Hay! Entry error please enter a valid number");
    
                if(myScanner.hasNext()){
                  myScanner.next();
                }
            }
        }
    

    That will consume the invalid input of “KKK” and let the program continue properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Test { public static void main(String[] args) { DemoAbstractClass abstractClass = new
using System; namespace area { class Program { static void Main(string[] args) { double
public class fraktal extends JFrame { public fraktal (String args[]) { calc = new
My main class displays a JMenuBar. This JMenuBar is managed from calculator.ui.MenuBar. public JMenuBar
public class A { private A(int param1, String param2) {} public static A createFromCursor(Cursor
public class MyClass { public string MyProperty{ get; set; } Now, I would like
public class Knowing { static final long tooth = 343L; static long doIT(long tooth)
public class LinkedList { Object contents; LinkedList next = null; public boolean equals(Object item)
I'm trying to calculate the average of a student's marks: import java.util.Scanner; public class
I have this code but it doesn't work! public class Trial { public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.