# Thread: Need help with my program

1. Member
Join Date
Sep 2015
Posts
3
Rep Power
0

## Need help with my program

Well hello everyone! This is my first post, and I'm very excited to be apart of this community. I'm pretty new to Java and I'm currently working to get better at it, and I'm hoping you guys can help me.

Ok, here's my problem. I'm currently going through a popular Java book, and I'm challenging myself to do the activities. One of the activities I'm stumped on. Here's the instructions:

1. a. Prompt user for his or her first name in JOptionPane.showInputDialog(); format
b. Print a message saying hello in JOptionPane.showInputDialog(); format
c. Tell the user how many characters are in his or her name

2. a. Ask the user for the year they were born.
b. Calculate and print the age of the user is going to be this year.
c. Declare a constant for the average life expectancy; set it's value to 78.7
d. Print the message that tells the user the percentage of life they have lived so far.

3. a. Generate a random number between 1 and 20
b. Pop up a dialog box telling the user that the program is thinking of a number between 1 and 20
c. Pop up a dialog box telling the user the number and how far away from the number the users guess was.

I only made it to instructions (2,d). I'm stumped on how to do the calculation and what kinda of format to use. I know the calculation is easy, but what I'm having trouble in is getting the users input, doing the calculation, then spitting that out in a percentage format. I'm using the DecimalFormat() class, but maybe you guys have a better idea. I would be so grateful if someone could give me better ideas for doing the program, and maybe a little help with Instructions 3 too. Thanks! Here's my code: (Again please correct me if there is a better way)

import javax.swing.JOptionPane;

import java.text.DecimalFormat;

public class Chapter3_ProgramingActivity2 {

public static void main(String [ ] args){

String input = JOptionPane.showInputDialog( null, "Please enter your name");

JOptionPane.showMessageDialog(null, "Pleased to meet you " + input);

int length1 = input.length();

JOptionPane.showMessageDialog(null, "Your name has " + length1 + " characters");

String input2 = JOptionPane.showInputDialog(null, "What is your year of birth?");

int year = Integer.parseInt(input2);

int age = (year - 2015);

//Getting a weird output below that has '-' in front of age
JOptionPane.showMessageDialog(null, "Your age this year will be " + age);

final double ALE = 78.5;

//Stumped here
DecimalFormat percentage = new DecimalFormat("0.0%");

//Don't know how to get percentage lived with right format
//Not sure what to output
JOptionPane.showMessageDialog(null, "You have lived " + );

}
}

As you can see, I'm not finished because I stopped at Instructions (2,d). Again, I'm beginner and would be grateful for your help. Thanks!

2. ## Re: Need help with my program

Please edit your post and wrap the code in code tags.

3. Member
Join Date
Sep 2015
Posts
3
Rep Power
0

## Re: Need help with my program

My apologies.

Java Code:
```import javax.swing.JOptionPane;

import java.text.DecimalFormat;

public class Chapter3_ProgramingActivity2 {

public static void main(String [ ] args){

String input = JOptionPane.showInputDialog( null, "Please enter your name");

JOptionPane.showMessageDialog(null, "Pleased to meet you " + input);

int length1 = input.length();

JOptionPane.showMessageDialog(null, "Your name has " + length1 + " characters");

String input2 = JOptionPane.showInputDialog(null, "What is your year of birth?");

int year = Integer.parseInt(input2);

int age = (year - 2015);

//Getting a weird output below that has '-' in front of age
JOptionPane.showMessageDialog(null, "Your age this year will be " + age);

final double ALE = 78.5;

//Stumped here
DecimalFormat percentage =  new DecimalFormat("0.0%");

//Don't know how to get percentage lived with right format
//Not sure what to output
JOptionPane.showMessageDialog(null, "You have lived " + );

}
}```

4. ## Re: Need help with my program

The DecimalFormat class's API doc has some examples on how to use it. Try writing a small, simple program and experiment with different things to see what it does.
Also do a search here to find some more examples of code that uses DecimalFormat.

5. ## Re: Need help with my program

currentyear - birthyear you have to do ..

6. Member
Join Date
Sep 2015
Posts
3
Rep Power
0

## Re: Need help with my program

Originally Posted by RamyaSivakanth
currentyear - birthyear you have to do ..
Ok that makes perfect sense. Can't believe I missed that...

I will be sure to study more on DecimalFormat;
Thanks guys.

7. ## Re: Need help with my program

For your future reference: check out the Forum Rules, especially the third paragraph.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•