I am using JOptionPane

I am trying to use char like I usually use doble but doesnt work

Here is using double:

lb = JOptionPane.showInputDialog("Enter the pounds: ");

pounds = Double.parseDouble(lb);

Here is using char:

message = JOptionPane.showInputDialog("Welcome to Converting Weight System!\n"

+"Please enter P to convert your kilograms weight to Pounds "

+"or K to convert your pounds weight to Kilograms: ");

choice = Character.parseChar(message);

Probably when you use char is different but I dont know how, can some one help me please.

[683 byte] By [Maria81a] at [2007-11-26 21:03:53]
# 1
Can some one help me with this please
Maria81a at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 2

If you look at the Java API's for Character, there is no parseChar method. Since message is a String I would do something like:

message = JOptionPane.showInputDialog("Welcome to Converting Weight System!\n" +

"Please enter P to convert your kilograms weight to Pounds " +

"or K to convert your pounds weight to Kilograms: ");

choice = message.charAt(0);

This will get the first char of the message String.

ignignokt84a at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 3

I didnt know that.

This is my program.

So if the user enter P will go to getPounds and if enter a k will go to getKilogram?

import javax.swing.*;

public class ConvertWeight

{

public void determineWeight()

{

String message, kilo, lb, again, getKilograms, getPounds, result;

doublekilograms, pounds, continu, w;

charchoice, K, k, P, p, Y, y;

message = JOptionPane.showInputDialog("Welcome to Converting Weight System!\n"

+ "Please enter P to convert your kilograms weight to Pounds "

+ "or K to convert your pounds weight to Kilograms: ");

choice = Character.parseChar(message);

do{

if (choice=='K' || choice=='k')

{

kilo = JOptionPane.showInputDialog("Enter the kilograms: ");

kilograms = Double.parseDouble(kilo);

w=getPounds(kilograms);

result=String.format("You enter %.3d in kilograms and in pounds is: %.3f", kilograms, w);

}

else

{

lb = JOptionPane.showInputDialog("Enter the pounds: ");

pounds = Double.parseDouble(lb);

w=getKilograms(pounds);

result=String.format("You enter %.3d in pounds and in pounds is: %.3f", pounds, w);

}

again = JOptionPane.showInputDialog("Please enter P to convert your kilograms weight to Pounds "

+ "or K to convert your pounds weight to Kilograms: ");

continu = Integer.parseInt(again);

}while (continu=='Y' || continu=='y');

JTextArea outputArea = new JTextArea(9, 9);

JScrollPane scroller = new JScrollPane(outputArea);

outputArea.setText(result);

JOptionPane.showMessageDialog(null, scroller, "Weight Results", JOptionPane.PLAIN_MESSAGE);

}

public double getPounds(double kilograms)

{

double weight;

weight=kilograms*(11/5);

return weight;

}

public double getKilograms(double pounds)

{

double weight;

weight=pounds/(11/5);

return weight;

}

}

Maria81a at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 4

ok I change it, compile fine, execute fine but when I enter p or k on the first box dialog ends and gives me

Exception in thread "main" java.lang.NumberFormatException: For input string: "p"

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java:447)

at java.lang.Integer.parseInt(Integer.java:497)

at ConvertWeight.determineWeight(ConvertWeight.java:35)

at ConvertWeightTest.main(ConvertWeightTest.java:13)

Process completed.

so doesnt go to getPound or getKilograms

did some one knows why?

Maria81a at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 5

Because you can't parse "P" or "K" to an integer. That's what the value of "again" will be after the input dialog. And "continu" will never be assigned the correct value, nor is it the correct data type, you probably want to check that too. In fact, you can get rid of "continu" altogether and just loop while "again" is "Y" or "y". But don't forget to ask whether or not to continue, you aren't doing that either...

BinaryDigita at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 6

But if I put again as a char, gives me an error and I already change continu to character.

this is what I have

import javax.swing.*;

public class ConvertWeight

