# Can't figure out how to write this program.

• 05-17-2011, 01:56 PM
Quinn
Can't figure out how to write this program.
I am trying to write a program that can calculate whether a six digit number is a zero weight number or not. It is zero weight if:
(sum of oddly positioned digits)-(sum of evenly positioned digits)=0

Here is what I have so far but I think I am pretty far off track because I get a load of errors when I try to compile it.

import javax.swing.JOptionPane;

public class A1Q2
{
public static void main(String[] args)
{
String inputString; // For reading input
Int inputInteger; // Integer for testing
Int weight; // Weight of inputInteger
Int sumEven; // Sum of even positioned digits
Int sumOdd; // Sum of odd positioned digits
Int first; // Digit in first position
Int second; // Digit in second position
Int third; // Digit in third position
Int fourth; // Digit in fourth position
Int fifth; // Digit in fifth position
Int sixth; // Digit in sixth position
Int oneDigit; // Integer reduced to one digit
Int twoDigit; // Integer reduced to two digits
Int threeDigit; // Integer reduced to three digits
Int fourDigit; // Integer reduced to four digits
Int fiveDigit; // Integer reduced to five digits
Int sixDigit; // Integer with six digits

// Get integer to be tested
inputString =
JOptionPane.showInputDialog("Enter a six digit number to determine \n"+
" if it is a zero weight integer.");

// Convert the input to an int
inputInteger = Integer.parseInt(inputInt);

// Break down the integer to each digit

(Int)inputInteger%10 = sixth;

(Int)inputInteger/10 = fiveDigit;

(Int)fiveDigit%10 = fifth;

(Int)fiveDigit/10 = fourDigit;

(Int)fourDigit%10 = fourth;

(Int)fourdigit/10 = threeDigit;

(Int)threedigit%10 = third;

(Int)threedigit/10 = twoDigit;

(Int)twoDigit%10 = second;

(Int)twoDigit/10 = oneDigit;

(Int)oneDigit%10 = first;

// Determine the sum of the oddly positioned numbers
sumOdd = first + third + fifth;

// Determine the sum of the evenly positioned numbers
sumEven = second + fourth + sixth

// Calculate if the integer is a zero weight integer
weight =
sumEven - sumOdd;

// Display the results
JOptionPane.showMessageDialog(null,"Zero Weight Numbers \n" +
"Input: " + inputInteger + "\n" +
"Weight: " + weight + "\n" +
"End of Processing";
}
}
• 05-17-2011, 01:59 PM
Dark
Ok, what are the errors? We can't read your mind and I wouldn't expect anyone to copy paste my code into their IDE to try and get the same errors you're getting.

What isn't working?
• 05-17-2011, 02:14 PM
Quinn
Cannot find symbol
symbol: class Int

and

Cannot find symbol
symbol: variable (several different variables)

I am getting those errors on pretty much every line
• 05-17-2011, 02:30 PM
Dark
I'm not really sure what an Int is, you didn't define it. I believe you're looking for int. Java is case sensitive so make sure you use the proper capitalization of terms.

Try that out, and report back.
• 05-17-2011, 02:47 PM
sunde887
That approach is very clunky and poorly organized. It has too many limitations. You should consider an array list for even ints and an array list for odd ints. Then you can separate the items into the correct array. Finally you could sum the array lists.

If you post code, please use code tags, to do this type [code] then paste in your code and close with [/code]

Code:

```declare arraylist for even indexes declare arraylist for odd indexes loop   if on even index, extract and store in even list   else extract and add in odd lost end loop sum lists compare results```
• 05-17-2011, 02:57 PM
Dark
Sunde, this looks like a school project to me so he might be limited in what he can use. I don't know if he is, but it should always be noted when offering a change like that to make sure its ok with the teacher if it is a school project.

Some teachers aren't ok with you getting ahead of the book.

And with that note, however if it is not a school project and you are not learning from a book by yourself, I suggest going with Sunde's method. Its much more efficient and much less coding.

However make sure you can use that method before changing any of your code, it would be a shame to get a failing grade on an assignment because you got ahead of the book.
• 05-17-2011, 03:26 PM
JosAH
Duh, recursion is your friend here:

Code:

```private static int f(int n) {         if (n < 10) return n;         return n%10-f(n/10); }```
Because this is a complete giveaway, no explanation or anything ...

kind regards,

Jos
• 05-18-2011, 09:53 AM
Quinn
Hello,

The only restriction I have on my homework is that we are not allowed to use String methods. I would like to try using Arrays because they seem to make a whole lot of things easier but we haven't covered that section yet. What I am having problems with is getting the input into the array. I need to use the JOptionPane to receive the integer for testing and am not sure how to make that happen.

Thanks if you have any advice. Keep in mind that this is the first assignment I have ever done in Java.
• 05-18-2011, 10:04 AM
sunde887
An array will work, however; be aware that the size of an array is constant(it can't be changed). They are surprisingly simple to use as well, you declare them like this
Code:

`Type[] varName;`
obviously you replace Type with the correct type(in your case int). You can declare them and initialize them with a value or as empty elements. Here is declaring as some size if empty elements
Code:

`int[] digits = new int[n];`
Replace n with the size you want the array to be. You will have to determine the size of the input before creating the array.

You can set a value in the array with the subscript operator([]) with an index inside it. The following snippet will set each value in some array to 0
Code:

```for(int j = 0; j != arr.length; ++j){   arr[j] = 0; }```
in the above snippet, arr is a generic array(use your own arrays name instead) and length is a built in member of the array which returns the amount of elements in can hold.

With this information in mind, see if you can find a way to do something like this
--pseudo code
Code:

```get input declare and initialize unfilles array of size n, where n is the size of input loop   split number, add to array end loop```
Once you can get this array filled it should be fairly simple to finish.

If you need more information on arrays check out the java tutorials.

Googling "java tutorial arrays" should generate useful pages.
• 05-18-2011, 02:38 PM
Quinn
Thank you sunde887 that was a huge help :)
• 05-18-2011, 04:34 PM
JosAH
And of course my reply (#7) is completely ignored (as usual).

kind regards,

Jos aka the Invisible Man
• 05-18-2011, 05:34 PM
DarrylBurke
Hey sunde I just thought I'd chip in and be the fourth member to post in this thread after Quinn, Dark and yourself.

db
• 05-18-2011, 06:35 PM
JosAH
Quote:

Originally Posted by DarrylBurke
Hey sunde I just thought I'd chip in and be the fourth member to post in this thread after Quinn, Dark and yourself.

db

See? See? SEE? That's what I'm all about! I'm completely ignored except by the Dutch IRS! It's not fair! Yoohoo! look ---> here <---

attention!

Jos <--- here! here!
• 05-18-2011, 08:08 PM
DarrylBurke
Would be nice if someone would quote me once in a while, but it never happens...

db
• 05-18-2011, 08:14 PM
JosAH
Quote:

Originally Posted by DarrylBurke
Would be nice if someone would quote me once in a while, but it never happens...

For years I thought I was the inventor of the Off By One Error (OBOE) but it seems to be you ;-)

kind regards,

Jos aka The Forgotten One
• 05-18-2011, 08:25 PM
sunde887
Quote:

Originally Posted by JosAH
And of course my reply (#7) is completely ignored (as usual).

kind regards,

Jos aka the Invisible Man

Who?

:)
• 05-18-2011, 08:42 PM
Fubarable
Quote:

Originally Posted by sunde887
Who?

It's nothing but forum noise; please take no notice. Us grand and all-powerful moderators are working on a way to rid us of it once and for all.
• 05-18-2011, 08:57 PM
JosAH
Quote:

Originally Posted by sunde887
Who?

:)

How would I know? I've never seen the idiot.

kind regards,

XXX
• 05-18-2011, 09:04 PM
JosAH
Quote:

Originally Posted by Fubarable
It's nothing but forum noise; please take no notice. Us grand and all-powerful moderators are working on a way to rid us of it once and for all.

Yes, we agree! Pull out his intestins! Poke his eyeballs out! Defenstrate him! Burn him. Make him eat his liver!

kindest regards,

Mrs. and Mr. Tarquin fimtimblimblimbim *boing* ole bus stop biscuit barrel. (slightly silly on the left side) <-- 1298 points