# negative Number « Number « Java Data Type Q&A

### 1. Rounding negative numbers in Java    stackoverflow.com

According to Wikipedia when rounding a negative number, you round the absolute number. So by that reasoning, -3.5 would be rounded to -4. But when I use java.lang.Math.round(-3.5) returns ...

### 2. Adding negative and positive numbers in java without BigInt    stackoverflow.com

i'm trying to write a small java class. I have an object called BigNumber. I wrote method that add two positive numbers, and other method that subract two also positive numbers. ...

### 3. How to generate random positive and negative numbers in java    stackoverflow.com

I am trying to generate random integers over the range (-32768, 32767) of the primitive data type short. The java Random object only generates positive numbers. How would I go about ...

### 4. How does java do modulus calculations with negative numbers?    stackoverflow.com

Am I doing modulus wrong? because I keep getting `-13 % 64 = 51` not `-13 % 64 = -13`

### 5. Best way to make Java's modulus behave like it should with negative numbers?    stackoverflow.com

In java when you do

``````a % b
``````
If a is negative, it will return a negative result, instead of wrapping around to b like it should. What's the best way to fix ...

### 6. Prevent Negative numbers for Age without using client side validation    stackoverflow.com

I have an issue in Core java. Consider the Employee class having an attribute called age.

``````class Employee{
...``````

### 7. Mod in Java produces negative numbers    stackoverflow.com

It seems like when I calculate `int i = -1 % 2` I get -1 in Java. In Python I get 1. What do I have to do to get the ...

### 8. Java Random giving negative numbers    stackoverflow.com

Having a night mare with Java`s Random, if i do this:

``````Random rng = new Random(seed) // seed == 29 in this example

String ss = "";
...``````

### 9. Strange behaviour in Scanner with negative numbers in java    stackoverflow.com

i try to find the answer but could not, maybe is a litlle strange what im going to ask, and i would like to get some idea of what is going ...

### 10. Java - Program accepting negative numbers when it shouldn't    stackoverflow.com

I have to make a program that uses nested loops to collect data and calculate the average rainfall over a period of years. First, the program should ask for the number ...

### 11. Cube root of a negative number    stackoverflow.com

I'm trying to find the cube root of a negative number but I get a NaN. Any help?

``````System.out.println(Math.pow(-8, 1.0 / 3.0));
``````

### 12. infix to postfix with negative numbers    coderanch.com

Hi guys! I already have an algorithm on how to convert infix to postfix expressions but it is only limited positive numbers. My problem is: 1. How about if there are negative numbers in the infix expression. 2. Is there another algorithm for converting infix to postfix with negative numbers? 3. How then do I evaluate the postfix expression? for example: ...

### 14. shift operator on negative number    coderanch.com

Originally posted by Cindy Yang: can anybody explain in detail how to do following code? I have trouble when using shift operator on negative. int i = -8, j = -8; i >>= -4; j >>= -34; i=? j=? Thanks. Cindy Both i and j end up as -1. i >>= -4 is equivalent to i >>= 28. j >>= -34 ...

### 16. Math.floor with negative numbers    coderanch.com

hi there.... System.out.println(Math.floor(-2.1)); what do you think the answer is? -3.0 that's what. i thought it rounded towards zero...i guess it does with positive numbers, but the point is that .floor gets the lowest whole number... so therefore it must be -3.0 ... silly little things like that must be watched for on the exam eh.

### 17. Shift operators working on negative numbers    coderanch.com

class Shifter { public static void main(String[] args) { int i = -1; int iLeft = i << 4; int iRight = i >> 4; int iRightUnsigned = i >>> 4; System.out.println("\nOriginal: " + i + "...\n" + Integer.toBinaryString(i)); System.out.println("\nLeft by 4: " + iLeft + "...\n" + Integer.toBinaryString(iLeft)); System.out.println("\nRight by 4: " + iRight + "...\n" + Integer.toBinaryString(iRight)); System.out.println("\nRight unsigned ...

### 18. random numbers, returns a negative number?    coderanch.com

hey guys im new to coderanch. im having some problem wit this code. its generating a random number, but its negative and i cant figure out why. im writing a text-based-rpg-combat-system. package grogvskroh; import java.util.*; import java.math.*; import java.io.*; import java.awt.*; public class grog extends StartGame { public void grogAttack() { Random grogRand = new Random(); // makes random numberfor grogs ...

### 19. Checking for negative numbers in method parameters    coderanch.com

I'm also a java beginner, and on the same boat as you Darrin, just stuck on the same exercise and after an hour trying write some code myself that didn't work, found this forum and read some other sources also. So this is what I came up with, don't know if it is the right way, but it's working. import acm.program.*; ...

### 20. Method Doesnt turn negative number 0 and doesnt return 3    java-forums.org

Write a method that accepts an integer array of unspecified length as a parameter. It should remove all negative values from the array and return the size of the altered array. Unused values at the end of the array should be filled with zeroes. For example, the array {3, 5, -2, 4, -3} should become {3, 5, 4, 0, 0} and ...

### 21. Help with negative numbers    java-forums.org

I'm writing a calculator in Java and I am having trouble with negative numbers. The numbers are stored in and array of strings, called input, and Output is a text field where I display my results. Temp is a double. When I run the program and enter a negative number, it acts as if it is positive. I convert a string ...

### 22. Mod of Negative Numbers    java-forums.org

Hi All, This may not b a new question to most of you but since am a newbie, please consider answering this. For languages like c/c++ and java, how does mod % operator work with negative numbers. for e.g and trying to fetch the output of -3%2 . The actual output is -1 whereas it should be just 1. Could you ...

### 23. forming negative numbers    java-forums.org

Hi, What would be the best way to form negative numbers from a string "-" and a number? I can convert the number to a string, so I'd have say "-5", but that's a string and not really the number -5. Is there a way to tell Java that it's a number with a negative value? Thanks!

### 26. Negative numbers don't work    forums.oracle.com

Well, what is the problem, do you think? BTW it's rude to post code here that you know is irrelevant. It means that you're wasting others' time. Ideally you should create a small, compilable (unless the problem is that you can't get it to compile) example that demonstrates the trouble you're having and does nothing else.

### 27. handling negative numbers.....    forums.oracle.com

i see what your saying with a method use, i was thinking that myself, basicly my program take in 3 postive integer values and sends them to a triangle class the triangle class decides based on the input what type of triangle is Equalaterial, Scalene, or Isoclese (pardon the spelling) a method would be great but it would only excute once ...

### 31. Validation for negative numbers    forums.oracle.com

Hi, I have "quantity" variable as an Integer datatype , which should not allow "0" (zero) or "any negative values" (-1). Is there any simple predefined function in java to check this and throw a validation message if quantity is "0" or "-ve"? I should not use Javascript. Similarly if i add a value "" for "name" field it is added ...

### 32. Catch Negative Number    forums.oracle.com

There is no exception for negative numbers, because negative numbers are not exceptional when parsing for numbers. Don't try to handle with with an exception, or in the catch block. Try to parse the string to get a number. If it fails with a parse exception, tell the user. If it doesn't fail, then after parsing, check to see if the ...

### 35. Checking negative even number    forums.oracle.com

Write a program that is supposed to read 500 integers from user. Use a break statement to stop the reading if any negative even number is entered. Display that number Can anyone write the pseudocode for this question? The thing that bothers me is that a negative even number, How do I write the code to stop this one? Can anyone ...

### 38. disallow negative numbers..    forums.oracle.com

is there an exception for negative numbers? i'm trying to create a program which takes the user's input like this: input a positive integer: 5 System prints out "good job" after then repeats the question until the user types 0. I got that part working but if i input something like this: input a positive integer: -2 System prints out "Positive ...

### 40. Adding two possitive numbers results a negative????    forums.oracle.com

is an array of four integers that picture an ascii characters). Normaly that would result a buffer full of positive numbers.In my case thats not always true. Many times i get as a result a negative number even if the other two numbers that i add are positive. What's wrong here?? Thanks in advance!