Results 1 to 3 of 3
- 01-29-2013, 09:48 AM #1
Member
- Join Date
- Jan 2013
- Posts
- 1
- Rep Power
- 0
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 java.net.URL;
import javax.swing.ImageIcon;
public class Review {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog("What is your name?");
System.out.println(name);
String task = JOptionPane.showInputDialog("What would you like me to do?");
System.out.println(task);
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!
- 01-29-2013, 09:59 AM #2
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";
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 01-29-2013, 11:16 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
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 + ";"; sb.append("blah"); sb.append(i); sb.append(";"); }
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
-
Need Help With Simple Input Output Program
By Mastermime in forum New To JavaReplies: 12Last Post: 12-18-2012, 04:49 AM -
How to put an input dialog box in a combo box program
By Shenaniganizer in forum New To JavaReplies: 1Last Post: 11-13-2012, 06:41 PM -
I cant get this Message dialog to appear.
By dienesh77 in forum New To JavaReplies: 2Last Post: 03-07-2011, 11:03 AM -
Error message in executing a simple program from DOS window
By betelgeuse in forum New To JavaReplies: 6Last Post: 02-24-2009, 03:50 PM -
JOptionPane - message dialog
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 10:11 AM
Bookmarks