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 8626187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:57:53+00:00 2026-06-12T07:57:53+00:00

I have a class assignment to read in data using Scanner. import java.util.Scanner; public

  • 0

I have a class assignment to read in data using Scanner.

import java.util.Scanner;

public class Project23
{
    public static void main(String[] args)
    {
        // Declarations and instantiations.
        Scanner scan = new Scanner(System.in);
        String any = "";
        boolean more = false;
        double purchase = 0.0;

        // Ask if user would like to run program?
        System.out.print("Do you have any purchases? Y/N?: ");

        // Ready value into string.
        any = scan.nextLine();
        System.out.println();

        // If any is equal to y or Y it will set the value of more to true
        // this runs the while statement below.
        more = any.toUpperCase().equals("Y");

        // While more is still true continue to run the code within the brackets.
        while (more)
        {
            System.out.print("Please input purchase amount: ");
            purchase += scan.nextDouble();
            System.out.println();

            System.out.print("Do you have any more purchases Y/N?: ");
            any = scan.nextLine();
            System.out.println();

            more = any.toUpperCase().equals("Y");
        }

        if (purchase >= 2500)
            System.out.println("Purchase >= 2500");
        else
            System.out.println("Purchase < 2500");
    }
}

The bottom part is just there as a test for me to find out if everythign is running alright. However the while loop i have setup doesn’t seem to want to continue running more than once. It will take in one value, then if I say yes I have more values (y or Y) it will just exit and print either boobies

  • 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-12T07:57:54+00:00Added an answer on June 12, 2026 at 7:57 am

    Basically what is happenning is ,
    While you are reading double as scan.nextDouble(), you are reading only the double, however there would be ENDLINE character when you hit enter on the stream that is not read by scan.nextDouble().
    So when you reach the any1=scan.nextLine(), it reads the endline character which does not equal to Y or y. As a result it exits the while loop.
    correct your code like this, just make change one line where you are reading doubel :

    while(more){
    
        System.out.print("Please input purchase amount: ");
        purchase+=Double.parseDouble(scan.nextLine());
        System.out.println();
        System.out.print("Do you have any more purchases Y/N?: ");
        // scan.nextLine();
        String any1=scan.nextLine();
        System.out.println();       
        more = any1.equals("y") || any1.equals("Y");//Shortened :)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my second programming assignment in my Java class, we have to create a
I have a medium complex C++ class which holds a set of data read
I have an assignment in my introduction to Java class where we are suppose
Can I optimize the class assignment here at all? I have to do this
I have to do an assignment for my class and it says not to
I have completed the assignment (yes it is for a programming class), but I
As part of the last assignment in a beginner python programing class, I have
I have a static std::map<std::string, CreateGUIFunc> in a class that basically holds strings identifying
For my Operating Systems class I have an assignment due that is built onto
I got an assignment that i have to make a programm in Java that

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.