Results 1 to 8 of 8
  1. #1
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Post I have an incredibly simple program, and it terminates at random

    My family and I are planning a trip, and my mother went and said: "Hey, you're taking a programming class, make one to figure out the cost!"
    So, I started up that, just to figure out things like admission prices, taxis, food, and so on. We might have to stay a night, so I added a set of if statements to figure that out. But for some reason, it terminates right before I get to those if statements, and I have no idea why.

    Java Code:
    import java.util.*;
    public class trip {
    	public static void main(String[] args) {
    		int people, adults, seniors, kids;
    		double hotelRate, hotelCost, museumAdults, museumKids, museumSeniors, museumTotal, food = 100, total, train, taxes = .0925;
    		String hotel;
    		Scanner input = new Scanner(System.in);
    		System.out.println("Enter the number of people:");
    		people=input.nextInt();
    		
    		System.out.println("Enter the price for a round-trip set of 5 train tickets:");
    		train = input.nextInt();
    		System.out.println("Round-trip train cost: $"+train);
    		
    		System.out.println("The number of adult tickets for the field museum:");
    		adults = input.nextInt();
    		museumAdults = 15 * adults;
    		System.out.println("$"+museumAdults);
    		
    		System.out.println("The number of kid tickets for the field museum:");
    		kids = input.nextInt();
    		museumKids = 10 * kids;
    		System.out.println("$"+museumKids);
    		
    		System.out.println("The number of senior tickets for the field museum:");
    		seniors = input.nextInt();
    		museumSeniors = seniors * 10;
    		System.out.println("$"+museumSeniors);
    		
    		museumTotal = ((museumAdults + museumSeniors + museumKids)* taxes) + (museumAdults + museumSeniors + museumKids);
    		System.out.println("Total for the museum(with taxes): $"+museumTotal);
    		
    		System.out.println("Rainforest Cafe doesn't give prices online, I'll just assume $100 for 5 of us.");
    		System.out.println("The distance from Chicago Union Station to the Field Museum is about 2 miles. This makes taxi fare about: $6-$8, including tax. $10 should be fine for this trip.");
    		System.out.println("The distance from The Field Museum to the Rainforest Cafe is about 3 miles. This makes taxi fare about: $7-$9, including tax. $10 should be fine for this trip.");
    		
    		System.out.println("Are we staying at a hotel for the night?");
    		hotel=input.nextLine();
    	if(hotel.equals("yes")){
    		System.out.println("The nightly fee for one person:");
    		hotelRate = input.nextInt();
    		hotelCost = hotelRate * people;
    		System.out.println("The cost for the five of us to stay in that hotel would be about" +hotelCost);
    		System.out.println("I don't know the exact location of the hotel, so you'll have to guess the price of a taxi back to the train station. Probably no more than $20.");
    	}
    	if(hotel.equals("no")){
    			
    		}
    	}
    
    }
    I have no idea why it does this, but I'd appreciate it if anyone had a solution. Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: I have an incredibly simple program, and it terminates at random

    When you do your last integer input for number of seniors, there is still a line terminator in the input buffer. So you need to put an extra rextLine statement to flush that out before prompting for a newline to get a yes or no answer.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: I have an incredibly simple program, and it terminates at random

    Quote Originally Posted by jim829 View Post
    When you do your last integer input for number of seniors, there is still a line terminator in the input buffer. So you need to put an extra rextLine statement to flush that out before prompting for a newline to get a yes or no answer.

    Jim
    I don't quite understand, could you show me what you mean?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: I have an incredibly simple program, and it terminates at random

    Java Code:
           input.nextLine();  // flush previous line terminator to prepare for new input
            System.out.println("Are we staying at a hotel for the night?");
    
            hotel=input.nextLine();
        if(hotel.equals("yes")){
            System.out.println("The nightly fee for one person:");
    
            hotelRate = input.nextInt();
            hotelCost = hotelRate * people;
            System.out.println("The cost for the five of us to stay in that hotel would be about" +hotelCost);
            System.out.println("I  don't know the exact location of the hotel, so you'll have to guess the  price of a taxi back to the train station. Probably no more than $20.");
        }
        if(hotel.equals("no")){
                 
            }
        }
    I put the extra nextLine() at the beginning of the code snippet.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    herpeslurpy is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Re: I have an incredibly simple program, and it terminates at random

    Thanks a ton, it works now.

  6. #6
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: I have an incredibly simple program, and it terminates at random

    Ok jim here i dont want to deviate or took away this question... I want to ask why we have to do this in nextLine and not in any of the else nextInt or double or etc.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: I have an incredibly simple program, and it terminates at random

    Quote Originally Posted by Jamil37037 View Post
    Ok jim here i dont want to deviate or took away this question... I want to ask why we have to do this in nextLine and not in any of the else nextInt or double or etc.
    See #2
    Quote Originally Posted by jim829 View Post
    When you do your last integer input for number of seniors, there is still a line terminator in the input buffer. So you need to put an extra nextLine statement to flush that out before prompting for a newline to get a yes or no answer.
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Jamil37037 is offline Member
    Join Date
    Feb 2013
    Location
    Islamabad, Pakistan
    Posts
    25
    Rep Power
    0

    Default Re: I have an incredibly simple program, and it terminates at random

    thanks lol didnt saw tha line. thanks for getting my focus to that line

Similar Threads

  1. Creating a simple random file access program
    By bad biddy in forum New To Java
    Replies: 6
    Last Post: 04-03-2012, 12:13 AM
  2. Replies: 3
    Last Post: 03-13-2012, 09:28 AM
  3. My program terminates before letting me enter an answer
    By soradogoof in forum New To Java
    Replies: 2
    Last Post: 09-22-2011, 01:01 AM
  4. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 05:12 AM
  5. Application automatically terminates
    By PaulT in forum Eclipse
    Replies: 4
    Last Post: 03-18-2009, 05:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •