Hello to anyone who can help me.
I'm a freshman in comp sci and here is my problem:
I need to have the user type in a number in a JOptionPane input box, and I need to change the number they input into a double.
Here's what I have, it doesn't work:

import javax.swing.JOptionPane;
public class Transfer
{
public static void main(String[] args)
{
Person jeff = new Person(300, 1000);
Person kwame = new Person(150, 0);
jeff.lookInWallet();
jeff.lookInAccount();
kwame.lookInWallet();
kwame.lookInAccount();
[I]String t = JOptionPane.showInputDialog([/I][I]"To:"[/I][I]);
String f = JOptionPane.showInputDialog("From:"[/I][I]);
String a = OptionPane.showInputDialog("Amount (In Dollars.Cents):"[/I][I]);[/I]
if(t = "jeff")
{
new Check(jeff, kwame, a);
}
else if(t = "kwame")
{
new Check(kwame, jeff, a);
}
else
System.out.print("No person by that name, make sure you are case-sensitive (jeff, not Jeff)");
jeff.lookInWallet();
jeff.lookInAccount();
kwame.lookInWallet();
kwame.lookInAccount();
}
}

Ignore everything except the italicized area.

Also, if anyone knows how to have the user input a string, and then from that string, set the parameters for a new object, please tell me.
Thanks.

Recommended Answers

All 9 Replies

To change a string into a double try:
double x=Double.parseDouble(someString);

Is that what you need?

To change a string into a double try:
double x=Double.parseDouble(someString);

Is that what you need?

Thanks, that works fine.
Is there any way to make an input string become the name of a new object?
Ex:

String [I][U]t[/U][/I] = JOptionPane.showInputDialog("To:");
Person [I][U]t[/U][/I] = new Person(300, 1000);

Or more importantly,

String t = JOptionPane.showInputDialog("To:");
String f = JOptionPane.showInputDialog("From:");
new Check([I][U]t[/U][/I], [I][U]f[/U][/I], x);

**********|**|
**********|**|
*****Object Person

Ignore asterisks, the lines are to show that t & f are of the Person class.

Thanks in advance.

Update!
This isn't working!!!

String to = JOptionPane.showInputDialog("To:");
String from = JOptionPane.showInputDialog("From:");
String a = JOptionPane.showInputDialog("Amount (In Dollars.Cents):");
double x=Double.parseDouble(a);
if(to=="jeff" || from=="kwame")
new Check(jeff, kwame, x);
else if(to=="Jeff" || from=="Kwame")
new Check(jeff, kwame, x);
else if(to=="kwame" || from=="jeff")
new Check(kwame, jeff, x);
else if(to=="Kwame" || from=="Jeff")
new Check(kwame, jeff, x);

Yes, I created the jeff and kwame Person objects already.
If someone knows why this isn't working, please tell me!!!
Thanks.

Ok, obvious answer...
You can't compare Strings with ==.
Strings are objects so you need to use to.equals("jeff"), or "jeff".equals(to).
If you wanted to use !=, you would do !to.equals("jeff") to swap the value from true to false, or false to true.

Ok, obvious answer...
You can't compare Strings with ==.
Strings are objects so you need to use to.equals("jeff"), or "jeff".equals(to).
If you wanted to use !=, you would do !to.equals("jeff") to swap the value from true to false, or false to true.

Ok, thanks, so I don't have to create a method "equals()"?

You don't need to create an equals method.
If you look in the API, the Object class has an equals method, and the String class has an equals which overrides this.
So for your purpose you don't need to make a new one, but you could if you wanted.

I'm not sure precisely what you're trying to do with the other thing.
I think you're trying to use the same variable for two different things, so the compiler won't know which is which.
I think you're best to just use separate variable, say tStr for the t String and tPer for the t Person.

No, no, no.
I'm trying to have the user input someone's name, then create a new Person object named whatever they input.
Ex:
You enter "Dave" in the input box.
I want the program to do this:
Person Dave = new Person();
or if you entered "jason"
Person jason = new Person();
Then I need a new Check object to be created with the person you entered.
Input by user:
To: Dave
From:jason
Amount:20.52
Program:
new Check(Dave, jason, 20.52)

How can I get this to work???

I have attached my Person and Check classes, and the Transfer program I made. This might show you a little better what I'm trying to do.

Again, I want the user to put in the name of who they're writing the check to, then who it's from, then the amount.
A new check object should be made based on their data.
Then their total balances are displayed.

As far as I know, I don't think you create a variable name that way.
I can't see why you'd need to. You're program looks quite simple, you don't need to do it the way your trying.

I have a better suggestion, in your Person class, create a private String field called name.
So that any person object has:
1) Name
2) Balance
3) Cash

When you create your Person's, you would do...
Person per1 = new Person("Jeff",300.0, 1000.0);
Person per2 = new Person("Kwane",150.0, 0.0);

If you wanted to get the name of the person, just use a getName() method to return the name field.
Eg per2.getName(); would return "Kwane"

Your idea is interesting, but confusing and probably not possible.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.