Some Help Please (Java CLI Program)

Discussion in 'Mac Programming' started by speedracer5, Jul 17, 2011.

  1. speedracer5, Jul 17, 2011
    Last edited by a moderator: Jul 17, 2011

    speedracer5 macrumors newbie

    Joined:
    Jul 17, 2011
    #1
    Can anyone help me on this Java program I have? it's just about a do...while and it's driving me nuts. I got it to compile and work on the first run, but when I need to repeat the program is where I run into a slight problem. It's supposed to not bunch up in the repeat. here is what I am talking about when I repeat the output:

    Would you like to prepare another bill for the next customer?
    Enter Y for Yes or N for No: Y
    State your company name: Please enter customer name: <--- this should be two lines.

    My code:
    Code:
    import java.text.DecimalFormat; // for decimal formatted output
    import java.util.Scanner; // package needed for GUI
    
    public class Bill
    {
    	public static void main(String[] args) throws Exception	
    	{
    		DecimalFormat num = new DecimalFormat("$,###.00");
    		
    		
    		// variables
    
    		String Company;						// to hold Company input
    		String str;								// to hold Customer
    		int AccountNumber;					// to hold AccountNumber
    		char repeat; 							// to hold yes or no, 'Y' or 'N'	
    
    									
    		double kwhUsed;			// kilowatts used per hour
    		double OldMeterReading; // string for inputting OldMeterReading	
    		double NewMeterReading; // string for inputting NewMeterReading
    		
    		double AmountDue;
    		double charge = 0.0;
    		
    			
    		Scanner input = new Scanner(System.in); 	// initialize scanner
    	
    		do
    		{
    			System.out.print("State your company name: "); // Company name
    			Company = input.nextLine();
    				
    	 		System.out.print("Please enter customer name: "); // CustomerName
    			str = input.nextLine();	
    																			
    			System.out.print("Enter account number: "); // AccountNumber
    			AccountNumber = input.nextInt();
    				
    			System.out.print("What is your old meter reading? "); // OldMeterReading);
    			OldMeterReading = input.nextDouble();
    
    			System.out.print("What is your new meter reading? "); // NewMeterReading);
    			NewMeterReading = input.nextDouble();
    		
    			// calculate the current electricity usage
    		
    			kwhUsed = (NewMeterReading - OldMeterReading);
    
    											
    			// computations
    
    			if(kwhUsed <= 300)
    			{
    			charge = 5.00;
    			}
    			else if(kwhUsed <= 1000)
    			{
    			charge = 5.00 +(0.03*(kwhUsed - 300));
    			}
    			else if (kwhUsed >= 1001);
    			{
    			charge = 35.00 +(0.02*(kwhUsed - 1000));
    			}
    			
    							
    			AmountDue = (charge*kwhUsed); // calculate the bill
    		
    		
    			System.out.println("Company name is: " + Company); // company name
    				
    			System.out.println("Customer name is: " + str); // customer
    			
    			System.out.println("Account number is: " + AccountNumber); // account number
    	
    			System.out.println("Old Meter Reading is: " + OldMeterReading); // old meter rating
    	
    			System.out.println("New Meter Reading is: " + NewMeterReading); // new meter rating
    		
    			System.out.println("Kilowatts per hour used is: " + kwhUsed);	// kilowatts per hour
    	
    			System.out.println("Amount Due is: " + num.format(AmountDue));	// amount due	
    		
    			System.out.println(); //Prints a blank line.
     
    			// As many users as there are input
    			System.out.println("Would you like to prepare another bill "+"for the next customer?");
     
     
    			System.out.print("Enter Y for Yes or N for No: ");
     
     			str=input.next(); 		// Read next char
    			repeat=str.charAt(0); // Get the first char.
     			
    			} 
    			while (repeat=='Y' || repeat=='y');
    					
    			System.out.println("Thank you. Goodbye.");
    		} 
    	}
     
  2. itickings macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #2
    Not only should it be two lines, shouldn't you have to input something before the second line shows up?

    Take a look at the methods for input you use in different situations, and also what values are actually assigned and when.
     
  3. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #3
    System.out.println prints a newline for you, print does not.

    -Lee
     
  4. itickings macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #4
    While that is true, it is completely unrelated to the actual problem.

    If you look at how print and println are used, you'll notice that print is used consistently to make the input start on the same line, after the text, and println otherwise. My previous post should be enough to locate the actual problem.
     
  5. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #5
    Do you notice that you have to hit enter after hitting Y or N? That enter is in the input stream, but you don't read it after reading the Y or N. So when you next read from the input stream, first thing read is that Enter, hence the first prompt seems to be skipped after repeating.

    Put another input.nextLine() after input.next() to read in the Enter.
    Code:
    str = input.next(); // Read next char
    repeat = str.charAt(0); // Get the first char.
    [COLOR=blue]input.nextLine(); // Skip over end-of-line[/COLOR]
    
     
  6. speedracer5, Jul 20, 2011
    Last edited by a moderator: Jul 20, 2011

    speedracer5 thread starter macrumors newbie

    Joined:
    Jul 17, 2011
    #6
    How do I cut it down to two decisions for the selection control structure?

    eg:

    PHP:
    // computations

                
    if(kwhUsed <= 300)
                {
                
    charge 5.00;
                }
                else if(
    kwhUsed <= 1000)
                {
                
    charge 5.00 +(0.03*(kwhUsed 300));
                }
                else if (
    kwhUsed >= 1001);
                {
                
    charge 35.00 +(0.02*(kwhUsed 1000));
                }
    It should be only the first two decisions, the else of the 2nd goes to the last computation.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Like this?
    Code:
    if ( someCondition )
    {  ...  }
    else if ( otherCondition )
    {  ...  }
    else
    {  .. every condition not previously accounted for ends up here .. }
     
  8. speedracer5 thread starter macrumors newbie

    Joined:
    Jul 17, 2011
    #8
    thanks. i actually figured it out before. I wasn't thinking too hard. :)
     

Share This Page