IIDSII Hoodz - 1 year ago 89
Java Question

# Have to enter input twice rather than once to continue pogram

I'm writing a program for buying tickets. So far this is my code.

``````package prog;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Scanner;
public class assign {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Which type of ticket do you require stand or terrace?");
char tickType=sc.next().charAt(0);
System.out.println("How many tickets do you require?");
byte amount=sc.nextByte();
if (amount<5)
System.out.println("How many tickets for students?");
byte stud=sc.nextByte();
if (amount>=5)
System.out.println("You are entitled to a 12% discount.");
else if (stud>0)
System.out.println("discount will be 10%");
else
System.out.println("You are not entitled to a discount");
if (tickType=='S'||tickType=='s')
System.out.println("Ticket cost is\t "+amount*20.00);
else
System.out.println("Ticket cost is\t"+(amount*15.00));
double stand= amount*20.00;
double terrace= amount*15.00;
//VAT is 23%
``````

When I get to

``````System.out.println("How many tickets do you require?");
byte amount=sc.nextByte();
``````

if I enter 5 or a higher number I have to enter it twice rather than once can somebody tell me why this is happening please?

You have a flaw in the logic: if amount is less than five, then you print "How many tickets for students?", but then read the value even if amount is greater or equal to five.

The best solution would be to declare `byte stud = 0;` before `if (amount < 5)`, and then correct the if statement to include two lines:

``````byte stud = 0;
if (amount < 5) {
System.out.println(...);
stud = sc.nextByte()
}
``````

This way, you read the value for stud only if amount is less than 5.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download