• Post Reply Bookmark Topic Watch Topic
  • New Topic

New at this...need help  RSS feed

 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a program in which a dialog box will pop up three different times asking a users first name, second name, and age. then another three pop up one after the other asking the same three questions for a second user. after they are complete the answers in regards to age are compared and the final dialog box says something like bob hope is older than dan dunn. basically i am wondering how i write this code, so if anyone can direct me in the right direction that would be great. thank you so much. however you can help will be nice..thanks again!!
 
Vijayendra V Rao
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its pretty easy to pop up an input dialog box and then accept an input from the user. To pop-up a dialog box, use the following syntax:



You can do this and collect data from two users and then do that comparison stuff and pop up another dialog box to the user with something like this:

 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your reply ok this is what i have so far and i know its not right, but if you could point me to where i need to change something and to what that would be awesome. if anyone could help that would be great!! im just not sure how to integrate the age to the other two prompts and how the pc knows its for two different people. help??

// This program prompts the user for their first
// and last name and their age and compare the two values

import javax.swing.*;

public class HW1 {

// access modifier
public static void main( String[] args ) {

string name= JOptionPane.showInputDialog ("Please enter your first name!")

string name= JOptionPane.showInputDialog ("Please enter your last name!")

val1 = Integer.parseInt( JOptionPane.showInputDialog(
"Please enter your age!" ) );

string name= JOptionPane.showInputDialog ("Please enter your first name!")

string name= JOptionPane.showInputDialog ("Please enter your last name!")

val2 = Integer.parseInt( JOptionPane.showInputDialog(
"Please enter your age!" ) );


// compare the values
// boolean comparison statement
if ( val1 > val2 )
// give the statement that will be executed ONLY IF
// val1 is greater than val2
message = "First value is greater than the second.";

if ( val1 < val2 )
message = "First value is less than the second.";

if ( val1 == val2 )
message = "First value is same as the second.";

// report the result back to the user
JOptionPane.showMessageDialog( null, message );

// quit the program
System.exit( 0 );
}
}
 
Richard Bradford
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what I would do in this instance is create a class called 'User' which has private fields for firstName, secondName and age accessed via getFirstName(), setFirstName(String name) methods etc and create instances of this class for each user.

The User classes could then be populated by:
bob.setName(JOptionPane.showInputDialog ("Please enter your first name!"));
etc

Though ideally you would probably move the gathering of user data to a seperate method, something like getUserData() which creates the 3 dialogs and returns a User object
example:
User bob = getUserData();

I would then have a method which compares the users ages, probably in the User class itself. Somthing like:
boolean isOlderThan(User u)
which compares the ages of users ie to test if bob is older than john:
boolean isOlder = bob.isOlderThan(john);

Hope this helps.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

- there is no type "string" only "String" your program will not compile like this

if you do this:


1: You create a variable named "name" and assign the value returned from the Dialog for the first name.
2: You create a variable named "name" and assign the value returned from the Dialog for the last name.

there are several problems with this: at the second line the variable named "name" is already declared and the compiler will not let you declare another variable with the same name.
and also if you assign two values to the same variable, then you will loose the first value. So you should use different names (e.g. firstName1, lastName1, firstName2, ...)

try to change your code, compile it and tell us what happens :-)

pascal
 
Holly Leery
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I changed the code to try and fit what you are
telling me, I'm just not to good at it yet. I have to do a lot of trial and error type stuff ya know? So here is the new one, what do you think?
Oh and also thank you for your replies I really appreciate the help.
// This program prompts the user for their first
// and last name and their age and compare the two values

import javax.swing.*;

