Results 1 to 2 of 2
  1. #1
    stud91 is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Error in reading from a file containing integers

    Before the admin knows and locks this thread I honestly declare I have posted this same question on stackoverflow.com. Here is the link:

    XML Code:
    http://stackoverflow.com/questions/11373002/read-from-a-file-containing-integers-java
    However, I wasn't able to get a decent reply. I am facing a filenotfound exception error despite the file being in the projects folder in workspace. The code is as follows:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    
    import javax.swing.Timer;
    
    import javax.swing.*;
    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 {
    				//File f = new File("C:\\Users\\Haroon\\workspace\\mahad\\temperature.txt");
    				FileReader file = new FileReader(inputValue);
    				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 FileWriter method used is on line 80. Any help would be well appreciated. Thanks!
    Last edited by stud91; 07-07-2012 at 11:00 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,044
    Blog Entries
    7
    Rep Power
    23

    Default Re: Error in reading from a file containing integers

    Your program asks for the file path; type in its exact (absolute) path and preferably use forward slashes, i.e. C:/Users/Haroon/workspace/mahad/temperature.txt

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Reading only integers from text files.
    By Fatz in forum New To Java
    Replies: 8
    Last Post: 05-17-2012, 06:14 PM
  2. Error reading file
    By sehudson in forum New To Java
    Replies: 3
    Last Post: 03-01-2011, 05:44 AM
  3. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 06:45 PM
  4. Reading A CSV file error.
    By jetnor in forum New To Java
    Replies: 3
    Last Post: 02-05-2011, 04:51 AM
  5. reading from a zip file, error
    By Mr tuition in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2008, 01:39 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •