Forum Stats

  • 3,875,419 Users
  • 2,266,914 Discussions
  • 7,912,203 Comments

Discussions

Like while loop in an if elseif statement with user input to abort or repeat

User_P726M
User_P726M Member Posts: 14 Green Ribbon

Hello together

I am learning Java and want to solve a task with certain specifications.

There are 4 inputs which the user has to enter actively and these are then written into the respective variable. 

There are two prices and a delivery time. 


import java.util.Scanner;


public class Customer {


  public static void main(String[] args) {

   Scanner keyScan = new Scanner(System.in);

   int deliveryTime1, deliveryTime2;

   double price1, price2;


   System.out.print("Price from the first company A: ");

   price1= keyScan.nextDouble();

   System.out.print("Delivery time company A: ");

   deliveryTime1= keyScan.nextInt();

   System.out.print("Price from the first company B: ");

   price2= keyScan.nextDouble();

   System.out.print("Delivery time company B: ");

   deliveryTime2= keyScan.nextInt();

   keyScan.close();

    

   if (price1< price2 && deliveryTime1< deliveryTime2)

    System.out.println("Order from company a");

   else if (price1< price2 && deliveryTime1> deliveryTime2)

    System.out.println("Order from company b");

   else if (price1> price2 && deliveryTime1< deliveryTime2)

    System.out.println("Order from company a");

   else if (price1> price2 && deliveryTime1> deliveryTime2)

    System.out.println("Order from company b");

   else if (price1< price2 && deliveryTime1> 14)

    System.out.println("Order nothing");

   else if (price1 > price2 && deliveryTime1 > 14)

    System.out.println("Order nothing");

   else if (price1< price2 && deliveryTime2> 14)

    System.out.println("Order nothing");

   else if (price1> price2 && deliveryTime2> 14)

    System.out.println("Order nothing");

   else if (price1== price2 && deliveryTime1 < 14)

    System.out.println("Free selectable");

   else if (price1== price2 && deliveryTime2 < 14)

    System.out.println("Free selectable");   

  }

}

The following points must be fulfilled


In the case of the cheaper order with a delivery time shorter than 14 days should be ordered there

In the case of the cheaper order with a delivery time longer than 14 days but not that of the more expensive one, the order should be placed there.

if both have longer than 14 days, nothing should be ordered

if both are the same price and both do not have a delivery time longer than 14 days, the customer can decide for themselves


Furthermore, I want to program this so that the output still says how much cheaper it is compared to the other company and the delivery time of a company x is too long and this in a loop where you can trigger the order again or cancel this with a defined number with the output "Thank you for your visit"

I've currently come this far, but am still beating my head against the logic. That is, I don't know how to display the result in the system.out.println (how much cheaper it is or the delivery time) and still wrap this in a loop.

Thanks for the help!

Tagged:

Best Answers

  • justsomeone
    justsomeone Member Posts: 40 Red Ribbon
    Answer ✓

    extra point for variable

    any variable should be intialized before used

    for primitive type like int/double/char you need to set value for it using

    1) literal value like say int a = 5; this is literal value

    2) using expression that return the value like int a = scanner.nextInt();


    same rule apply for reference type which i guess your did not learn about it yet

    the only expression to this rule is instance field - static field - array element ) which initialized automaticaly to it's default value for the type so for all numeric type it will be 0 or 0.0 and for char it's '\u0000'

    and false for boolean and null for all reference type


  • justsomeone
    justsomeone Member Posts: 40 Red Ribbon
    Answer ✓

    this much better

    int answer = keyScan.nextInt(); for the intial value of answer you can set it to 1
    

    or you want to allow the user to not even run the program for even a single time ?

    if this what you want then you can at least print a question to the user

    or another way

    int answer;

    then change the while to do while

    do{

    ... same code as you have

    }while(answer==1);


    i did not check the if else part as i did not get the whole rule for it but you have

    deliverytime1 <= maxdeliverytime && deliverytime1 <= maxdeliverytime
    

    you compare deliverytime1 twice i guess it type and even both of them could be removed cause you already cover that condition from the previous if by this one

    else if (deliverytime1 >= maxdeliverytime && deliverytime2 >= maxdeliverytime)
    


    you doing great well done :)

«1

Answers

  • justsomeone
    justsomeone Member Posts: 40 Red Ribbon

    Hello


    if you learned about class then you would create class Company that has 2 fields one for time and another for price

    have some methods to check if certain object time is less that 14 and based on that get the lowest of them

    if you did not get there yet then

    this part of it

    double priceDifferance = Math.abs(price2 - price1);
    
    if (price1 == price2) {
        if (deliveryTime2 < 14 && deliveryTime1 < 14) {
            System.out.println("Free selectable");
        } else if (deliveryTime2 > 14 && deliveryTime1 > 14) {
            System.out.println("Order nothing");
        } else { // That is mean both has same price but one has tie delivery less than 14 and the other one longer than 14
            
            System.out.println((deliveryTime1 < deliveryTime2 ? "Order from company a cheaper from B by" : "Order from company b cheaper than a by") + priceDifferance);
            // do same thing with delivaryTime
        }
    }
    

    using class would make the code much better but if you did not get there do not worry for now

    but i did not get one point

    "In the case of the cheaper order with a delivery time longer than 14 days but not that of the more expensive one, the order should be placed there." what that mean

    and why you need the loop for

    if you mean that the program will do the same thing till user input some then you can use while loop with true as it's condition and when you read that number you exit the loop using break statement


    hope that help and have a nice day :)

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    Hello justsomeone!

    Thanks so much for your input.

    Exactly that's what i mean with the while loop, i tried it by myself but it never worked.

    while(true)

    at the start above the if statement. But how should the end look with the output "done? 0 for yes 1 for no". So if 0, the program should end. If 1 the loop should start over again.

    And what I meant is, if both offers are equal in price and in both cases the delivery time is not more than two weeks, the buyer can decide for himself where to order.

    Thanks again for your time and input!!

  • justsomeone
    justsomeone Member Posts: 40 Red Ribbon

    you very welcome @User_P726M

    then use while just after you define double price from your code

    after this line

    double price1, price2;

    and just before the end of the while loop you output this

    "done? 0 for yes 1 for no"

    then using the scanner you check if the user input 0 then you break the loop using simple

    break; and print the "Thank you for your visit"

    and if the user insert 1 then do nothing and the loop will start over again

    hope that help and have a nice day :)

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    Thank you again justsomeone!


    So if i got it right..

    import java.util.Scanner;
    
    
    public class Customer {
    
    
      public static void main(String[] args) {
    
       Scanner keyScan = new Scanner(System.in);
    
       int deliveryTime1, deliveryTime2;
    
       double price1, price2;
       int n;
    
       while(true)
       Scanner input = new Scanner(System.in);
    
       System.out.print("Price from the first company A: ");
    
       price1= keyScan.nextDouble();
    
       System.out.print("Delivery time company A: ");
    
       deliveryTime1= keyScan.nextInt();
    
       System.out.print("Price from the first company B: ");
    
       price2= keyScan.nextDouble();
    
       System.out.print("Delivery time company B: ");
    
       deliveryTime2= keyScan.nextInt();
    
       keyScan.close();
    
        
    
       if (price1< price2 && deliveryTime1< deliveryTime2)
    
       System.out.println("Order from company a");
    
       else if (price1< price2 && deliveryTime1> deliveryTime2)
    
       System.out.println("Order from company b");
    
       else if (price1> price2 && deliveryTime1< deliveryTime2)
    
       System.out.println("Order from company a");
    
       else if (price1> price2 && deliveryTime1> deliveryTime2)
    
       System.out.println("Order from company b");
    
       else if (price1< price2 && deliveryTime1> 14)
    
       System.out.println("Order nothing");
    
       else if (price1 > price2 && deliveryTime1 > 14)
    
       System.out.println("Order nothing");
    
       else if (price1< price2 && deliveryTime2> 14)
    
       System.out.println("Order nothing");
    
       else if (price1> price2 && deliveryTime2> 14)
    
       System.out.println("Order nothing");
    
       else if (price1== price2 && deliveryTime1 < 14)
    
       System.out.println("Free selectable");
    
       else if (price1== price2 && deliveryTime2 < 14)
    
       System.out.println("Free selectable");  
    
       System.out.println("Do you wanna exit? 0 - yes 1 - No ");  
       n = input.nextInt();
         
       if (n != 0) {
           System.out.println("You entered " + n);
            continue;
          }
          else {
            break;
      }
    
    }
    

    does that makes sense?

  • justsomeone
    justsomeone Member Posts: 40 Red Ribbon

    you very welcome


    1) you need to use bracket to wrap all statement inside the while loop

    and it better to use {} even for single line statement for if and while and so on as it make your code more readable

    2) close the scanner after the while loop

    3) why you created a new scanner called input?

    4) need to work with all those if else to reduce them so split them with nested cases also recheck the order of it for example

     if (price1< price2 && deliveryTime1< deliveryTime2)
    
       System.out.println("Order from company a");
    

    this will not fulfill your condition that delivary1 and 2 are less than 14 so if delivary1 was 16 and 2 was 17 which from what i got of the requirement then it should print

    System.out.println("Order nothing");
    

    so try to find a tree of condition which will help you to reduce the amount of if and also not make you test the same condition more than once for example

    else if (price1== price2 && deliveryTime1 < 14)
    
       System.out.println("Free selectable");
    
       else if (price1== price2 && deliveryTime2 < 14)
    

    here you test that price1 == price2 twice

    5) for the part of how you exit the loop then you did a great job well done


    hope that help and have a nice day :)

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    Hi justsomeone

    So my code looks like that now if i got you right and again, thanks for your time and knowledge! About the logic of the statements i will give it a look later. My goal is the while loop right now...but it's seems not really working. I'm sure i did something wrong

    public class Lieferant {
    
      public static void main(String[] args) {
       Scanner keyScan = new Scanner(System.in);
       int deliveryTime1, deliveryTime2;
       double price1, price2;
       int n;
    
       while(true) {
       System.out.print("Price from the first company A: ");
       price1= keyScan.nextDouble();
     
       System.out.print("Delivery time company A: ");
       deliveryTime1= keyScan.nextInt();
       
       System.out.print("Price from the first company B: ");
       price2= keyScan.nextDouble();
       
       System.out.print("Delivery time company B: ");
       deliveryTime2= keyScan.nextInt();
       keyScan.close();
       }
    

    So here is the while loop closed after the scanner, is that right?

       if (price1< price2 && deliveryTime1< deliveryTime2) {
       System.out.println("Order from company a");
       }
       else if (price1< price2 && deliveryTime1> deliveryTime2) {
       System.out.println("Order from company b");
       }
       else if (price1> price2 && deliveryTime1< deliveryTime2) {
       System.out.println("Order from company a");
       }
       else if (price1> price2 && deliveryTime1> deliveryTime2) {
       System.out.println("Order from company b");
       }
       else if (price1< price2 && deliveryTime1> 14) {
       System.out.println("Order nothing");
       }
       else if (price1 > price2 && deliveryTime1 > 14) {
       System.out.println("Order nothing");
       }
       else if (price1< price2 && deliveryTime2> 14) {
       System.out.println("Order nothing");
       }
       else if (price1> price2 && deliveryTime2> 14) {
       System.out.println("Order nothing");
       }
       else if (price1== price2 && deliveryTime1 < 14) {
       System.out.println("Free selectable");
       }
       else if (price1== price2 && deliveryTime2 < 14) {
       System.out.println("Free selectable");  
       }
       
       System.out.println("Do you wanna exit? 0 - yes 1 - No ");  
       
       if (n != 0) {
           System.out.println("You entered " + n);
            continue;
          }
          else {
            break;
          }
       }
    }
    

    can you explain me what a nested case is? i know it's a lot of if statements...and my head is exploding already.

  • justsomeone
    justsomeone Member Posts: 40 Red Ribbon

    you very welcome


    the while loop should wrap the whole code to be more specific

    it start same as you did but it end after the last if where you check if the user want to try again or not

     if (n != 0) {
           System.out.println("You entered " + n);
            continue;
          }
          else {
            break;
          }
       }
    

    and you close the scanner after the closing bracket of the while loop

    or inside this one

     if (n != 0) {
           System.out.println("You entered " + n);
           keyScan.close();
            continue;
          }
    

    why?

    cause you would not need to read any input later

    but using your code will close it after the first iteration of the loop while you still need it in case the user want to repeat it

    what i meant by nesting things is like

    if (price1== price2 && deliveryTime1 < 14) {
       System.out.println("Free selectable");
       }
       else if (price1== price2 && deliveryTime2 < 14) {
       System.out.println("Free selectable");  
       }
    

    in the above code you test price1==price2 twice

    then you can short that to

    if (price1 == price2) {
        if (deliveryTime1 < 14) {
            System.out.println("Free selectable");
        } else if (deliveryTime2 < 14) {
            System.out.println("Free selectable");
        }
    }  
    

    here i test price1== price2 only once

    can we make this one even shorter sure we can

    if (price1 == price2) {
        if (deliveryTime1 < 14 || deliveryTime2 < 14) {
            System.out.println("Free selectable");
        }
    }
    

    we can even remove the nested if using this one

    if (price1 == price2 && (deliveryTime1 < 14 || deliveryTime2 < 14)) {
    
        System.out.println("Free selectable");
    }
    

    i just take part of your code and apply the nested if to show you how it work

    and later it make us see another opertaunity to make it more shorter and without any repeating test

    so in coding remove the repeated code and later when you learn methods and classes you will get better way to solve it

    feel free to ask anytime and i am glad to help and let me know if i need to explain something in better way

    hope that help and happy coding :)

  • User_P726M
    User_P726M Member Posts: 14 Green Ribbon

    justsomeone

    I fixed some stuff and made it better! But still got problems with the while loop.

    Under the variables i started with while(true) {

     while(true) {
          
        System.out.print("Price company a: ");
        price1= keyScan.nextDouble();
        System.out.print("delivery time A ");
        deliverytime1= keyScan.nextInt();
        System.out.print("Price company b ");
        price2 = keyScan.nextDouble();
        System.out.print("delivery time b");
        deliverytime2= keyScan.nextInt();
        keyScan.close();
        
       
    

    defined int n;

    public class Customer {
    
    
       public static void main(String[] args) {
        Scanner keyScan = new Scanner(System.in);
        int lieferzeitA, lieferzeitB;
        double preisA, preisB;
        int n;
    


    and under the last else if I have put


    		if (n !=0) {
    		   System.out.println("You entered " + n);
    		   keyScan.close();
    		     continue;
    		}
    

    but the compiler isn't happy and i don't get why

    " The local variable n may not have been initialized" is the output

  • justsomeone
    justsomeone Member Posts: 40 Red Ribbon

    could you share the full code

    but for your earlier code

     System.out.println("Do you wanna exit? 0 - yes 1 - No ");  
       
       if (n != 0) {
    

    you did not read the input from the user so you need

    to user your scanner to get the input from the user

    and also remove this close statement here

     deliverytime2= keyScan.nextInt();
        keyScan.close();
    

    and for this error

    "The local variable n may not have been initialized"


    you need to set a value for any local variable before access it that what it complain about


    hope that help and have a nice day :)

  • justsomeone
    justsomeone Member Posts: 40 Red Ribbon
    Answer ✓

    extra point for variable

    any variable should be intialized before used

    for primitive type like int/double/char you need to set value for it using

    1) literal value like say int a = 5; this is literal value

    2) using expression that return the value like int a = scanner.nextInt();


    same rule apply for reference type which i guess your did not learn about it yet

    the only expression to this rule is instance field - static field - array element ) which initialized automaticaly to it's default value for the type so for all numeric type it will be 0 or 0.0 and for char it's '\u0000'

    and false for boolean and null for all reference type