public class HW1 {

// access modifier
public static void main( String[] args ) {

User 1 = getUserData();

1.setName(JOptionPane.showInputDialog ("Please enter your first name!"));

1.setName(JOptionPane.showInputDialog ("Please enter your last name!");

1.setName(JOptionPane.showInputDialog ("Please enter your age!");


User 2 = getUserData();

2.setName(JOptionPane.showInputDialog ("Please enter your first name!"));

2.setName(JOptionPane.showInputDialog ("Please enter your last name!");

2.setName(JOptionPane.showInputDialog ("Please enter your age!");


// compare the users
// boolean comparison statement
if ( User 1 > User 2 )
// give the statement that will be executed ONLY IF
// User 1 is greater than User 2
message = "User 1 is older than User two.";

if ( User 1 < User 2 )
message = "User 2 is older than User 1.";

if ( User 1 == User 2 )
message = "User 1 is the same age as User 2.";

// report the result back to the user
JOptionPane.showMessageDialog( null, message );

// quit the program
System.exit( 0 );
}
}
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
countrygirl2149,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's another programming tip...

don't try and do everything at once. It appears to me you wrote your original program in its entirety, then tried to compile it. That is a sure-fire way to give yourself a heart attack when you see 800 error messages.

I would approach this a little slower. first, write a program that pops up a dialog box asking for a first name. Once i got that working, i might try to add code that would print the entered name back out. Even if this isn't part of the assignment, it's a good test to make sure your input box is working how you think it is.

AFTER i get all that working, i'd add the code for the last name dialogbox. At this point, i could probably add in the code to print the first and last name.

some people will tell you to only write ONE line, then compile. that way, if an error pops up you know which line is causing it. Personally, i think that is a little extreme, but that's just my opinion.

what you might be able to do is comment out large hunks of your code, to get a little bit working at a time. as you get parts working, start un-commenting out small chunks of code, and get those to work, and so on.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Holly,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We request that display names follow the pattern FIRST_NAME + SPACE + LAST_NAME, as indicated by the registration form.

Thanks Pardner! Hope to see you 'round the Ranch!
 
Richard Bradford
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by countrygirl2149:
ok I changed the code to try and fit what you are
telling me, I'm just not to good at it yet. I have to do a lot of trial and error type stuff ya know? So here is the new one, what do you think?
Oh and also thank you for your replies I really appreciate the help.
// This program prompts the user for their first
// and last name and their age and compare the two values

import javax.swing.*;

public class HW1 {

// access modifier
public static void main( String[] args ) {

User 1 = getUserData();

1.setName(JOptionPane.showInputDialog ("Please enter your first name!"));

1.setName(JOptionPane.showInputDialog ("Please enter your last name!");

1.setName(JOptionPane.showInputDialog ("Please enter your age!");


User 2 = getUserData();

2.setName(JOptionPane.showInputDialog ("Please enter your first name!"));

2.setName(JOptionPane.showInputDialog ("Please enter your last name!");

2.setName(JOptionPane.showInputDialog ("Please enter your age!");


// compare the users
// boolean comparison statement
if ( User 1 > User 2 )
// give the statement that will be executed ONLY IF
// User 1 is greater than User 2
message = "User 1 is older than User two.";

if ( User 1 < User 2 )
message = "User 2 is older than User 1.";

if ( User 1 == User 2 )
message = "User 1 is the same age as User 2.";

// report the result back to the user
JOptionPane.showMessageDialog( null, message );

// quit the program
System.exit( 0 );
}
}

Ok, there are a few problems here.
1. you are trying to create a user by calling getUserData(); but this method is not defined. This method should contatin the JPanel stuff and return a User object (assuming you have written the User class).

public User getUserData() {
User user = new User();
user.setFirstName(JOptionPane.showInputDialog("Please enter your first name"));
...
...
...
return user;
}

Doing it this way means the same code can be reused for creating both users. Also you should name your user variables something other than 1 or 2, user1 and user2 would be better.
ie User user1 = getUserData();
2. When doing the comparison you are actually trying to declare User objects and then compare them. You need to compare the ages of your existing User objects.
[ September 10, 2004: Message edited by: Richard Bradford ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!