Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve figured out how to get my java program to work in the console. I can\'t fi

ID: 664079 • Letter: I

Question

I've figured out how to get my java program to work in the console. I can't figure out how to get it to do the same using the JOptionPane.class

Below is what I have for the console. The program takes any number you input, breaks it into an array and then adds them together.

Thanks for your help in advanced.

import java.util.Scanner;

public class inputOutputSum
{
public static void main(String[] args)
{
System.out.println("This program takes any number you enter and sums the individual intigers.");
Scanner kb = new Scanner(System.in);
// asks for the number
System.out.println("Enter the number you want to sum?");
String input = kb.nextLine();
kb.close();
// calls the routine below and prints the number and its sum
System.out.printf("The sum of %s is %d%n", input, sum(input));
}
// this section breaks the number into an array of individual intergers and adds them together
private static int sum(String input)
{
int result = 0;
char[] chs = input.replaceAll("[^\d]", "").toCharArray();
for (char c : chs)
{
result += Integer.parseInt(String.valueOf(c));
}
return result;
}
}

Explanation / Answer

import javax.swing.JOptionPane; public class sumofnumbers { public static void main(String[] args) { String input; int num = 0; JOptionPane.showMessageDialog(null, "This program is designed to calculate the sum of consecutive numbers starting at 1 " + " and ending at a certain point."); input = JOptionPane.showInputDialog("Please choose a number as the ending point?"); if (isANum(input)){ num = Integer.parseInt(input); sum(num); JOptionPane.showMessageDialog(null, "The sum of these numbers is " + sum(num) + "."); } else JOptionPane.showMessageDialog(null, "That is not a digit. Please try again."); System.exit(0); }//main private static boolean isANum (String input) { boolean numSoFar = true; for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote