Assistance with Flow Control Assigment

0 Alicia Scott · October 23, 2015
Hello! I'm completely new to java. I've been given a rather tricky assignment that deals with the for flow control loop. My professor isn't very helpful, and his former classmates that I have reached out to say the curriculum has changed and they have no idea what to do. Awesome, right? I was referred to this forum by a former student. She said great things about you guys. 

I'll copy and paste the directions and if you guys could translate in standard English what is going on, that would be great. If you're more into solving it for me then by all means go ahead -- but I'm also interested in actually comprehending what is being asked of me so that I can be successful in future assignments.

I have to gather numerical input from a user, then output the individual numbers as well as the sum of those numbers. (For example, 1234 as 1 2 3 4 and 10.) 

You need the JOptionPane Class's Input DialogBox to collect all the numbers as one String value e.g. "321"

Create a for loop with the length of the String set as the  maximum interations e.g. assuming "321" is stored in a variable named str, set up the for loop header as for(int i = 0; i<str.length(); i++)

Inside the for loop use the String class's chartAt method to extract the individual String characters while type casting them into integer data types e.g. (int)str.charAt(i) ; 

Note that (int)str.charAt(i) ; will extract the ASCII or Unicode value of the character. For instance if you type cast '3', you get the Unicode value of 51. To resolve this is, subtract the cast value from 48 to get the actual value e.g.  Assuming the current  value of i is '3', (int)str.charAt(i) - 48 give you the actual value 3
While you are extracting the actual values, you can add them to your accumulator variable during each iteration, and then display the result of the accumulator outside the loop.

Don’t forget to display the individual extracted characters separated by a single space before showing the sum. This can be done use the print method, and concatenating each extracted character with a single white space in double quotes in each iteration e.g. str.charAt(i) + " "

Post a Reply


Oldest  Newest  Rating
0 Gary Whitney · October 23, 2015
So what part(s) don't you understand?
Here is my brief translation:
The goal is to  get a string of digits from the user and display them in a string separated by spaces (I would include a comma but that requires an if statement in the for loop so I left it out) along with the total.

Initialize something like this:
   String strOutput = "The sum of the values ";
   String strUserInput = "";
   int intTotal=0;
   int intNum=0;

1. You are to use the GUI dialog-box instead of a Scanner, to get the numbers, as a String
2. The numbers are going to be jammed together in one String. This is the tricky part because users don't always input the way you expect them to. e.g. 3 2 1 or 3, 2, 1 instead of 321. Let's keep it simple and assume you  are the user and will put the numbers in without separators and you are only accepting the digits 0-9.

3.  The dialog-box is going to return a string and characters are defined in the ASCII table, so digits begin at 48 for 0..
The Strings have a charAt(int index) method that Returns the char value at the specified index.
However you want  integers so you are going to cat it  to an int.

4. You use a simple for loop because the str.length() method tells you how many times to run the loop.
Each time trough  the loop:
 * intNum = (int) strUserInput.charAt(i);
 * intTotal += intNum;
 * strOutout = strOutput + intNum + " ";
After the loop:
 * strOutout = strOutput + "= " + intTotal;

Now display the output however you are suppose to.
  • 1

Java / Android Development


Very popular language used to create desktop applications, website applets, and Android apps.

Bucky Roberts Administrator