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.

Results 1 to 5 of 5

Thread: number turning negative during calculation why???

  1. #1
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default 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. #2
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default 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?
    Need Java help? Check out the HotJoe Java Help forums!

  3. #3
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default 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));
    		panel1.add(troopLabel);
    		panel1.add(troopField);
     
    		add(panel1);
     
    		JPanel panel2 = new JPanel();
    		panel2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    		panel2.add(accelLabel);
    		panel2.add(acceleratorField);
    		add(panel2);
     
    		JPanel panel3 = new JPanel();
    		panel3.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    		panel3.add(button);
     
    		add(panel3);
     
     
    		JPanel panel4 = new JPanel();
    		panel4.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    		panel4.add(uraniumCost);
    		panel4.add(totalCost);
    		add(panel4);
     
    		JPanel panel5 = new JPanel();
    		panel5.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    		panel5.add(spaceLabel);
    		panel5.add(space);
    		add(panel5);
     
    		JPanel panel6 = new JPanel();
    		panel6.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    		panel6.add(expLabel);
    		panel6.add(expGain);
    		add(panel6);
     
    		JPanel panel7 = new JPanel();
    		panel7.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    		panel7.add(totalTimeLabel);
    		panel7.add(totalTime);
    		add(panel7);
     
    		button.addActionListener(this);
     
     
    	}
     
     
    	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. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,335
    Thanks
    181
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default 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. #5
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default 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

Similar Threads

  1. Turning a JSP into a JAVA file
    By PyroPlasm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 24th, 2012, 12:51 PM
  2. [SOLVED] Is it possible to get factorial of negative number
    By Lokesh in forum Java Theory & Questions
    Replies: 3
    Last Post: August 4th, 2011, 05:45 PM
  3. Negative
    By xew123 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 06:25 PM
  4. Need this to end when a negative number is entered
    By ponyexpress in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 28th, 2010, 09:02 AM
  5. Evaluating to negative zero
    By helloworld922 in forum Java Theory & Questions
    Replies: 6
    Last Post: June 25th, 2009, 02:34 PM