need numbers to output onto a new line
hay guy...I'm having trouble with some code using an applet. I'm outputting a messgae asking a user to ener 4 number. When the numbers are entered and the user selects ok. An inforamtion messgae of the numbers entered will appear on the screen...now i have that part done but what if i want the numbers to be entered into a new line. In c++ you just enter \n. This doesn't work in this case.
Example:
user entered 1234
I want 1
2
3
4
to be outputed on my information box
Code Java:
*/import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
/**
*
*
*/
public class NewJApplet extends javax.swing.JApplet {
String number1;
/** Creates a new instance of NewJApplet */
public NewJApplet() {
number1=JOptionPane.showInputDialog(null,"Please Enter Four Digits.",
JOptionPane.QUESTION_MESSAGE);
JOptionPane.showMessageDialog(null,"The numbers you typed are.\n"+ number1+"\n");
}
}
Any help would be great!!
Re: need numbers to output onto a new line
hi . first please put your codes on highlight tags >> [highlight=java] code [/highlight]
you cant print those individualy EACH line.. because your String object(Variable) is a WHOLE string.
so when you enter, say, 1234 , the number1 does not contain individual strings.. its a one whole intact STRING
, when you wrote this statement >
Quote:
"The Numbers you typed are. "\n" + number1 + "\n",
its like your telling java ok print a NewLine("\n) and print the values of number1 and print another NewLine "\n"
you need to get each one of it.. and assign it individually into another STRING object and adding a new line
ill provide the logic of the loop for you .
Code :
for (int x = 0; x <= number1.length() - 1; x++) {
eachNumber = eachNumber + ("" + "\n" + number1.charAt(x));
}
where eachNumber increments everytime it gets the value on your STRING Object number1 and adds a "\n" which is a new line, now go ahead how are you goint to print all the value of eachNumber object.
sorry for bad english .. im a bit tired :(