-2

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

Bernard
  • 7,908
  • 2
  • 36
  • 33
D3TXER
  • 152
  • 2
  • 13
  • SO... YOU NEED TO PUT IN a little more effort. find the problem, don't use teen words(boobies) etc etc – Caffeinated Oct 03 '12 at 01:57
  • 1
    You are saying you said "yes", it should either say "y" or "Y". :) – Bhesh Gurung Oct 03 '12 at 01:59
  • ok, so first of all. Its a test...its just there to remind me that it shouldn't be. Second, you're answer still doesn't solve the problem. I have no issue with constructive criticism. If you stop and think for a moment about what the Y stands for (cough) yes (cough) what i meant was when i had made a input that confirmed yes... "y" || "Y" – D3TXER Oct 03 '12 at 04:07

4 Answers4

7

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 :)
}
Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
Jimmy
  • 2,589
  • 21
  • 31
2

You should be able to figure this out yourself by EITHER running the application under a debugger OR adding some diagnostic trace prints; e.g. something like this at the appropriate point(s).

    System.out.println("any is ==>'" + any + "'<==");

(I bet that this will show that any doesn't contain what you expect it to ...)

I strongly recommend that you learn to debug your own code rather than asking other people to help. This is a critical skill for a professional software engineer.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Stephen, I appreciate your comment here. I have no desire to become a professional software engineer. Mostly I have been working with front end UI and relational DB's. Coding is not really my strong suit. I feel it is important for me to know a little about as much as possible so that I can get the "big picture" of what I need. Hence the class. In my post I mentioned having a poor instructor, as well as limited peer group for help. That is why I came here, so that people who are adept can help me. I felt that this was the spirit of the site. I will try to do more work on my own in the future. – D3TXER Oct 03 '12 at 04:13
  • 1
    @user1639634 - whether or not you aspire to become a professional, it is a good thing to be able debug your own code. The point I was trying to make is that you need to *learn* that skill. I wasn't trying to get at you for being lazy or anything like that. – Stephen C Oct 03 '12 at 04:22
1

This seems to work fine for me...

boolean more = true;
while (more) {

    System.out.print("Do you have any more purchases Y/N?: ");
    String any1 = scan.nextLine();
    System.out.println();
    if (any1.equalsIgnoreCase("y")) {
        more = true;
    } else {
        more = false;
    }

    System.out.println(more);

}

Which kind of freaks me out cause I'm sure there's not that much difference

while(more==true) is not required while(more) says the same thing.

if(any1.equals("y") || any1.equals("Y")) is not required, if (any1.equalsIgnoreCase("y")) will do the same thing

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Hey this ignore case is awesome. If my score was high enough I would +1 you for this one. Thanks again. – D3TXER Oct 03 '12 at 04:15
1

For a cleaner and more readable code, prefer in this case do..while block since now no need for more variable:

public static void main(String[] args) {
    double purchase = 0.0;
    Scanner scan = new Scanner(System.in);
    System.out.print("Do you have any more purchases Y/N?: ");
    if (!scan.nextLine().equalsIgnoreCase("y")) {
        return;
    }
    do {
        System.out.print("Please input purchase amount: ");
        purchase += Double.parseDouble(scan.nextLine());
        System.out.print("Do you have any more purchases Y/N?: ");
    }
    while (scan.nextLine().equalsIgnoreCase("y"));
    System.out.println("Your purchase amount is: " + purchase + " and so is " + (purchase >= 2500 ? ">= 2500" : "< 2500"));
}

For a homework, that's pretty good I think. For a real work in a project, you should refactor in many small methods that make sense and avoid most of redundancy. That could be your secondary homework: optimize it :)

Mik378
  • 21,881
  • 15
  • 82
  • 180
  • (scan.nextLine().equalsIgnoreCase("y")); This line was very helpful. I edited my code with it. However do...while executes once no matter what right? I want to check with user to confirm they have a purchase. Essentitally provide an escape early on. Throw in a System.exit(0); if they did not intend to use the program. Or maybe create method for this whole thing and then just recall the method in a loop or offer to exit. But thank you for your help. – D3TXER Oct 03 '12 at 04:32
  • @user1639634 I've just updated the code with what you expect. – Mik378 Oct 03 '12 at 08:57