Reputation: 123
You all were so helpful yesterday in getting over my first hump in this problem that I wanted to see if there's a way I can modify my final product (Apologies if my formatting is off - Still trying to get indentations correct in my IDE.
import javax.swing.JOptionPane;
public class NumberLoops {
public static void main(String[] args) {
String strNum1;
String strNum2;
int intNum;
boolean isValid = true;
boolean isError = false;
strNum1 = JOptionPane.showInputDialog ("Enter Number String");
for (int i=0; i<strNum1.length(); i++) {
char c = strNum1.charAt(i);
if (c == '-'){
JOptionPane.showMessageDialog(null, "Negative Digit Found - Enter Positive Numbers Only", "Error", JOptionPane.INFORMATION_MESSAGE);
isValid = false;
break;
}
}
if (isValid){
for (int i=0; i<strNum1.length(); i++) {
char c = strNum1.charAt(i);
intNum = Character.getNumericValue(c);{
if (intNum > 0 && intNum <= 9){
isError = true;
break;
}
}
}
}
if (isError){
int aDigit,totalNum=0;
char chDigit;
strNum2 = String.valueOf(strNum1);
for (int count=0; count<strNum1.length();count++){
chDigit = strNum2.charAt(count);
aDigit = Character.getNumericValue(chDigit);
totalNum += aDigit;
if (aDigit > 0 && aDigit <= 9){
System.out.print(aDigit + " ");
}
}
System.out.print(" " + "The sum is: " + totalNum);
}
}
}
My question concerns the last loop. It functions as desired by printing to the console if I enter say 123123 into the message prompt, it lists that string as 1 2 3 1 2 3 and then the total of 12.
But that is in the console itself. What I'm trying to wrap my mind around is getting it to display in a message box instead of the console.
I'm guessing I need to create a new string like (incoming pseudocode):
if (aDigit > 0 && aDigit <= 9){
strNum3 = everycharinStr2 + " "
Which is the part I guess I'm not grasping.
FYI, this is a homework assignment so I don't necessarily want an outright answer, but I feel I am so close that I need some extra eyes on it to see if there's anything I can do. (I have read up on arrays and such but we aren't at that point yet so I don't think I'll go down that road quite yet.)
Upvotes: 4
Views: 107
Reputation: 32145
You only have to make a little change in your code:
strNum2 = String.valueOf(strNum1);
String resultString = "";
for (int count=0; count<strNum1.length();count++){
chDigit = strNum2.charAt(count);
aDigit = Character.getNumericValue(chDigit);
totalNum += aDigit;
if (aDigit > 0 && aDigit <= 9){
System.out.print(aDigit + " ");
resultString += aDigit + " ";
}
}
JOptionPane.showMessageDialog(null, resultString);
Create a new String
and in each iteration append the number to this String
, then show it in a dialog.
Upvotes: 2
Reputation: 109547
The errors are commented out.
//if (intNum > 0 && intNum <= 9){
if (intNum < 0 || intNum > 9){
isError = true;
break;
}
//if (aDigit > 0 && aDigit <= 9){
if (aDigit >= 0 && aDigit <= 9){
System.out.print(aDigit + " ");
//resultString+ = aDigit + " ";
resultString += "" + aDigit;
}
//if (isError) {
if (!isError) {
Upvotes: 1
Reputation: 4998
Just use a JLabel
!
Since your already using swing, this would be your best choice. But then you'd need a frame and panel, so if you don't already have that, don't do this.
JLabel label = new JLabel()
label.setText("Random Text!")
You can also do this, with a pop up:
JOptionPane.showMessageDialog(null, "My String!");
Upvotes: 0