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

• March 6th, 2013, 10:12 PM
RepersGhost
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

Code Java:

```  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; } }```

Code Java:

```  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()); } }```
• March 6th, 2013, 10:51 PM
Zaphod_b
Re: Trying to figure out how to use char in this problem.
Quote:

Originally Posted by RepersGhost
... 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
• March 7th, 2013, 01:38 AM
RepersGhost
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).
• March 7th, 2013, 01:52 AM
Starstreak
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);
• March 7th, 2013, 02:46 AM
RepersGhost
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.