#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0

    Help with program


    Code:
    package wirelessBill;
    
    import java.util.Scanner;
    
    /* @Author Xander Carlson
     * CSIS 161 Programming I Professor Abuhejleh
     * 12:00-12:50 MoWeFri
     * Program #7 Due: Wednesday November 14th
     */
    
    /*This program displays the wireless bill based on service plan as
     * well as day and night minutes
     */
    
    public class WirelessBill {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//Initializes the user_in command. Allows inputs from the user.
    		Scanner user_in = new Scanner(System.in);
    		//Initializes the variables
    		int accountNumber;
    		String Name;
    		String serviceCode;
    		int dayMin;
    		double dayCost;
    		int nightMin;
    		double nightCost;
    		double Total;
    		int min;
    		double cost;
    		System.out.println("Welcome to Verizon Wireless. What is your name?");
    		Name = user_in.nextLine();
    		System.out.println("Hello " + Name + ", what is your Account Number?");
    		accountNumber = user_in.nextInt();
    		System.out.println("What service plan do you have? Premium or Regular?");
    		serviceCode = user_in.nextLine();
    		if(serviceCode.equals("Premium"))
    			{
    			System.out.println("You have the Premium wireless service.");
    			System.out.println("How many daytime minutes did you use?");
    			dayMin = user_in.nextInt();
    			System.out.println("How many nighttime minutes did you use?");
    			nightMin = user_in.nextInt();
    			dayCost = (dayMin - 75)*.10;
    			nightCost = (nightMin - 100)*.05;
    			Total = dayCost + nightCost + 25;
    			}
    		else if(serviceCode.equals("Regular"))
    			{
    			System.out.println("You have the Regular wireless service."); 
    			System.out.println("How many minutes did you use?");
    			min = user_in.nextInt();
    			cost = (min - 50)*.20;
    			Total = cost;
    			}
    	}
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    This programs prompts the user for name and account number then should ask if they have Premium or Regular wireless service. I enter the name and account number, then the prompt "Do you have Premium or Regular service?" pops up and it should run a user_in.nextLine but it just stops. Any help?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    I believe your answer is here:


    http://stackoverflow.com/questions/8873351/reading-strings-next-and-nextline-java


    Basicly, when you use Scanner's nextInt() method, and you put in an integer from the commandline, you insert both an integer and a string, because you enter "{number}\n". The {number} is handled by nextInt(), the \n is handled by getNextLine().
    Add an extra getNextLine() to resolve this.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Code:
    package wirelessBill;
    
    import java.util.Scanner;
    
    /* @Author Xander Carlson
     * CSIS 161 Programming I Professor Abuhejleh
     * 12:00-12:50 MoWeFri
     * Program #7 Due: Wednesday November 14th
     */
    
    /*This program displays the wireless bill based on service plan as
     * well as day and night minutes
     */
    
    public class WirelessBill {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		//Initializes the user_in command. Allows inputs from the user.
    		Scanner user_in = new Scanner(System.in);
    		//Initializes the variables
    		int accountNumber;
    		int dayMin;
    		int nightMin;
    		int min;
    		String Name;
    		String serviceCode;
    		double dayCost;
    		double nightCost;
    		double Total;
    		double cost;
    		System.out.println("Welcome to Verizon Wireless. What is your name?");
    		Name = user_in.nextLine();
    		System.out.println("Hello " + Name + ", what is your Account Number?");
    		accountNumber = user_in.nextInt();
    		System.out.println("What service plan do you have? Premium or Regular?");
    		serviceCode = user_in.nextLine();
    		serviceCode = user_in.nextLine();
    		String strService = serviceCode;
    		String strServiceCap = strService.toUpperCase();
    		if(serviceCode.equals("PREMIUM"))
    		{
    			System.out.println("You have the Premium wireless service.");
    			System.out.println("How many daytime minutes did you use?");
    			dayMin = user_in.nextInt();
    			System.out.println("How many nighttime minutes did you use?");
    			nightMin = user_in.nextInt();
    			dayCost = (dayMin - 75)*.10;
    			nightCost = (nightMin - 100)*.05;
    			Total = dayCost + nightCost + 25;
    			System.out.println("Name is " + Name + ". Account number is " + accountNumber + ". Total bill is " + Total + ".");
    		}
    		else if(serviceCode.equals("REGULAR"))
    		{
    			System.out.println("You have the Regular wireless service."); 
    			System.out.println("How many minutes did you use?");
    			min = user_in.nextInt();
    			cost = (min - 50)*.20;
    			Total = cost;
    			System.out.println("Name is " + Name + ". Account number is " + accountNumber + ". Total bill is " + Total + ".");	
    		}
    		System.out.println("End of program");
    		
    	}
    }
    Now my code just ignores the If Else statement regarding which service plan they have.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    Try debugging the code by printing out the values of the variables used in the if statements so you can see what the computer sees when the code executes.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by NormR
    Try debugging the code by printing out the values of the variables used in the if statements so you can see what the computer sees when the code executes.
    It tells me to initialize Total if it prints outside of the If Else, so the program is not seeing the If Else whatsoever.
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    348
    What was printed out when you added the print statement to show the values of the variables used in the if statement?


    Please copy and paste here the full contents of the console window from when you execute the code. Add some comments to it showing what is wrong.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Hint: look at which variable you are doing the comparison with.
  16. #9
  17. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,554
    Rep Power
    1621

    Upper case vs arbitrary case


    observe the following lines in your codes:
    Java Code:
     
    //preceding codes omitted for brevity
     
     
      	System.out.println("What service plan do you have? Premium or Regular?");
    		serviceCode = user_in.nextLine();
    		serviceCode = user_in.nextLine();
    		String strService = serviceCode;
    		String strServiceCap = strService.toUpperCase();
     
    //the rest of the codes follows


    You have assigned whatever read from the user_in.nextLine() twice to serviceCode. The first one may have read the value "premium" or "regular" correctly from the Scanner, but the second call to user_in.nextLine() might have read in the new line character (the Enter key). So, you might have probably overwritten the correct input value with the new line character you read from Scanner the second time.

    Try not to assign the value returned by the second call to user_in.nextLine(), something like the following codes
    Java Code:
     
    //preceding codes omitted for brevity
     
    System.out.println("What service plan do you have? Premium or Regular?");
    		serviceCode = user_in.nextLine();
    		user_in.nextLine();
    		String strService = serviceCode;
    		String strServiceCap = strService.toUpperCase();
     
    //the rest of the codes follows


    After this, print out the value assigned to serviceCode in the console.
    When the programming world turns decent, the real world will turn upside down.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Actually, the first nextLine takes the newline character from the previous nextInt, the second nextLine takes the inputted string.
    The problem is that you are comparing serviceCode instead of strServiceCap

IMN logo majestic logo threadwatch logo seochat tools logo