Results 1 to 15 of 15
Thread: Negative Integers
 09302013, 07:59 PM #1Member
 Join Date
 Jan 2013
 Posts
 22
 Rep Power
 0
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; } } }
Inputting positive integers is fine, when I try to to input negative integers it doesn't show the answer.
Guide me please?
 09302013, 08:30 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 3,341
 Rep Power
 5
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 09302013, 10:10 PM #3Just a guy
 Join Date
 Jun 2013
 Location
 Netherlands
 Posts
 3,599
 Rep Power
 5
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
 10012013, 01:41 AM #4Member
 Join Date
 Jan 2013
 Posts
 22
 Rep Power
 0
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.; 10012013 at 01:43 AM.
 10012013, 02:53 AM #5Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 3,341
 Rep Power
 5
Re: Negative Integers
Well, if you want to. Just take the absolute value of the entered number  Math.abs().
Regards,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 10022013, 02:15 AM #6Member
 Join Date
 Jan 2013
 Posts
 22
 Rep Power
 0
 10022013, 03:56 AM #7
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.
 10032013, 12:16 AM #8Member
 Join Date
 Jan 2013
 Posts
 22
 Rep Power
 0
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.
 10032013, 01:26 AM #9Senior Member
 Join Date
 Jan 2013
 Location
 United States
 Posts
 3,341
 Rep Power
 5
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,
JimThe Java™ Tutorial  SSCCE  Java Naming Conventions
Poor planning our your part does not constitute an emergency on my part.
 10032013, 05:45 AM #10Member
 Join Date
 Jan 2013
 Posts
 22
 Rep Power
 0
 10032013, 05:51 AM #11
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?
 10042013, 01:12 AM #12Member
 Join Date
 Jan 2013
 Posts
 22
 Rep Power
 0
 10042013, 04:18 AM #13
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?
 10062013, 12:44 AM #14Member
 Join Date
 Jan 2013
 Posts
 22
 Rep Power
 0
 10072013, 06:10 AM #15Member
 Join Date
 Jan 2013
 Posts
 22
 Rep Power
 0
Similar Threads

Mod of Negative Numbers
By Venny in forum New To JavaReplies: 7Last Post: 01282011, 05:32 AM 
Help with negative numbers
By cela0811 in forum New To JavaReplies: 2Last Post: 01042011, 05:04 PM 
Byte value comes out negative
By Bluefox815 in forum New To JavaReplies: 1Last Post: 07032010, 03:52 AM 
I don't want negative money [BUG]
By anthonym2121 in forum New To JavaReplies: 1Last Post: 04072009, 08:06 AM
Bookmarks