I have this code and it works. It prints out the number, product, and sum, but I need it to be able to work with more than one number. For example if enter

45 6 70

I need it to say
Numbers: 45 6 70
Sum: 121
Product: 18900

How would I go about doing that?

System.out.println("\nThe numbers are: ");
		System.out.println(r);
		for (int i = 0; i < r.length(); i++) {
					
				a[i] = r.substring(i, i + 1);
				sum = sum + Integer.parseInt(a[i]);
				b[i] = Integer.parseInt(a[i]);
				product = product * b[i];
		}
				System.out.println("\nThe sum of the numbers are "  + sum );
				System.out.println("\nThe product of the num are " + product);
			
		}
		
		}
}

Sorry here is the full code

public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter Number:");
		String r = sc.nextLine();

		for (int n = 0; n < r.length(); n++) {
			if (!Character.isDigit(r.charAt(n)))
				System.out.println("ERROR");
			break;
		}

		int[] b = new int[r.length()];
		String[] a = new String[r.length()];

		int sum = 0; // The sum
		int product = 1; // The product

		{
			System.out.println("\nThe numbers are: ");
			System.out.println(r);
		}
		for (int i = 0; i < r.length(); i++) {

			a[i] = r.substring(i, i + 1);
			sum = sum + Integer.parseInt(a[i]);
			b[i] = Integer.parseInt(a[i]);
			product = product * b[i];
		}
		System.out.println("\nThe sum of the numbers are " + sum);
		System.out.println("\nThe product of the num are " + product);
	}
}

<SPAM SNIPPED>

sorry bro the output was:
The numbers are:
1223
The sum of the numbers are 8
The product of the num are 12

your code cannot take double digit numbers.
for the input like
12 10
it gives the error

Edited 5 Years Ago by peter_budo: Keep It Spam-Free - Do not mention, plug or refer to any product, service, or website you are affiliated with

Can you use the Scanner classes methods to parse the tokens/words that are entered into numbers? A word or token is a one or more characters separated by white spaces.
If not then your code needs to build the words by concatenating single characters together until it finds a white space character.

Could you give me an example of how to concatenate single characters until it finds a white space?

here is one way to find a token in a String:
begin outer loop
set an index to the first character of the token
copy the index's value to a scanning index

start inner loop
increment the scanning index
look at the character at the scanning index's location
if the char is not a digit, exit the loop you have found the end of the token
end of loop, continue at the top of the inner loop

get the substring between the starting index and the scanning index
that is the token.
Convert the token into a integer value and save it in an array
scan to the next digit
loop back to get the next token in the String.

To work out the logic, use a paper and pencil, write down a sample input string with some tokens separated by spaces and work thru the logic on how to find the first digit and then the space that ends that token and then how to get the token between those two points.

Edited 5 Years Ago by NormR1: n/a

Can you tell me why the code won't print out the sum, number, and product?

public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter Number:");
		String r = sc.nextLine();
		
String tempString = " ";   // there's no space in-between the quotes
String output = " ";

System.out.println("\nThe numbers are: " + r);

// outer loop (loops through String r)

for (int i = 0; i < r.length(); i++)

{
       // if the current character is not a space
       if (r.charAt(i) !=  ' ')
       { 
               // add the current character to tempString
               tempString = tempString + r.charAt(i);
       }
       
       // if the current character is a space
       if (r.charAt(i) ==  ' ')
       {    	   
               // to get the sum and product, we need to loop through tempString like we did for "r"
               int sum = 0;
               int product = 1;
      
               for (int j = 0; j < tempString.length(); j++)
               {        	  
            	   int digit = Integer.parseInt(tempString.substring(j,j));
            	   		sum += digit;
                       product *= digit;
                // end of the inner for loop                   
               }
               // print out the result
               output += ("Number: " + tempString);
               output += ("\nThe sum of the digits are "  + sum );
               output += ("\nThe product of the digits are " + product);

               // clear tempString so it can be used again
               tempString = " " ;
       } // end of "if the current character is a space" statement

} // end of outer loop
System.out.println(output);
	}
	}

Can you post what the program does output?

To debug your code you should separate out the substring call to a separate statement and print the String that is returned to see how your parsing of the numbers is working.
Also print out the values of the indexes as they are changed and used.

This is what the program outputs

Enter Number:
23

The numbers are: 23

Your logic needs work.
You need to find all of the digits in each string of digits before trying to convert them to an integer. To do that you need a loop the continues looking at the characters until it finds one that is not a digit. Then you have all of the digits in the number and can go convert them. Your code only looks at one character before continuing to convert it.
Say your input is this: "12 34";
Save start index of 0
look at [1] continue since its a digit
look at [2], its a space so stop
Now use the start index of 0 and the ending index of 2 for a substring to get the number.
To get the next number you need logic the same as above except you are now looking to move over the spaces to the digits. When you find a digit, save its index in the start index variable and continue as above.

This is what the program outputs

Enter Number:
23

The numbers are: 23

And that's all it prints out???
What does this print out?

System.out.println(output);

You should change the above to the following so you can see that it printed.
System.out.println("output=" + output);

Edited 5 Years Ago by NormR1: n/a

Change the last printout to this:

System.out.println("output=" + output + ", tempString=" + tempString + "<");

Can you help me figure out why my methods won't work

Scanner sc = new Scanner(System.in);
	String r = sc.nextLine();
	
	int [] b = new int[r.length()];
	String[] a = new String[r.length()];
	
	public String Operations{
	System.out.println("Enter the numbers");
	System.out.println("\nThe numbers are: " + r);
	System.out.println(r);

		int k = 0;
		int product = 1;
		for (int j = 0; j < r.length(); j++)
        {       
			a[j] = r.substring(j, j + 1);
			System.out.println(a[j]);
			k = k + Integer.parseInt(a[j]);
			
            b[j] = Integer.parseInt(a[j]);
            product = product * b[j];
		}                           
		System.out.println("\nThe sum of the digits are "  + k);
		System.out.println("\nThe product of the numbers are" + product);
	}
	
public String errors{
		String tempString = " ";
		for(int i = 0; i < r.length(); i++)
	    { 			
			if (r.charAt(i) != ' '){
	            // add the current character to tempString
	            tempString = tempString + r.charAt(i);         
	    }	    
	     // if the current character is a space
		if (r.charAt(i) == ' '){
			tempString = tempString + r.charAt(i);
		}
		  if(!Character.isDigit(r.charAt(i)))

                System.out.println("ERROR");
            break;
		}
}
}
Comments
Have you update any thing in it???Do not post multiple same things>

why my methods won't work

I have no idea what the methods are supposed to do.
I don't see where you call your methods.
The method: errors(), What is it supposed to do? What does it do?
The method: Operations(), what is it supposed to do?

What are the arrays: a and b for? What do you want them to contain?

(Sorry; I posted without seeing the second page. Gosh the code has gained some new problems on the 2nd page. ;-)

  • Try entering 'two three space enter'. See what happens.
  • You need to know exactly what tempString.substring(j, j) is returning. This is a trick that I use: System.out.println(tempString.substring(j, j) + "<");
  • There is a subtle but important difference between an empty string and a string that contains one space.

Edited 5 Years Ago by JeffGrigg: n/a

System.out.println("tempStr=" + tempString.substring(j, j) + "<");

An id also helps you know which print out came from what statement

This article has been dead for over six months. Start a new discussion instead.