-1
import java.util.Scanner;

public class CreatePurchase {

    public static Purchase item;
    public static Scanner details;

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

        int invoice = details.nextInt();
        boolean invoiceRange = ((invoice >= 1000) && (invoice <= 8000));
        while (!invoiceRange)
        {
            invoice = details.nextInt();
        }

        item.setInvoice(invoice);

        double sale = details.nextDouble();
        if (sale >= 0)
        {
            item.setSale(sale);
        }
        item.display();
    }
}

No syntax or semantic errors appear when running this code. If you input the correct values for invoice and sale, it runs with no issues. The issue I am facing seems to be logical and I don't really understand why. After passing the first loop cycle, it keeps requesting that I input values as if it doesn't go through the checking phase anymore, which causes an infinite loop.

I don't think the Boolean logic is wrong because it works when providing the right values, but I thought I should ask here before assuming anything.

I think it has something to do with me declaring my objects as static fields. I wanted to make the Purchase class a part of the CreatePurchase class, by making it a field. Java wouldn't allow me to access it without creating an instance of the CreatePurchase class, which I think is recursive and would cause a stack overflow. Making them static worked, but I think the fields either stop receiving values after the first input, or the Boolean logic is just faulty.

This question comes from the same assignment that the question Java use of static fields is based on.

Community
  • 1
  • 1
i0h3
  • 25
  • 9
  • 2
    Your boolean condition `(!invoceRange)` is never changed in the loop, thus it goes infinite. – a-sir Oct 26 '16 at 21:56
  • 1
    Your question title is misleading -- objects are neither static nor instance -- they simply *are*. ***Variables*** on the other hand can be declared static or not declared static (so-called *instance* variables), and the same object can be referenced by both a static and an instance field. – Hovercraft Full Of Eels Oct 26 '16 at 21:58
  • 1
    There's not much point to making those objects into static fields, if they are only accessed from the `main` method. – 4castle Oct 26 '16 at 22:08
  • Your title has nothing to do with your actual problem. – user207421 Oct 26 '16 at 23:37
  • @EJP I had a problem that I believed was related to trying to call objects from a static context. Turns out it was the Boolean logic. Sorry somewhat new to Java. – i0h3 Oct 27 '16 at 00:07

3 Answers3

0

invoiceRange is not updated inside the while so it always contains the same value and the while never ends.

David Rojo
  • 2,337
  • 1
  • 22
  • 44
0

the issue is because your invoiceRange is not resetting to true in the while loop.

you need to set it to invoiceRange=true inside while loop or break the loop using break keyword

0

You should use a do-while loop here:

int invoice;
do {
    invoice = details.nextInt();
} while (invoice < 1000 || invoice > 8000);

This will repeatedly ask for an invoice value from the user until it is within the accepted range. Your code does not work because you only set the value of invoiceRange after reading in the first value given by the user, so if that value is outside of the given range, then the loop will run infinitely.

This could also be done with the addition of one line to your while loop:

int invoice = details.nextInt();
boolean invoiceRange = ((invoice >= 1000) && (invoice <= 8000));
while (!invoiceRange)
{
    invoice = details.nextInt();
    // Need this line to update the value of invoiceRange every
    // time a new invoice value is read in
    invoiceRange = ((invoice >= 1000) && (invoice <= 8000));
}

However, a do-while is preferable in the case where you want to do something (read in an invoice value from the user) before checking if you need to keep doing that action (check if invoice value is within accepted range).

One final note: Since you are only using item and details in your main method, it would be better if you declared and initialized them within the method. This makes the code more readable and helps you keep track of where changes to variables are occurring, which makes it easier to identify the cause of issues.

mapeters
  • 1,067
  • 7
  • 11
  • Thank you for the suggestion. I'm new to Java so I kept looking into Java's unique type system without even trying to see if changing the Boolean logic would help. Does that mean the static reference is fine? Thinking about how the static keyword works, it seems unlikely. – i0h3 Oct 27 '16 at 00:25
  • @i0h3 Yes the static reference is fine, in the sense that it's not going to prevent your program from running correctly. But like I mentioned in my answer, it would be better to not have static class fields and instead just declare them in the `main` method. – mapeters Oct 27 '16 at 01:13