Results 1 to 7 of 7
  1. #1
    Nack is offline Member
    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0

    Default 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. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need help with my program

    Please edit your post and wrap the code in code tags.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Nack is offline Member
    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0

    Default 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. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    12

    Default Re: Need help with my program

    currentyear - birthyear you have to do ..
    Ramya:cool:

  6. #6
    Nack is offline Member
    Join Date
    Sep 2015
    Posts
    3
    Rep Power
    0

    Default Re: Need help with my program

    Quote Originally Posted by RamyaSivakanth View Post
    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. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Need help with my program

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 19
    Last Post: 07-17-2015, 03:40 PM
  2. Replies: 6
    Last Post: 12-15-2013, 06:40 AM
  3. Replies: 4
    Last Post: 11-21-2012, 01:19 PM
  4. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  5. Replies: 13
    Last Post: 03-22-2011, 06:49 AM

Posting Permissions

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