Results 1 to 19 of 19
  1. #1
    Quinn is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default 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";
    }
    }

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  3. #3
    Quinn is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    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

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    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);
    }
    Because this is a complete giveaway, no explanation or anything ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Quinn is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    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.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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;
    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
    Java 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
    Java 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
    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
    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.

  10. #10
    Quinn is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you sunde887 that was a huge help :)

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    And of course my reply (#7) is completely ignored (as usual).

    kind regards,

    Jos aka the Invisible Man
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    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

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    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!
    cenosillicaphobia: the fear for an empty beer glass

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

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

    db

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by DarrylBurke View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

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

    kind regards,

    Jos aka the Invisible Man
    Who?

    :)

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sunde887 View Post
    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.

  18. #18
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Who?

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

    kind regards,

    XXX
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. need help to figure out basic java program
    By shane123 in forum New To Java
    Replies: 21
    Last Post: 12-02-2011, 04:12 AM
  2. Replies: 10
    Last Post: 04-20-2011, 02:03 AM
  3. Replies: 11
    Last Post: 03-22-2011, 06:33 AM
  4. trying to write a program for this
    By durdanto in forum New To Java
    Replies: 5
    Last Post: 02-15-2011, 01:27 PM
  5. [SOLVED] cant figure this program out..help
    By einstein1234 in forum New To Java
    Replies: 26
    Last Post: 04-23-2009, 04:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •