Results 1 to 19 of 19
- 05-17-2011, 02:56 PM #1
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
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, 02:59 PM #2
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?- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-17-2011, 03:14 PM #3
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
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, 03:30 PM #4
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.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-17-2011, 03:47 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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]
Java 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, 03:57 PM #6
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.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 05-17-2011, 04:26 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Duh, recursion is your friend here:
Java Code:private static int f(int n) { if (n < 10) return n; return n%10-f(n/10); }
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-18-2011, 10:53 AM #8
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
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, 11:04 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
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
Java Code:Type[] varName;
Java Code:int[] digits = new int[n];
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
Java Code:for(int j = 0; j != arr.length; ++j){ arr[j] = 0; }
With this information in mind, see if you can find a way to do something like this
--pseudo code
Java 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
If you need more information on arrays check out the java tutorials.
Googling "java tutorial arrays" should generate useful pages.
- 05-18-2011, 03:38 PM #10
Member
- Join Date
- May 2011
- Posts
- 5
- Rep Power
- 0
Thank you sunde887 that was a huge help :)
- 05-18-2011, 05:34 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
And of course my reply (#7) is completely ignored (as usual).
kind regards,
Jos aka the Invisible ManBuild a wall around Donald Trump; I'll pay for it.
- 05-18-2011, 06:34 PM #12
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, 07:35 PM #13
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-18-2011, 09:08 PM #14
Would be nice if someone would quote me once in a while, but it never happens...
db
- 05-18-2011, 09:14 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-18-2011, 09:25 PM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
-
- 05-18-2011, 09:57 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-18-2011, 10:04 PM #19
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Build a wall around Donald Trump; I'll pay for it.
Similar Threads
-
need help to figure out basic java program
By shane123 in forum New To JavaReplies: 21Last Post: 12-02-2011, 05:12 AM -
Can't figure out how to get the info from class definition to driver program
By gnng in forum New To JavaReplies: 10Last Post: 04-20-2011, 03:03 AM -
New to Java and connot figure out simple java program- please help
By tm02943 in forum New To JavaReplies: 11Last Post: 03-22-2011, 07:33 AM -
trying to write a program for this
By durdanto in forum New To JavaReplies: 5Last Post: 02-15-2011, 02:27 PM -
[SOLVED] cant figure this program out..help
By einstein1234 in forum New To JavaReplies: 26Last Post: 04-23-2009, 05:30 AM
Bookmarks