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.