Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How do you make JOptions compile?

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How do you make JOptions compile?

    Here is my code so far to ask users to enter lastnames of three triathletes and their seconds. However, the code will not compile. Thank you so much!
    import javax.swing.JOptionPane;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class TriathletePanel
    {
        String str, lasNameStr, secondStr, result;
        int num, again, lastName;
     
        lastNameStr = JOptionPane.showingInputDialog ("Enter Last Name: ");
        lastName = Char.parsechar(lastNameStr);
        str = str.toUpperCase();
        str = str.replaceAll("\\W","");
        secondStr = JOptionPane.showingInputDialog ("Enter Seconds: ");
        num = Integer.parseInt(secondStr);
     
        lastNameStr = JOptionPane.showingInputDialog ("Enter Last Name: ");
        lastName = Char.parsechar(lastNameStr);
        str = str.toUpperCase();
        str = str.replaceAll("\\W","");
        secondStr = JOptionPane.showingInputDialog ("Enter Seconds: ");
        num = Integer.parseInt(secondStr);
     
        lastNameStr = JOptionPane.showingInputDialog ("Enter Last Name: ");
        lastName = Char.parsechar(lastNameStr);
        str = str.toUpperCase();
        str = str.replaceAll("\\W","");
        secondStr = JOptionPane.showingInputDialog ("Enter Seconds: ");
        num = Integer.parseInt(secondStr);
    Last edited by copeg; July 17th, 2011 at 09:26 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do you make JOptions compile?

    For future reference, please wrap your code in the code tags and please post the full error message you receive. As is, your class is not enclosed in matching curly braces.

  3. The Following User Says Thank You to copeg For This Useful Post:

    GirlCoder (July 19th, 2011)

  4. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How do you make JOptions compile?

    If nothing else, you'll be getting a NullPointerException because you keep doing this:
    str = str.toUpperCase();
    str = str.replaceAll("\\W","");
    The problem is, 'str' is never initialized, so is always null...

  5. The Following User Says Thank You to dlorde For This Useful Post:

    GirlCoder (July 19th, 2011)

  6. #4
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How do you make JOptions compile?

    I am new to Java. Sorry for the confusion. Here is the full code and it is supposed to look like this but it will not compile. I am not sure why.
    import javax.swing.JOptionPane;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Scanner;
    import java.text.DecimalFormat;
     
    public class TriathletePanel extends JPanel
    {  
        public static void main (String[] args)
        {
      String lastNameStr, secondStr, result;
         int num, again, lastName, seconds, Kmh;
         Scanner scan = new Scanner(System.in);
     
        for (lastName = 0; lastName <= 3; lastName ++)
        {
        lastNameStr = JOptionPane.showInputDialog ("Enter Last Name: ");
        lastName = Integer.parseInt(lastNameStr);
        secondStr = JOptionPane.showInputDialog ("Enter Seconds: ");
        num = Integer.parseInt(secondStr);
     
        result = "Triathlete" + lastName + "Seconds" + num;
     
       JOptionPane.showMessageDialog(null, result);
      }
            seconds = num;
            Kmh = 10000/seconds;
     
            DecimalFormat fmtK = new DecimalFormat("0.##");
            DecimalFormat fmtS = new DecimalFormat("###,###.##");
            String output = fmtK.format(Kmh + seconds);
     
     
    }
     
       private JLabel inputLabel, outLabel, resultLabel, NameListener, ButtonListener;
     
        public TriathletePanel()
            {
            setBackground (Color.blue);
            setPreferredSize(new Dimension(150, 200));
            setLayout (new GridLayout (5, 3));
            setBorder ( BorderFactory.createLineBorder (Color.red, 2));
     
     
        }
     
    }

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: How do you make JOptions compile?

    it will not compile
    Please copy and paste here the full text of the error messages.

Similar Threads

  1. Cannot get to compile
    By theoneyouenvy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 19th, 2011, 10:17 PM
  2. Cannot get to compile
    By Goff256 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 19th, 2011, 01:09 PM
  3. [SOLVED] I CANT COMPILE
    By savvas in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 31st, 2011, 11:34 AM
  4. *why won't this compile?*
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 2nd, 2010, 07:18 PM
  5. need help to compile
    By hardwarewizard in forum Java Theory & Questions
    Replies: 0
    Last Post: February 14th, 2010, 10:03 AM