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 2 of 2 FirstFirst 12
Results 26 to 34 of 34

Thread: ASSIGNMENT HELP

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

    Default Re: ASSIGNMENT HELP

    @myjava okay i'll try your code out! thanks man

    --- Update ---

    Quote Originally Posted by Norm View Post
    Did you try it to see what the results are? Write two lines of code to do the subtract and print out the results.
    //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-0;
    		num1 = c1*2;
    		System.out.println(num1);

    i've tried subtracting it by 0 and '0'
    i get 112 every time

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ASSIGNMENT HELP

    i've tried subtracting it by 0 and '0'
    There is a big difference between the int 0 and the char '0'.
    When you learned arithmetic, what was said about how a value change when you subtracted 0 from it?

    Post the code that subtracts '0' from the char and prints out the results to show what problem you are talking about.

         int num1 = c1-0;   //  sets the  value of num1
         num1 = c1*2;       //  immediately changes the value of num1
    The second line above completely replaces the value of num1 set by the first statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #28
    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
    When you learned arithmetic, what was said about how a value change when you subtracted 0 from it?
    the teacher was absent that day and we were expected to learn the lesson ourselves!
    so what do you think i should do? subtract it by 0 after the if (num1>9) or before?

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ASSIGNMENT HELP

    As I said several times before, one way to convert a digit char to an int value is to subtract '0' from it. For example: '3' - '0' = 3
    The steps would be:
    get a char
    convert the char to an int value by subtracting '0' from it.
    Then double the int value
    then compare the doubled int to 9

    subtract it by 0
    0 is not the same value as '0'. The 's are important
    If you don't understand my answer, don't ignore it, ask a question.

  5. #30
    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
    As I said several times before, one way to convert a digit char to an int value is to subtract '0' from it. For example: '3' - '0' = 3
    okay it works now, but how would i throw this all into a loop. i don't get how i would be able to set up a for loop and have it check if each number doubled is greater than 9 and then individually add them together.. help please?

    char c1 = SIN.charAt(1);
    		char c2 = SIN.charAt(3);
    		char c3 = SIN.charAt(5);
    		char c4 = SIN.charAt(7);
    		int num1 = c1-'0'; //This will get a integer value for a character
    		num1 = num1*2;
    		int num2 = c2-'0';
    		num2 = num2*2;
    		int num3 = c3-'0';
    		num3 = num3*2;
    		int num4 = c4-'0';
    		num4 = num4*2;
     
    		if (num1>9){
    		System.out.println(num1);
    		int num1_1 = (num1%10) + (num1/10);
    		}

  6. #31
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ASSIGNMENT HELP

    Does the code now completely check a SIN to be correct?
    Once the code is able to check a SIN for correctness, then a loop can be made to get another SIN and check it.
    Something like this:
    begin loop
    get SIN
    check that SIN is correct
    end loop

    The loop is not used for checking the correctness of a SIN.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #32
    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
    Does the code now completely check a SIN to be correct?
    yeup! i finally got it to work, but I'm having one last problem..
    the program is supposed to keep running until 999999999 is entered

    so if i create a while loop for example:

    while (SIN!=999999999) {
    <entire code here>
    }


    it would keep running infinitely, if i choose to prompt again at the end of the code i would have to change the while loop variable and thus making it not work.

    i've tried your format of
    loop
    get sin
    check sin
    end loop
    but i cant seem to get the variables set

  8. #33
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ASSIGNMENT HELP

    My example was very simple and didn't show any tests to end the loop. a test would need to be added after the SIN was read to check for the request to end the loop and the loop exited there. A break statement is one way to exit a loop.
    If you don't understand my answer, don't ignore it, ask a question.

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

    ThePrince (April 21st, 2013)

  10. #34
    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
    My example was very simple and didn't show any tests to end the loop. a test would need to be added after the SIN was read to check for the request to end the loop and the loop exited there. A break statement is one way to exit a loop.
    yeup, i finally got the damn thing to work. thanks for your help norm. much appreciated man.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Need help with assignment
    By djhunt90 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2012, 03: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, 03: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, 08:20 AM