Hey, I have completed my code for Java and I have to put it in GUI, problem is I dont know a thing about GUI or if my program works correctly with it or not.

Here is my code and what the code does is you type in random numbers (Example 234) and it will tell you what the sum of those digits are (the answer it would give is 9) then it will give you the mod 9 of the answer.

Will this code work with GUI?

import javax.swing.JOptionPane;

public class Sumofdigits 
{


	public static void main(String[] args) 
	{
		// Set variables 
		long b;
		
		// Make the GUI
		String inputValue = JOptionPane.showInputDialog(null, "Enter digits", "Compute the sum of the digits", JOptionPane.QUESTION_MESSAGE);
		long n = Long.parseLong(inputValue);		
		
		JOptionPane.showMessageDialog(null,n);
		
		// Compute n % 9
		b = n % 9;
		
	}
	
	public static long sumDigits(long n)
	{
		// Set sum equal to 0
		long sum = 0;
		
		// Make a while loop
		while(n > 0)
		{
			// Extract the last digit
			sum = sum + n % 10;
			
			// Delete the last digit
			n = n / 10;
		}
		return sum;
		
		
	}

}

Thank you to anyone that can help me out with this problem.

Recommended Answers

All 4 Replies

Member Avatar for ztini

Rather than parsing your input right away, consider using .toCharArray() from the String class to break out the digits of the input into individual slots in an array.

This will certainly make it easier to then calculate the sum:

int sum = 0;
for (char digit : inputValue.toCharArray()
sum += (int) digit;


A couple of other things:
You created the method sumDigits, but you never call it.
Do the math as you described..sum the digits, perform modulus, then do your second dialog.

Looks good so far, post your final result when you get done.

Thanks for the help but I kinda have to do my code this way, see im in an introductory course for java in college and this is what the question is asking out of the book:

Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(long n)

(Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 (=4). To remove 4 from 234, use 234 / 10 (=23). Use a loop to repeatedly extract and remove the digit until all the digits are extracted. Write a test program that prompts the user to enter an integer and displays the sum of all its digits in GUI.)

I hope this helps clarify things to help out with my code.

Member Avatar for ztini

just change line 16 to:

JOptionPane.showMessageDialog(null,sumDigits(n));

Thank you very much ztini!!! You helped me out immensely!!! Here is the final code

import javax.swing.JOptionPane;

public class Sumofdigits 
{


	public static void main(String[] args) 
	{
		
		// Make the GUI
		String inputValue = JOptionPane.showInputDialog(null, "Enter digits", "Compute the sum of the digits", JOptionPane.QUESTION_MESSAGE);
		long n = Long.parseLong(inputValue);		
		
		JOptionPane.showMessageDialog(null,sumDigits(n));
		
		// Mod 9
		long b = n % 9;
	
		JOptionPane.showMessageDialog(null,sumDigits(b));
		
		
	}
	
	public static long sumDigits(long n)
	{
		// Set sum equal to 0
		long sum = 0;
		
		// Make a while loop
		while(n > 0)
		{
			// Extract the last digit
			sum = sum + n % 10;
			
			// Delete the last digit
			n = n / 10;
		
		}
		return sum;
		
		
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.