Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 34

Thread: ASSIGNMENT HELP

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default ASSIGNMENT HELP

    i tried at least 50 times for this code to work and i just can't get it to work. can someone tell me how to do it?


    Assignment - Repetition

    In the nine-digit Social Insurance Number given to each person having a job or filing an income tax return in Canada, the ninth digit is a check digit that is used to test the validity of the other digits in the SIN. The ninth digit is determined by the following procedure.

    a) Double the 2nd, 4th, 6th, and 8th digits.
    If the result is two digits long, add the digits together
    b) Add the digits of the numbers found in step (a).
    c) Add the 1st, 3rd, 5th, and 7th digits
    d) Add the numbers found in steps (b) and (c).
    e) Subtract the unit (ones digit) digit of the result of step (d) from 10 and note the units digit of the result. For the SIN to be valid, its ninth digit must have this value.

    Write a program that repeatedly reads nine-digit numbers and determines whether or not each number is a valid SIN, The program should stop when it reads the value 999999999.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    i tried at least 50 times
    The instructions look reasonable.
    Take them one at a time. What problem do you have doing the first step?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    Quote Originally Posted by Norm View Post
    Take them one at a time. What problem do you have doing the first step?
    i don't know how to get each digit on it's own... i tried %10 to get the last digit but i don't understand how i would throw this all into a for loop!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    Are you allowed to convert the number to a String? That would make getting the digits by their position easier.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    Quote Originally Posted by Norm View Post
    Are you allowed to convert the number to a String? That would make getting the digits by their position easier.
    yeah you can do that! my teacher said you're allowed to do anything as long as it's in a loop but i dont know how to do it.. like i know the syntax but don't know how to apply it here.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    What part of step one is giving you the problem?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    i don't know how to convert it to from a int to a string then back ...

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    How does the program get the number's value initially? Is is read into a String?
    If it starts as a String, then you don't need to convert it.
    If it starts as an int, the String class has a method to convert it to a String.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    what do you suggest would be the best way to do it

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    Read it as a String.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    ThePrince (April 18th, 2013)

  12. #11
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    Quote Originally Posted by Norm View Post
    Read it as a String.
    i'm a java noob... could you get my program started? like just until the loop cause i tried to put it as a string but i don't know how i would get the 2nd, 4th, 6th, 8th digits out of it! im STRUGGLING! thanks!

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    Start by putting in some import statements as needed.
    Next define the class
    next define the main() method.

    Put off trading in the Strings. Define a String with a sample value and work on the steps listed in post#1
    When that works, then look at using a loop to read in multiple Strings to be tested as in the above.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    char c1 = SIN.charAt(1);
    char c2 = SIN.charAt(3);
    char c3 = SIN.charAt(5);
    char c4 = SIN.charAt(7);
    int num1 = c1*2;
    if (num1>9){
    Integer.toString(num1);
    char num1_1 = ((num1.charAt(1))) +(num1.charAt(2));

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Cannot invoke charAt(int) on the primitive type int
    Cannot invoke charAt(int) on the primitive type int

    i don't know what i did wrong, any suggestions? maybe where i should move the conversion statement

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    What is the definition of the variable: SIN? You have not posted enough of the code to see SIN's definition.

    Some comments:
    1)char values are not the same as int; '3' is not the same as 3. To get the 3 from '3' you need to subtract '0'
    '3' - '0' = 3
    2)The toString() method returns a String. The posted code does not assign the value returned by the toString() method to a variable.

    When posting code Please wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Use the % and / operators to get the two digits from an int value between 10 and 99
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    	// Prompt for SIN Card number as string, convert to integer to get check digit, output the check digit, convert back to string for proccess
     
    		System.out.println("Please enter your SIN card number: ");
    		String SIN = in.nextLine();
    		int SIN1 = Integer.parseInt(SIN);
    		int checkdigit = SIN1%10;
    		System.out.println("The checkdigit is: " + checkdigit);
    		Integer.toString(SIN1);
     
    		//Start the process
     
    		char c1 = SIN.charAt(1);
    		char c2 = SIN.charAt(3);
    		char c3 = SIN.charAt(5);
    		char c4 = SIN.charAt(7);
    		int num1 = c1*2;
    		if (num1>9){
    			Integer.toString(num1);
    			char num1_1 = ((num1.charAt(1))) +(num1.charAt(2));
     
    		}


    i'm completely lost... this is my first assignment using java, i have experience with python but this is on another level! help please! what should i do !!

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    char values are not the same as int; '3' is not the same as 3. To get the int value 3 from '3' you need to subtract '0'. For example: '3' - '0' = 3

    To get the two digits from a number greater then 9 and less the 100 use the % and / operators:
    13 % 10 = 3
    13 / 10 = 1

    num1.charAt(1)
    You can not call methods for primitives. Methods go with objects
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: ASSIGNMENT HELP

    working on your program here. Taking some time but I am developing a solution for it.

  19. The Following User Says Thank You to myjava For This Useful Post:

    ThePrince (April 20th, 2013)

  20. #18
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    okay thanks so much! but why is it that when i put down 888888888 as the SIN number, and do this
                    char c1 = SIN.charAt(1);
    		char c2 = SIN.charAt(3);
    		char c3 = SIN.charAt(5);
    		char c4 = SIN.charAt(7);
    		int num1 = c1*2;
     
    		if (num1>9){
    		int num1_1 = (num1%10) + (num1/10);
    		System.out.println(num1%10);
    		System.out.println(num1/10);
    		System.out.println(num1_1);
    		}

    i get the results:
    2
    11
    13

    when i should be getting:
    1
    6
    7

    NOTE: i just put the system.out statements to see what each answer was, for the final code i would not display this.

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    What was the value of num1? You should print that also.

    The value of the char '8' is not the int 8? Copy and execute this:
     System.out.println("'8'="+ (int)'8');  // '8'=56
    Also when printing out the values of variables, the output will be clearer if the values are labeled
    System.out.println("num1_1="+num1_1);
    If you don't understand my answer, don't ignore it, ask a question.

  22. #20
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    Quote Originally Posted by Norm View Post
    What was the value of num1? You should also print that also.
    woah, i checked the value of num1 and i got 112!!
    any thoughts on why?

    and yeah, ill do that for sure next time!

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    i checked the value of num1 and i got 112!!
    any thoughts on why?
    See the code in post#19 for an explanation. Look at an ASCII character table that gives the int values of all the characters.

    See posts#14 and #16 for a technique to convert a char to an int
    If you don't understand my answer, don't ignore it, ask a question.

  24. #22
    Junior Member
    Join Date
    Apr 2013
    Posts
    16
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: ASSIGNMENT HELP

    Quote Originally Posted by Norm View Post
    What was the value of num1? You should print that also.

    The value of the char '8' is not the int 8? Copy and execute this:
     System.out.println("'8'="+ (int)'8');  // '8'=56
    i copy and pasted it into the compiler and got '8' =56
    so i would have to subtract '8' by 0 to get 8?

  25. #23
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: ASSIGNMENT HELP

    1. Take the user input as string

    2. Select the digit at each place and save it into an array on integers. It can be done by
    int numbers = new int[9];
    for(int i = 0; i < 9; i++)
    {
    char a = stringAt(i);
    numbers[i] = parse the char to integer.
    }

    3. int total = 0;
    for loop(int i = 2; i < 9; i = i +2)
    { int temp = 0;
    temp = numbers[i] * 2;
    if(temp > 9)
    total += temp/10;
    total += temp%10;
    else
    total += temp;
    }

    4. for loop(int i = 1; i < 9; i = i+2)
    {total += numbers[i];}

    5. int test = total%10 - total/10;

    6. if(test == numbers[8])
    SSN is correct.
    else
    SSN is wrong

    Hope this helps

  26. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,231
    Thanks
    52
    Thanked 2,296 Times in 2,267 Posts

    Default Re: ASSIGNMENT HELP

    so i would have to subtract '8' by 0 to get 8?
    Did you try it to see what the results are? Write two lines of code to do the subtract and print out the results.
    If you don't understand my answer, don't ignore it, ask a question.

  27. #25
    Junior Member
    Join Date
    Apr 2013
    Posts
    17
    My Mood
    Happy
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: ASSIGNMENT HELP

    Try my solution from scratch. I have tried it with code. It won't take long. I can't pose code here because of the regulations.

Page 1 of 2 12 LastLast

Similar Threads

  1. Need help with assignment
    By djhunt90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2012, 02:36 PM
  2. Need help with my assignment
    By kohii in forum What's Wrong With My Code?
    Replies: 14
    Last Post: February 4th, 2012, 02:40 AM
  3. Help with Assignment
    By Nessera in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2011, 05:28 PM
  4. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  5. Replies: 1
    Last Post: February 22nd, 2010, 07:20 AM