-1

I am trying to read from a file that contains three numbers. The file looks like this:

45
20
32

My code is below:

import java.awt.Color;
import java.awt.Desktop.Action;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.Timer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;


public class practise implements ActionListener {

int menuCount = 0;
int [] fileValues = new int[3];

JFrame frame1 = new JFrame();
JPanel[] panels = new JPanel[3];

JMenuItem menuitemMyDialog1 = new JMenuItem( "Open File" );
JMenuItem menuitemMyDialog2 = new JMenuItem( "EXIT" );
JMenuBar menuBar = new JMenuBar( );
JMenu menuData = new JMenu( "Menu" );

Label label = new Label();

JSlider slider = new JSlider( JSlider.VERTICAL,0,100,20);;

Timer timer = new Timer(1000,new TimerAction());

void go(){

    frame1.setTitle("Referred Coursework");
    frame1.setSize(600, 300);
    frame1.setVisible(true);
    buildGUI();


    menuitemMyDialog1.addActionListener( this );
    menuData.add( menuitemMyDialog1 );

    //buildGUI();

    menuitemMyDialog2.addActionListener( this );
    menuData.add( menuitemMyDialog2 );

    menuBar.add( menuData );
    frame1.setJMenuBar( menuBar );

}
int b = 0;
class TimerAction implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(b == 3){ timer.stop(); }
        slider.setValue(fileValues[b]);
        b++;
    }
}


@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub


    if(arg0.getSource() == menuitemMyDialog1){
        menuCount = 1;
        String inputValue = JOptionPane.showInputDialog("File Open dialog box");
        label.setSize(80,80);
        label.setText(inputValue);
        label.setLocation(40,160);

        //fileValues[1] = 27;  fileValues[0] = 2;  fileValues[2] = 62;


        try {
            FileReader file = new FileReader("temperature.txt");
            BufferedReader buf = new BufferedReader(file);
            int i = 0;

            String s = null;
            while((s = buf.readLine()) != null){
            fileValues[i] = Integer.parseInt(s);
            i++;
            }
        }catch (Exception e){e.printStackTrace();}

        Arrays.sort(fileValues);
        for (int i : fileValues){
            System.out.println(i);
        }

        timer.start();


    }
    if(arg0.getSource() == menuitemMyDialog2){

        frame1.dispose();
        System.exit(0);
    }

    }

public void buildGUI(){
    layoutComponents();
}

public void layoutComponents(){



    JLabel label1 = new JLabel();

    JSlider slider2,slider3;
    //JProgressBar bar = new JProgressBar( JProgressBar.VERTICAL,1000, 1020 );

    panels[0] = new JPanel();
    panels[1] = new JPanel();
    panels[2] = new JPanel();

    panels[1].setBorder( new TitledBorder( "Temperature" ) );

    slider.setMajorTickSpacing(20);
    slider.setPaintTicks( true );
    slider.setPaintLabels( true );
    slider.setMinorTickSpacing(10);

    panels[1].add( slider );

    panels[1].setBackground(Color.orange);

    frame1.setLayout( new GridLayout( 1,2 ) );
    for ( int i = 0; i < panels.length;i++ ){
            frame1.add( panels[i] );
        }
}

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    practise obj = new practise();
    obj.go();


}

}

The program compiles alright and gives no errors. But when I output the contents of the array fileValues I get: 0 0 0

Any help would be appreciated. Thanks!

Update I reviewed the exception for FileReader and now it is showing a FileNotFoundException. This is strange as the file exists in the project folder. Any suggestions??

stud91
  • 1,854
  • 6
  • 31
  • 56

1 Answers1

1

You need to provide the full path for "temperature.txt".

You ignore the exceptions sent by your I/O operations:

    try {
        FileReader file = new FileReader("temperature.txt");
        BufferedReader buf = new BufferedReader(file);
        int i = 0;

        String s = null;
        while ((s = buf.readLine()) != null) {
            fileValues[i] = Integer.parseInt(s);
            i++;
        }
    } catch (Exception e) {
    }

If you replace the catch block by something like:

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

You should get a self explanatory message.

assylias
  • 321,522
  • 82
  • 660
  • 783
  • 1
    And does it work when using the full path? Have you tried debugging step by step to see what goes wrong? – assylias Jul 07 '12 at 07:34
  • You do see the output with 0 0 0 right? And there is only one place where you print the array? – assylias Jul 07 '12 at 07:43
  • I donot know how to do it properly. I am running the code on Eclipse – stud91 Jul 07 '12 at 08:02
  • You need to set a breakpoint on the filereader line and run your program in debug mode ( for more details => google). That should help you find the problem. Good luck! – assylias Jul 07 '12 at 08:03
  • Thanks for your help! I tried the debugger. It gives the file not found exception. It wasn't giving it before like when i run the program. However, it is strange that it is giving this error even when I am using the full path of the file. – stud91 Jul 07 '12 at 08:13
  • I am using like this now: File f = new File("C:\\Users\\Haroon\\workspace\\mahad\\temperature.txt"); FileReader file = new FileReader(f); – stud91 Jul 07 '12 at 09:03
  • It looks fine... I suggest you create a new project with just a main that tries to open the file and post any issues you encounter as a new, more focused, question. – assylias Jul 07 '12 at 09:08
  • I did what you said. Same problem...Someone is suggesting there could be a problem with the PATH environmental variable. Is it so? – stud91 Jul 07 '12 at 09:15
  • Your new question is much better because the scope is narrower. I'm sure you will get an answer. – assylias Jul 07 '12 at 09:46