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 7 of 7

Thread: ParsingTo Interger Issue

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ParsingTo Interger Issue

    I could pare to Integer, so I have to used double in this code. Is there anyone can help on that please. Also can someone check on modulus formula it seems not working right.Please Help
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    /**
     * Sample application using Frame.
     *
     * @author 
     * @version 1.00 11/06/19
     */
    public class HomeWork6Frame extends JFrame {
    			//Creating the Labels
    	 private JLabel SecondsL, MinutesL, HoursL, DaysL, YearsL; //L stand for Label
    			//Creating the TextField
    	 private JTextField SecondsTF, MinutesTF, HoursTF, DaysTF, YearsTF;//TF stand for TextField
    			//Creating the Buttons
    	 private JButton  calculate, exit;
     
    			//Decalring actionlistener for the buttons
    	 private CalculateButtonHandler cbHandler;
    	 private ExitButtonHandler ebHandler;
     
         public HomeWork6Frame()
         {
     
     
     
            	// The lables part of the program
     
    	      	 setTitle("Program to Conver Seconds into Minutes,Hours and Days");
    	      	 SecondsL = new JLabel("Number of Seconds to be convert it: ", SwingConstants.RIGHT);
    	      	 MinutesL = new JLabel("Number of Minuts: ", SwingConstants.RIGHT);
    	       	 HoursL   = new JLabel("Number of Hours: ", SwingConstants.RIGHT);
    	      	 DaysL    = new JLabel("Number of Days: ", SwingConstants.RIGHT);
    	       	 YearsL   = new JLabel("Number of Years: ", SwingConstants.RIGHT);
     
    			//Text Field protion tf the program
    			SecondsTF = new JTextField(10);
    			MinutesTF = new JTextField(10);
    			HoursTF   = new JTextField(10);
    			DaysTF    = new JTextField(10);
    			YearsTF   = new JTextField(10);
     
    			// Creating the Buttons
    			calculate = new JButton("Calculate");
    			cbHandler = new CalculateButtonHandler();
    	 		calculate.addActionListener(cbHandler);
     
    	 		exit = new JButton("Exit");
    	 		ebHandler = new ExitButtonHandler();
    	 		exit.addActionListener(ebHandler);
     
     
    		       	//Container portion of the prog
    		       Container pane = getContentPane();
    		       pane.setLayout(new GridLayout(7, 2));
     
    		       //Addin Labels and TextField to the pane
    		       pane.add(SecondsL);
    		       pane.add(SecondsTF);
    		       pane.add(YearsL);
    		       pane.add(YearsTF);
    		       pane.add(DaysL);
    		       pane.add(DaysTF);
    		       pane.add(HoursL);
    		       pane.add(HoursTF);
    		       pane.add(MinutesL);
    		       pane.add(MinutesTF);
     
     
     
     
     
     
     
    				//Adding the Buttons
    				pane.add(calculate);
    				pane.add(exit);
     
     
    		       //Windows size and visibility
    		       setSize(new Dimension(400, 300));
    		       setVisible(true);
     
     
        }
     
     
        /**
         * Shutdown procedure when run as an application.
         */
        protected void windowClosed()
        {
     
                System.exit(0);
        }
     
    		/**********************************************************
    		 ********** Calculate  class ******************************/
    	    private class CalculateButtonHandler implements ActionListener
    	    {
    			public void actionPerformed(ActionEvent e)
    			{
    				//Declaring Variables
    				double seconds;
    				double minutes = 60; //seconds
    				double hours   = 3600;//seconds
    				double days    = 86400; //seconds
    				double years   = 365;
    				//double temp;
     
    			// Perofimng the calculations
    				seconds = Double.parseDouble(SecondsTF.getText());
    				minutes = seconds/minutes;
    				seconds = seconds % minutes;
    				hours   = seconds / hours;
    				seconds = seconds % hours;
    				days    = seconds / days;
    				seconds = seconds % days;
    				years   = seconds / years;
    				seconds = seconds % years;
     
    				MinutesTF.setText("" + minutes);
    				HoursTF.setText("" + hours);
    				DaysTF.setText("" + days);
    				YearsTF.setText("" + years);
     
    			}
    		}
     
    		/*********************************************************
    		 ********** Exit class***********************************/
     
    		private class ExitButtonHandler implements ActionListener
    		{
    				public void actionPerformed(ActionEvent e)
    			{
    				System.exit(0);
    			}
    		}
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ParsingTo Interger Issue

    someone check on modulus formula it seems not working right.
    please explain what your problem is. What does the code do or not do?

    To learn how some operators work, write a 2 line program to use the operator and print out the results.
    Change the operands and try it again. Look at the output. Change and do it again.
    If after several tries you still do not understand, post your code here with its output and ask your question about what you don't understand.

    I could pare to Integer, so I have to used double
    Can you explain what your problem was here? I don't understand what "could pare to Integer" means.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ParsingTo Interger Issue

    Sorry for the confusing that I may caused. But my problem is. I want to read an in Textfield as seconds then convert that seconds to minutes, hours, days. If I declare all the variables as integers and use this
     seconds = Double.parseDouble(SecondsTF.getText());
    it wont parse it, so I have to use double instead.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ParsingTo Interger Issue

    it wont parse it, so I have to use double instead.
    Please explain what happens and show an example.

    If you enter: 1234546.555 in the SecondsTF, what value should end up in the seconds variable?

    Whoops missed this:
    f I declare all the variables as integers
    Did you look at the Integer class for the method to use?
    Last edited by Norm; June 19th, 2011 at 11:59 AM. Reason: See Integer class

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ParsingTo Interger Issue

    I got that fix thanks. And now i having another issue with the formula below
    I want to divide seconds to minute to get number of minutes then hours, then days. I could not figured out how to get hours from the minutes.
    . 
     
    				minutes = seconds/minutes;
    				seconds = seconds % minutes;
    				hours   = seconds / hours;
    				seconds = seconds % hours;
    				days    = seconds / days;
    				seconds = seconds % days;
    				years   = seconds / years;
    				seconds = seconds % years;
     
    				MinutesTF.setText("" + minutes);
    				HoursTF.setText("" + hours);
    				DaysTF.setText("" + days);
    				YearsTF.setText("" + years);

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: ParsingTo Interger Issue

    I could not figured out how to get hours from the minutes.
    How many hours in 130 minutes? How did you get that answer?

    As I said in post #2:
    To learn how some operators work, write a 2 line program to use the operator and print out the results.
    Change the operands and try it again. Look at the output. Change and do it again.
    Last edited by Norm; June 19th, 2011 at 12:46 PM.

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: ParsingTo Interger Issue

    If you want to get minutes from seconds, divide by 60. If you want to get hours from seconds, divide by 3600, (or you could divide the previously calculated minutes by 60). Etc.

Similar Threads

  1. Polymorphism issue
    By LDM91 in forum Java Theory & Questions
    Replies: 5
    Last Post: November 28th, 2010, 03:26 PM
  2. JButton issue, please help
    By Khoatic in forum AWT / Java Swing
    Replies: 3
    Last Post: November 19th, 2010, 11:19 PM
  3. [SOLVED] Calendar Issue
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2010, 01:19 PM
  4. how to display combination of char and interger
    By bsrk315 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 22nd, 2010, 08:09 AM
  5. Issues with Tomcat 6.0
    By sanyog24681 in forum Java Servlet
    Replies: 0
    Last Post: October 21st, 2008, 07:55 AM