Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2013
    Rep Power

    Default Simple program using input dialog and message ... And just say hi!

    Hi all! I had a brutal 101 on programming when I was working on a robot for college. Besides trying to self-taught (and ultimately forgot most of it) Python, this is my first real attempt on learning programming. I'm practicing Java partly to prepare for an upcoming class on C++, but mostly it seems like anopportunity to get into good habit. So here's my problem.

    Your program should read your name, then show a sequence of two dialog boxes:
    1) First, an input dialog box that asks: "What would you like me to do?"
    2) Then a message dialog box that says: "I'm sorry, your name. I'm afraid I can't do that."

    What I have so far:

    import javax.swing.JOptionPane;
    import javax.swing.ImageIcon;

    public class Review {

    public static void main(String[] args) {
    String name = JOptionPane.showInputDialog("What is your name?");
    String task = JOptionPane.showInputDialog("What would you like me to do?");
    JOptionPane.showMessageDialog(null, name);

    In the last line (JOptionPane), when the program runs it only shows a message box with my name on it. I don't know how you can place "I'm sorry," and ". I'm afraid I can't do that." with input name in between.

    If I modified the last line to have (null, "I'm sorry"), then the message box only shows "I'm sorry" on it. I tried to place the input name inside, but it always showed up as errors.

    I have tried System.out.println("I'm Sorry"); in before and System.out.println("I'm afraid I can't do that."), but those two messages show up in the console instead.

    I have tried to put another JOptionPane inside the JOptionPane (JOptionPaneception?), and it obviously didn't work.

    Any help and explanation (or just rewrite the code, if that is more convenient) are greatly appreciated. Thanks!

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Rep Power

    Default Re: Simple program using input dialog and message ... And just say hi!

    You can concatenate Strings:
    Java Code:
    String answer = "Hello " + name + "! This is one String";
    This is the lazy way, normally you'd use a StringBuilder.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Simple program using input dialog and message ... And just say hi!

    Unless it is (as in your example) a single line concatenation in which case that is (IMO) preferable to showing the creation of a StringBuilder.
    I say "showing the creation of a StringBuilder" because the compiler actually does that under the hood.
    Multi-line concatenation (eg concatenating a String in a loop) should involve a StringBuilder, of course.
    Java Code:
    String something = "";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < 10; i++) {
       something += "blah " + i + ";";
    For the String concat line the compiler will create a StringBuilder each time around the loop (not a Good Thing).
    The StringBuilder version on the other hand has the one object which does all the work.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Need Help With Simple Input Output Program
    By Mastermime in forum New To Java
    Replies: 12
    Last Post: 12-18-2012, 03:49 AM
  2. How to put an input dialog box in a combo box program
    By Shenaniganizer in forum New To Java
    Replies: 1
    Last Post: 11-13-2012, 05:41 PM
  3. I cant get this Message dialog to appear.
    By dienesh77 in forum New To Java
    Replies: 2
    Last Post: 03-07-2011, 10:03 AM
  4. Replies: 6
    Last Post: 02-24-2009, 02:50 PM
  5. JOptionPane - message dialog
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-17-2007, 09:11 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