-2
import java.io.*;

public class ManyTickets
 {

  public static void main (String [] args) throws IOException
  {
   String userInput;
   String userInput2;
   int intInput = 0;
   int intInput2 = 0;
   double total = 0.00;
   //(1) BufferedReader ageInput = new BufferedReader (new InputStreamReader (System.in));

Question 1: Above line works fine (1) when we change it into other place which i marked as (2). // but while executing "Please enter your age"line comes first though i create bufferedreader object before that statement in case (1).I expect compiler should wait for user input but it prints the statement. Though i create ageInput before try.

try{
    System.out.println("Please enter your age, or press '999' to exit.");

   //(2) BufferedReader ageInput = new BufferedReader (new InputStreamReader (System.in));

    userInput = ageInput.readLine();
    intInput = Integer.parseInt (userInput);
    while (intInput != 999)

//Question2:While executing by 999 if i type 999 without giving space it is execute it gives some output but not exit.how to avoid whitespaces in the beginning like whatever i am giving whether 999 or 999 or999 i need same output.I need exit but not for input like 99 9; I need method to avoid whitespaces in the input(beginning).

{
     if (intInput < 0 || intInput > 110)
     System.out.println("Invalid entry, or your age seems a bit too high to enter buy
     tickets);
     else if (intInput <= 12)
     {
      total = total + 6;
      System.out.println("The ticket cost for 1 ticket is " + total);
     }
      else if (intInput <= 64)
     {
      total = total + 11;
      System.out.println("The ticket cost for 1 ticket is " + total);  
      }
       else
      {
       total = total + 8;
       System.out.println("The ticket cost for 1 ticket is $" + total);
       }
       System.out.println("So far, your tickets cost is: $" + total  );
       System.out.print("Would you like to buy more tickets? You can buy up to 1 more ticket    per customer! If no press 999to exit");
       userInput = ageInput.readLine();
       intInput2 = Integer.parseInt (userInput);  
        }
       }catch (NumberFormatException e){
       System.out.println("Please restart the program, and enter an integer instead!");
        }
        } 
       {
        double total = 0.0;
        System.out.println("Thank you,  The total cost for the ticket is: $" + total);
        System.out.println("Have a nice day!");
       }
       }
General Grievance
  • 4,555
  • 31
  • 31
  • 45
karthick
  • 68
  • 1
  • 6

3 Answers3

0

Simply use Trim function to remove spaces before and after a input

ageInput.readLine().trim()

This is the definition of trim() method

public String trim() {
        int len = value.length;
        int st = 0;
        char[] val = value;    /* avoid getfield opcode */

        while ((st < len) && (val[st] <= ' ')) {
            st++;
        }
        while ((st < len) && (val[len - 1] <= ' ')) {
            len--;
        }
        return ((st > 0) || (len < value.length)) ? substring(st, len) : this;
    }

if you only want to remove first part remove second one

while ((st < len) && (val[len - 1] <= ' ')) {
    len--;
}

For Question 2 readline() is responsible for get the input. you can create buffered reader object anywhere before readline

Bruce
  • 8,609
  • 8
  • 54
  • 83
0

Question2:While executing by 999 if i type 999 without giving space it is execute it gives some output .how to avoid whitespaces in the beginning like whatever i am giving whether 999 or 999 i need same output. I need method to avoid whitespaces in the input.

To avoid white spaces simply remove them from strings like following:

userInput = ageInput.readLine().replaceAll(" ","");
intInput = Integer.parseInt (userInput);

Question 1: Above line works fine (1) when we change it into other place which i marked as (2). // but while executing "Please enter your age"line comes first though i create bufferedreader object before that statement in case (1).I expect compiler should wait for user input but it prints the statement. Though i create ageInput before try.

This is too ambiguous.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
0

Answer #1:

You are asking for user input after line 2 i.e. after print statement and hence it wait's after asking the question. If you move statement userInput = ageInput.readLine(); before print statement (keeping Buffered reader's instantiation at line 1) then it will wait for user input and then will print the statement.

Answer #2:

You could use String's trim() method as below:

userInput = ageInput.readLine().trim();

If you need user defined function (which i would avoid), you could do something like:

public String myStringTrimmer() {
    int len = value.length;
    int st = 0;
    char[] val = userInput.toCharArray();
    while ((st < len) && (val[st] <= ' ')) {
        st++;
    }
    while ((st < len) && (val[len - 1] <= ' ')) {
        len--;
    }
    return ((st > 0) || (len < value.length)) ? userInput.substring(st, len) : userInput;
}
SMA
  • 36,381
  • 8
  • 49
  • 73
  • It is not legal here to ask like this .so i am asking in my comment .Please tell me how to improve programming step by step . – karthick Dec 28 '14 at 05:39
  • Please document on while loops. Two while loops and return statement almas Shaikh – karthick Dec 28 '14 at 05:48
  • @karthick what do you mean by document? You already closed this question by accepting the answer, so what else you are looking for.. – SMA Dec 28 '14 at 06:13
  • I mean comments on while loop and return statements like what happens inside each line: – karthick Dec 28 '14 at 06:49
  • Is there a need now as you already done with your issue and now question is closed so please raise a new question? – SMA Dec 28 '14 at 07:05