Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to figure out how to use char in this problem.

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trying to figure out how to use char in this problem.

    Hello all this is my first time here. So I'm doing this program for school and we are supposed to follow the below UML. The program below works the way it is however it uses the int pkg instead of the char that it is supposed to. So the user has to type 1 2 or 3 instead of the A B or C like they should. So if anyone knows how to fix this or has any suggestions they would be much appreciated.

    Also if I did anything wrong with my first post I apologize in advance.

    InternetCharges
    - pkg: char
    - hours: double
    + InternetCharges(p: char, h: double)
    +setPkg(p:char):void
    +setHours(h: double): void
    +getPkg():char
    +getHours():double
    +getCharges():double


     
    public class InternetCharges
    {
    	private int pkg;
    	private double hours;
     
    	public InternetCharges(int p, double h)
    	{
     
    		pkg = p;
    		hours = h;
    	}
    	public void setPkg(char p)
    	{
    		pkg = p;
    	}
    	public void setHours(double h)
    	{
    		hours = h;
    	}
    	public int getPkg()
    	{
    		return pkg;
    	}
    	public double getHours()
    	{
    		return hours;
    	}
    	public double getCharges()
    	{
    		double charges = 0;
     
    		if (pkg == 1)
    			charges = 9.95;
    		else if (pkg == 2)
    			charges = 14.95;
    		else if (pkg == 3)
    			charges = 19.95;
    		else if (pkg == 3)
    			charges = (hours * 0);
     
    		if (pkg == 3)
    			charges = charges + (hours * 0);
    		else if ((hours > 10) && (pkg == 1))
    			charges = charges + ((hours - 10) * 2);
    		else if ((hours > 20) && (pkg == 2))
    			charges = charges + (hours - 20);
     
    		return charges;
    	}
    }

     
    import java.util.Scanner;
     
    public class InternetChargesDemo
    {
    	public static void main(String[] args)
    	{
    		Scanner keyboard = new Scanner(System.in);
     
    		int pkg;
    		double hours;
     
    		System.out.println("What package have you purchased? A=1 B=2 and C=3"); 
    			pkg = keyboard.nextInt();
    		System.out.println("How many hours have you used? ");
    			hours = keyboard.nextDouble();
     
     
    		InternetCharges IC = new InternetCharges(pkg, hours); 
     
     
    		System.out.printf("Your total charges for this month are: $%,.2f\n" , IC.getCharges()); 
    	}
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Trying to figure out how to use char in this problem.

    Quote Originally Posted by RepersGhost View Post
    ... it uses the int pkg instead of the char that it is supposed to....
    Well, as you have, no doubt, discovered, the Scanner class does not have a nextChar() method.

    So, here's something to try:

    In the InternetCharges class, change pkg to be a char and change pkg parameters and return data types in methods of the class from int to char. In the calculations, instead of having if (pkg == 1), you will have if (pkg == 'A'). Stuff like that.

    Then...

    In main(), use the Scanner next() or Scanner nextLine() method to read user input into a String variable. Then use the String charAt(0) method to obtain a char to use in the InternetCharges constructor.


    Cheers!

    Z

  3. The Following User Says Thank You to Zaphod_b For This Useful Post:

    RepersGhost (March 7th, 2013)

  4. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trying to figure out how to use char in this problem.

    Ok thanks everything you said made sense and I could do except for the "Then use the String charAt(0) method to obtain a char to use in the InternetCharges constructor." Just not sure how to exactly do that (I rarely have had to use the char so far).

  5. #4
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: Trying to figure out how to use char in this problem.

    To read in a single character using the scanner:

    Scanner keyboard = new Scanner(System.in);
    char pKg= keyboard.next().charAt(0);

  6. The Following User Says Thank You to Starstreak For This Useful Post:

    RepersGhost (March 7th, 2013)

  7. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trying to figure out how to use char in this problem.

    Thank you very much the program works the way it should now. Just need to make an invalid output and it should be perfect.

Similar Threads

  1. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 10:04 AM
  2. char problems
    By whome in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 25th, 2012, 09:00 PM
  3. char to int cov problem
    By jack_nutt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 16th, 2011, 11:40 AM
  4. Vietnamese Character Problem in JSP Report. Chinese and Thai Char OK
    By mrvora in forum JavaServer Pages: JSP & JSTL
    Replies: 7
    Last Post: October 23rd, 2009, 02:35 AM
  5. simple problem w/ appelets which i cant figure out
    By JavaGreg in forum Java Applets
    Replies: 7
    Last Post: August 15th, 2009, 07:22 PM