{

public void determineWeight()

{

String message, kilo, lb, again, getKilograms, getPounds, result;

doublekilograms, pounds, w;

charchoice, continu, K, k, P, p, Y, y;

message = JOptionPane.showInputDialog("Welcome to Converting Weight System!\n"

+ "Please enter P to convert your kilograms weight to Pounds "

+ "or K to convert your pounds weight to Kilograms: ");

choice = message.charAt(0);

do{

if (choice=='K' || choice=='k')

{

kilo = JOptionPane.showInputDialog("Enter the kilograms: ");

kilograms = Double.parseDouble(kilo);

w=getPounds(kilograms);

result=String.format("You enter %.3d in kilograms and in pounds is: %.3f", kilograms, w);

}

else

{

lb = JOptionPane.showInputDialog("Enter the pounds: ");

pounds = Double.parseDouble(lb);

w=getKilograms(pounds);

result=String.format("You enter %.3d in pounds and in pounds is: %.3f", pounds, w);

}

again = JOptionPane.showInputDialog("If you want to continue. Please enter P to convert your kilograms"

+ " weight to Pounds or K to convert your pounds weight to Kilograms: ");

continu = again.charAt(0);

}while (continu=='Y' || continu=='y');

JTextArea outputArea = new JTextArea(9, 9);

JScrollPane scroller = new JScrollPane(outputArea);

outputArea.setText(result);

JOptionPane.showMessageDialog(null, scroller, "Weight Results", JOptionPane.PLAIN_MESSAGE);

}

public double getPounds(double kilograms)

{

double weight;

weight=kilograms*(11/5);

return weight;

}

public double getKilograms(double pounds)

{

double weight;

weight=pounds/(11/5);

return weight;

}

}

And is stating the same, doesnt matter what letter I put.

Exception in thread "main" java.lang.NumberFormatException: For input string: "p"

at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)

at java.lang.Integer.parseInt(Integer.java:447)

at java.lang.Integer.parseInt(Integer.java:497)

at ConvertWeight.determineWeight(ConvertWeight.java:35)

at ConvertWeightTest.main(ConvertWeightTest.java:13)

Process completed.

Maria81a at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 7

Interesting, I don't get that stack trace and you aren't calling Integer.parseInt anywhere in your newly-posted code. After entering 'K' and the number of kilograms, I get an IllegalFormatException on this line (notice the line numbers in the stack trace):

result=String.format("You enter %.3d in kilograms and in pounds is: %.3f", kilograms, w);

The javadoc says it throws that exception when "a format specifier that is incompatible with the given arguments." You can read more here:

http://java.sun.com/j2se/1.5.0/docs/api/index.html

In other words, your format string is lying to that method about the type of argument you're passing. "d" means integer, not double.

BinaryDigita at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 8

Oh yeah, I forget to change that. Before I had pounds and kilograms as integer and I changed to double.

But still the same message.

This time instead of puting p or k, I put 9 and ask me the next quiestion which is "Enter the pounds: " and ends with out going to getKilograms. And gives me this message

Exception in thread "main" java.util.IllegalFormatPrecisionException: 3

at java.util.Formatter$FormatSpecifier.checkInteger(Formatter.java:2891)

at java.util.Formatter$FormatSpecifier.<init>(Formatter.java:2642)

at java.util.Formatter.parse(Formatter.java:2479)

at java.util.Formatter.format(Formatter.java:2413)

at java.util.Formatter.format(Formatter.java:2366)

at java.lang.String.format(String.java:2770)

at ConvertWeight.determineWeight(ConvertWeight.java:54)

at ConvertWeightTest.main(ConvertWeightTest.java:13)

Process completed.

Maria81a at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 9
I have already given you enough information to figure that out.
BinaryDigita at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...
# 10
Opps I forget to tell you that I got it know.Thanks
Maria81a at 2007-7-10 2:36:38 > top of Java-index,Desktop,Core GUI APIs...