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

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

1. ## 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());
}
}```  Reply With Quote

3. ## Re: Trying to figure out how to use char in this problem. 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  Reply With Quote

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

RepersGhost (March 7th, 2013)

5. ## 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).  Reply With Quote

6. ## 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);  Reply With Quote

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

RepersGhost (March 7th, 2013)

8. ## 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.  Reply With Quote