4

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.)

2
  • Your isError variable name is misleading. You set isError to true and break out if the number is between 1 and 9 inclusive. Then you display the numbers and sum if isError is set to true. Very confusing. If I had to guess, what you really mean to do is set isError to true if you encounter a character that is NOT between 1 and 9 inclusive and then only display the numbers and sum if isError is false. Commented Jun 5, 2015 at 16:38
  • Ah, I just noticed that after I came back from lunch and looked at what I had. You are right, that is confusing! I will correct this. It is in fact supposed to error only if a number is NOT between 1 and 9. Commented Jun 5, 2015 at 17:18

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is marvelous, exactly what I was trying to achieve. I modified it even further to include the total when the numbers are added up.
1

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) {

Comments

0

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!");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.