Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

My out.write wont write to my file

$
0
0
Making an odometer for class to calculate miles per gallons. Using actionlistener to implement buttons. My calculate button works but i'm suppose to be able to close the program and have it automatically replace the old odometer miles with the new ones entered. I've been using inputFile and outputFile to record them however my program wont write to the file and wont read either, can someone set me on the right path?


package mpg;
import javax.swing.*;
import java.awt.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.io.*;


public class MPG extends JFrame 
{

    public static MPG win;
   
    public static JLabel currentodometer = new JLabel("");
    public static JLabel gallonsLabel = new JLabel("");
    public static JLabel mpgLabel = new JLabel("");
    public static JTextField oldMiles;
    public static JTextField newMiles;
    public static JTextField usergallons;
    int podometer =0;
    int codometer=0;
    InputFile in =new InputFile("mpgIn.txt");
    OutputFile out =new OutputFile("mpgOut.txt");
    

    
    
    public MPG()//constructor
    {
        //set parameters
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setSize(250, 250);
        

        //add panels
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2, 5, 5));
        this.setTitle("MPG");

        //setting labels to my panels
        JLabel label1 = new JLabel("Previous Odometer ");
        oldMiles = new JTextField(10);
        JLabel codometer = new JLabel("Current Odometer: ");
        newMiles = new JTextField(10);
        JLabel gallons = new JLabel("Gallons: ");
        usergallons = new JTextField(10);
        JLabel mpg = new JLabel("MPG: ");
        JTextField mpg1 = new JTextField(10);
        JButton calculate1 = new JButton("Calculate");
        JButton saveExit = new JButton("Save/Exit");


        //put object in window
        this.add(panel);
        panel.add(label1);
        panel.add(oldMiles);
        panel.add(codometer);
        panel.add (newMiles);
        panel.add(gallons);
        panel.add(usergallons);
        panel.add(mpg);
        panel.add(mpgLabel);
     
        panel.add(saveExit);
        panel.add(calculate1);


        //add the button listener
        calculate1.addActionListener(new ButtonListener());
        //add text box to listener
        oldMiles.addActionListener(new ButtonListener());
        saveExit.addActionListener(new ButtonListener1());




    }//end of constructor


    ///********************************************************
    class ButtonListener implements ActionListener 
    {

       @Override
        public void actionPerformed(ActionEvent ae) 
        {
            int podometer = Integer.parseInt(oldMiles.getText());
            int codometer = Integer.parseInt(newMiles.getText());
            double uGallons = Integer.parseInt(usergallons.getText());
            double result = 0;
            
           
            result=((codometer-podometer)/uGallons);
            
            DecimalFormat twoDForm = new DecimalFormat("#.##");
            
            
            System.out.println(" "+ podometer);
            System.out.println(" "+ codometer);
            System.out.println(" "+ uGallons);
            System.out.println(" "+ result );
            System.out.println(""+ Double.valueOf(twoDForm.format(result)));
            
            mpgLabel.setText(" "+ Double.valueOf(twoDForm.format(result)));
            
         
        }//end of actionlistener

    }//end of class    
    
    class ButtonListener1 implements ActionListener 
    {//save/exit button

        @Override
        public void actionPerformed(ActionEvent ae) 
        {
           
            
            out.writeInt(codometer);

            
            
          win.dispose();
        }
        
    }
    
    //********************************************************

    public static void main(String[] args) 
    {
        win = new MPG();
        win.setVisible(true);
       

    }//end of main
}//end of class







Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>