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

# Thread: number turning negative during calculation why???

1. ## number turning negative during calculation why???

so here is my calculation and when total equals 397,683 the numbers start turning negative....why at that number and why at all? it works fine up to 397,682

```long days, hours, minutes, seconds;

days = (long) (total / 86400);
System.out.println("" + days);
total = total - (days * 86400);
System.out.println("" + total);
hours = (long) (total / 3600);
System.out.println("" + hours);
total =  total - (hours * 3600);
System.out.println("" + total);
minutes =  (long) (total / 60);
System.out.println("" + minutes);
total = total - (minutes * 60);
System.out.println("" + total);
seconds = (long) total;
System.out.println("" + seconds);

totalTime.setText("" + days + "d:" + hours + "h:" + minutes + "m:" + seconds + "s");```

-24855
-7096.0
-1
-3496.0
-58
-16.0
-16

thats what the println display when i do 397683

24855
10800.0
3
0.0
0
0.0
0

when i do 397682

2. ## Re: number turning negative during calculation why???

Well, you've missed the absolute most important part - how is total defined? You didn't take the time to give us a SSCCE but I will:

```public class DayThing {
public static void main( String argv[] ) {
long total = 397683;
long days, hours, minutes, seconds;

days = (long) (total / 86400);
System.out.println("" + days + " days" );
total = total - (days * 86400);
System.out.println("" + total + " total");
hours = (long) (total / 3600);
System.out.println("" + hours + " hours");
total =  total - (hours * 3600);
System.out.println("" + total + " total" );
minutes =  (long) (total / 60);
System.out.println("" + minutes + " minutes");
total = total - (minutes * 60);
System.out.println("" + total + " total" );
seconds = (long) total;
System.out.println("" + seconds + " seconds" );
}
}```

does not show the behavior you describe. Can you elaborate?

3. ## Re: number turning negative during calculation why???

sorry about that this is the whole code for the program here

```package testing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Test extends JFrame implements ActionListener{

private static final long serialVersionUID = 1L;

JLabel troopLabel = new JLabel("Troops: ");
JTextField troopField = new JTextField(7);

JLabel accelLabel = new JLabel("Accels: ");
JTextField acceleratorField = new JTextField(7);

JButton button = new JButton("Calculate");

JLabel uraniumCost = new JLabel("Uranium Cost: ");
JLabel totalCost = new JLabel();

JLabel spaceLabel = new JLabel("Space: ");
JLabel space = new JLabel();

JLabel expLabel = new JLabel("Exp Gain: ");
JLabel expGain = new JLabel();

JLabel totalTimeLabel = new JLabel("Time: ");
JLabel totalTime = new JLabel();

public Test(String title) {
setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

createGUI();
}

public void createGUI() {

setLayout(new GridLayout(7,1,10,10));

JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

JPanel panel5 = new JPanel();
panel5.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

JPanel panel6 = new JPanel();
panel6.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

JPanel panel7 = new JPanel();
panel7.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

}

public static void main(String[] args) {

Test gui = new Test("Chimera");
gui.setSize(430, 400);
gui.setVisible(true);
}

public void actionPerformed(ActionEvent event) {

totalCost.setText("");
space.setText("");
totalTime.setText("");
expGain.setText("");

String troopNumber = troopField.getText();
String accelNumber = acceleratorField.getText();
try {

long troopNumb = Long.parseLong(troopNumber);
long exptotal =  troopNumb * 717187;

int troopNum = Integer.parseInt(troopNumber);
int accelNum = Integer.parseInt(accelNumber);

String pressed = event.getActionCommand();
if(pressed.equals("Calculate")) {
long uraniumtotal = troopNum * 300;
long spacetotal = troopNum * 2;

space.setText("" + spacetotal);
totalCost.setText("" + uraniumtotal);
expGain.setText("" + exptotal);

double total = troopNum * 5400;

while (accelNum > 0) {
total = total *=0.7;
accelNum--;
}

long days, hours, minutes, seconds;

days = (long) (total / 86400);
System.out.println("" + days);
total = total - (days * 86400);
System.out.println("" + total);
hours = (long) (total / 3600);
System.out.println("" + hours);
total =  total - (hours * 3600);
System.out.println("" + total);
minutes =  (long) (total / 60);
System.out.println("" + minutes);
total = total - (minutes * 60);
System.out.println("" + total);
seconds = (long) total;
System.out.println("" + seconds);

totalTime.setText("" + days + "d:" + hours + "h:" + minutes + "m:" + seconds + "s");

}

}catch(NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Numbers only!");
}

}

}```

everything works great except this part where im trying to change the seconds into days, hours, minutes, seconds..if you run it and put 397682 in the troop field and 0 in the accel field and hit calculate the time at the bottom calculates but if you use 397683 the time comes out to be negative numbers or maybe its another part of my code thats messing with it?

also thank you for taking the time to reply its much appreciated

4. ## Re: number turning negative during calculation why???

Add some println's to debug and see what might be negative - this should lead you back to the culprit. You will see one of your variables becomes negative as it gets larger, and the following is a demonstration of (and big hint to) the problem:

```	public static void main(String[] args) {
System.out.println(Integer.MAX_VALUE);
System.out.println(397682 * 5400);
System.out.println(397683 * 5400);
}```

You need to be explicit in stating that you do not want integer math, but math with respect to your data type (in this case, a double). You can do so by casting one of your values prior to doing the actual math.

5. ## The Following User Says Thank You to copeg For This Useful Post:

derekxec (July 4th, 2012)

6. ## Re: number turning negative during calculation why???

ahhhh found it i had troopnum as an int and it couldnt hold that big of a number cant believe i missed this thanks a lot