Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By jim829

Thread: Negative Integers

  1. #1
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Negative Integers

    Java Code:
    package ZhyperMUFile;
    
    import java.util.*;
    
    public class javaprogram1{
    
    public static void main(String []args){
    	Scanner scn=new Scanner(System.in);
    
    int nums, store, nums1,counter = 0 ;
    
    System.out.print("Enter a number: ");
    nums=scn.nextInt();
    store = nums;
    	while (store > 0){
      store  = store/10;
      counter = counter + 1;
    	}
    	counter = counter - 1;
    	while (counter >= 0){
    		nums1 = nums% (int) Math.pow(10, counter);
    		nums = nums/(int) Math.pow(10,counter);
    		System.out.print (nums + "\t");
    		nums = nums1;
    		counter = counter - 1;
    		}
    	}
    }
    Output :



    Inputting positive integers is fine, when I try to to input negative integers it doesn't show the answer.

    Guide me please?

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,542
    Rep Power
    5

    Default Re: Negative Integers

    You just need to follow the logic of your program.

    1. Input a negative number --> nums.
    2. Assign nums to store.
    3. Since store is now negative the while loop exits immediately. So counter remains 0.
    4. Now counter becomes -1.
    5. Since -1 is less than 0 your second while loop exits immediately

    Regards,
    Jim
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,914
    Rep Power
    5

    Default Re: Negative Integers

    follow the logic, or add System.out.println statements to do it for you.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Negative Integers

    I got it guys, few question.

    Is it possible to remove the (-) sign?

    Example :

    Input : -123
    Output : 1 2 3

    with spacing.


    Thanks for guide.
    Last edited by Aries.; 10-01-2013 at 01:43 AM.

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,542
    Rep Power
    5

    Default Re: Negative Integers

    Well, if you want to. Just take the absolute value of the entered number - Math.abs().

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Negative Integers

    Quote Originally Posted by jim829 View Post
    Well, if you want to. Just take the absolute value of the entered number - Math.abs().

    Regards,
    Jim
    Done,

    Only the spacing that I can't get for negative integers, any idea?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Negative Integers

    Huh?

    The spacing is exactly the same for negative and positive numbers. It all depends upon what you put in the print statements and where you place them.

  8. #8
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Negative Integers

    One thing I forgot,

    When I input negative integers

    it display like this


    Input : -123
    Output : 12300

    or can someone modify my code above? or just put the code and I fill up.

    System.__.print ("__");

    just like that it seems this forum isn't a spoonfeeding.


    Thanks.

  9. #9
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,542
    Rep Power
    5

    Default Re: Negative Integers

    That doesn't make sense. The first thing you should do is take the absolute value. Then it should behave as though you entered a positive number.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Negative Integers

    Quote Originally Posted by jim829 View Post
    That doesn't make sense. The first thing you should do is take the absolute value. Then it should behave as though you entered a positive number.

    Regards,
    Jim
    Still don't get it . Sigh ----

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Negative Integers

    I assume your program currently works for positive numbers. Is that correct?
    If so you program should be something like:
    get user input
    process input
    produce output

    What you need to do is insert an extra step to check if the input is negative and convert to positive. Where do yo think that step should go? Before input? After output? Somewhere else?

  12. #12
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Negative Integers

    Quote Originally Posted by Junky View Post
    I assume your program currently works for positive numbers. Is that correct?
    If so you program should be something like:
    get user input
    process input
    produce output

    What you need to do is insert an extra step to check if the input is negative and convert to positive. Where do yo think that step should go? Before input? After output? Somewhere else?
    what I mean is, what line should I insert the code for negative integers to make it process until the displaying output. Yes the program is for positive only. I just saw this problem from .pdf file where I downloaded and I wanted to solve.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,789
    Rep Power
    7

    Default Re: Negative Integers

    You have already been given the answer in reply #5. Convert a negative number into a positive number before processing it. Are you going to try or continue whining hoping someone will just give you the code?

  14. #14
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

    Default Re: Negative Integers

    Quote Originally Posted by Junky View Post
    You have already been given the answer in reply #5. Convert a negative number into a positive number before processing it. Are you going to try or continue whining hoping someone will just give you the code?

    i'm not whining in the first place, i came here for learning. isn't a big deal?

  15. #15
    Aries. is offline Member
    Join Date
    Jan 2013
    Posts
    22
    Rep Power
    0

Similar Threads

  1. Mod of Negative Numbers
    By Venny in forum New To Java
    Replies: 7
    Last Post: 01-28-2011, 05:32 AM
  2. Help with negative numbers
    By cela0811 in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 05:04 PM
  3. Byte value comes out negative
    By Bluefox815 in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 03:52 AM
  4. I don't want negative money [BUG]
    By anthonym2121 in forum New To Java
    Replies: 1
    Last Post: 04-07-2009, 08:06 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
  •