Random Number help

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1288 Views - Last Post: 06 December 2010 - 08:47 PM Rate Topic: -----

#1 z2104   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 40
  • Joined: 10-November 10

Random Number help

Posted 05 December 2010 - 05:31 AM

Hi, this program suppose to write a number of binary file and then generate them when I click read button but it doesn't.

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class RandomNum extends JFrame implements ActionListener{

private JPanel panButtons;
private JTextArea taDisplay;
private JButton btnWrite, btnRead;

    public RandomNum() {

    	panButtons = new JPanel();
    	btnWrite = new JButton("Write");
    	btnRead = new JButton("Read");
    	panButtons.add(btnWrite);
    	panButtons.add(btnRead);
    	btnWrite.addActionListener(this);
    	btnRead.addActionListener(this);

    	taDisplay = new JTextArea();

		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		c.add(taDisplay);
		c.add(panButtons, BorderLayout.SOUTH);
    }

    	public void actionPerformed(ActionEvent e){
    		if(e.getSource () == btnWrite){

    			writeFile();

    		}

    		if(e.getSource() == btnRead){

				readFile();

    		}
    	}

    	public void writeFile(){

			String message = "";

			File filename = new File("binary.txt");

    		int n = (int) (Math.random() * 100 + 1);

    		for(n=0; n<=20; n++){
		try{
    			FileOutputStream fOut = new FileOutputStream(filename);
				DataOutputStream dOut = new DataOutputStream(fOut);
    			dOut.writeInt(n);
    			dOut.close();
    			message = "Done";
    			taDisplay.setText(message);

    		}
    		catch(IOException e){
    			taDisplay.setText("Error");
    		}
    		}

    	}


    	public void readFile(){

	  		FileInputStream fis = null;
	  		BufferedInputStream bis = null;
	  		DataInputStream dis = null;

			try{
				fis = new FileInputStream("binary.txt");
				bis = new BufferedInputStream(fis);
				dis = new DataInputStream(bis);

				while (dis.available() !=0){
					taDisplay.setText(dis.readLine());
				}

				fis.close();
				dis.close();
				bis.close();
			}
			catch(IOException i){

			}
    	}

			/*try{
				FileInputStream fis = new FileInputStream("binary.txt");
				DataInputStream dis = new DataInputStream(fis);
				BufferedReader br = new BufferedReader(new InputStreamReader(dis));
				String str;
				while ((str = br.readLine())!=null){
					taDisplay.append(str);
				}
				dis.close();
			}
			catch (IOException i){
				taDisplay.setText("Error" + i.getMessage());
			}*/

        public static void main(String[] args){

    		RandomNum frame = new RandomNum();
    		frame.setTitle("Random Number");
    		frame.setSize(850, 200);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}


Is This A Good Question/Topic? 0
  • +

Replies To: Random Number help

#2 darek9576   User is offline

  • D.I.C Lover

Reputation: 204
  • View blog
  • Posts: 1,747
  • Joined: 13-March 10

Re: Random Number help

Posted 05 December 2010 - 09:27 AM

Not enturile sure but on line 57 you close the stream. Therefore it is going to write the first iteration and then its being closed. Take it out of the loop, so that the loop comlpletes and then close the stream. As i said, not sure. Try it.
Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Random Number help

Posted 05 December 2010 - 11:26 AM

View Postdarek9576, on 05 December 2010 - 10:27 AM, said:

Not enturile sure but on line 57 you close the stream. Therefore it is going to write the first iteration and then its being closed. Take it out of the loop, so that the loop comlpletes and then close the stream. As i said, not sure. Try it.

It is exactly that. You should
- new FileOutputStream()
- new DataOutputStream()
- perform the for() loop that write to the file
- close the stream

Righ now you create/open/close at each iteration of the loop
Was This Post Helpful? 0
  • +
  • -

#4 z2104   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 40
  • Joined: 10-November 10

Re: Random Number help

Posted 06 December 2010 - 05:46 AM

So am I doing the wrong way?

View Postdarek9576, on 05 December 2010 - 11:27 PM, said:

Not enturile sure but on line 57 you close the stream. Therefore it is going to write the first iteration and then its being closed. Take it out of the loop, so that the loop comlpletes and then close the stream. As i said, not sure. Try it.


I did what you told me to... but still the same
Was This Post Helpful? 0
  • +
  • -

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Random Number help

Posted 06 December 2010 - 06:34 AM

Post your updated code.
Was This Post Helpful? 0
  • +
  • -

#6 z2104   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 40
  • Joined: 10-November 10

Re: Random Number help

Posted 06 December 2010 - 06:36 AM

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class RandomNum extends JFrame implements ActionListener{

private JPanel panButtons;
private JTextArea taDisplay;
private JButton btnWrite, btnRead;

    public RandomNum() {

    	panButtons = new JPanel();
    	btnWrite = new JButton("Write");
    	btnRead = new JButton("Read");
    	panButtons.add(btnWrite);
    	panButtons.add(btnRead);
    	btnWrite.addActionListener(this);
    	btnRead.addActionListener(this);

    	taDisplay = new JTextArea();

		Container c = getContentPane();
		c.setLayout(new BorderLayout());
		c.add(taDisplay);
		c.add(panButtons, BorderLayout.SOUTH);
    }

    	public void actionPerformed(ActionEvent e){
    		if(e.getSource () == btnWrite){

    			writeFile();

    		}

    		if(e.getSource() == btnRead){

				readFile();

    		}
    	}

    	public void writeFile(){

			String message = "";

			File filename = new File("binary.dat");

    		int n = (int) (Math.random() * 100 + 1);

    		for(n=0; n<=20; n++){
		try{
    			FileOutputStream fOut = new FileOutputStream(filename);
				DataOutputStream dOut = new DataOutputStream(fOut);
    			dOut.writeInt(n);
    			message = "Done";
    			taDisplay.setText(message);
				dOut.close();
    		}
    		catch(IOException e){
    			taDisplay.setText("Error");
    		}
    		}

    	}


    	public void readFile(){

	  		FileInputStream fis = null;
	  		BufferedInputStream bis = null;
	  		DataInputStream dis = null;

			try{
				fis = new FileInputStream("binary.dat");
				bis = new BufferedInputStream(fis);
				dis = new DataInputStream(bis);

				while (dis.available() !=0){
					taDisplay.setText(dis.readLine());
				}

				fis.close();
				dis.close();
				bis.close();
			}
			catch(IOException i){

			}
    	}

        public static void main(String[] args){

    		RandomNum frame = new RandomNum();
    		frame.setTitle("Random Number");
    		frame.setSize(850, 200);
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

This post has been edited by z2104: 06 December 2010 - 06:37 AM

Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Random Number help

Posted 06 December 2010 - 06:39 AM

You have not effectively changed anything at all. You are still closing the stream INSIDE the loop. You have to move the close() call to after the loop.
Was This Post Helpful? 0
  • +
  • -

#8 z2104   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 40
  • Joined: 10-November 10

Re: Random Number help

Posted 06 December 2010 - 06:44 AM

You mean put the close() call outside the try-catch? or Outside for-loop? But it won't be able to find the variable (dOut), if I put outside.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Random Number help

Posted 06 December 2010 - 06:47 AM

Yes. And you can, just change the scope.
                DataOutputStream dOut = null;
                for(n=0; n<=20; n++){
		try{
    			FileOutputStream fOut = new FileOutputStream(filename);
		        dOut = new DataOutputStream(fOut);
    			dOut.writeInt(n);
    			message = "Done";
    			taDisplay.setText(message);
				;
    		}
    		catch(IOException e){
    			taDisplay.setText("Error");
    		}
    		}
    		if (dOut != null)
    		    dOut.close();


Was This Post Helpful? 0
  • +
  • -

#10 z2104   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 40
  • Joined: 10-November 10

Re: Random Number help

Posted 06 December 2010 - 06:54 AM

View PostDogstopper, on 06 December 2010 - 08:47 PM, said:

Yes. And you can, just change the scope.
                DataOutputStream dOut = null;
                for(n=0; n<=20; n++){
		try{
    			FileOutputStream fOut = new FileOutputStream(filename);
		        dOut = new DataOutputStream(fOut);
    			dOut.writeInt(n);
    			message = "Done";
    			taDisplay.setText(message);
				;
    		}
    		catch(IOException e){
    			taDisplay.setText("Error");
    		}
    		}
    		if (dOut != null)
    		    dOut.close();



Hmm... I did what you told me to. There's an error says
unreported exception java.io.IOException; must be caught or declared to be thrown

Do I need to do try-catch?
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Random Number help

Posted 06 December 2010 - 06:56 AM

Yes. Or make the method throw it. One or the other.
Was This Post Helpful? 0
  • +
  • -

#12 z2104   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 40
  • Joined: 10-November 10

Re: Random Number help

Posted 06 December 2010 - 07:01 AM

Okay. By the way, thanks for your help :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: Random Number help

Posted 06 December 2010 - 01:54 PM

You're Welcome. Glad I could help.
Was This Post Helpful? 0
  • +
  • -

#14 m-e-g-a-z   User is offline

  • Winning
  • member icon


Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: Random Number help

Posted 06 December 2010 - 02:00 PM

View PostDogstopper, on 06 December 2010 - 12:47 PM, said:

Yes. And you can, just change the scope.
                DataOutputStream dOut = null;
                for(n=0; n<=20; n++){
		try{
    			FileOutputStream fOut = new FileOutputStream(filename);
		        dOut = new DataOutputStream(fOut);
    			dOut.writeInt(n);
    			message = "Done";
    			taDisplay.setText(message);
				;
    		}
    		catch(IOException e){
    			taDisplay.setText("Error");
    		}
    		}
    		if (dOut != null)
    		    dOut.close();



And removing the extra semi-colon :)
Was This Post Helpful? 0
  • +
  • -

#15 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Random Number help

Posted 06 December 2010 - 08:25 PM

Hey folks !!! You are drunk or tired ?

              DataOutputStream dOut = null;
              for(n=0; n<=20; n++){
try{
  			FileOutputStream fOut = new FileOutputStream(filename);
        dOut = new DataOutputStream(fOut);
  			dOut.writeInt(n);
  			message = "Done";
  			taDisplay.setText(message);
		;
  		}
  		catch(IOException e){
  			taDisplay.setText("Error");
  		}
  		}
  		if (dOut != null)
  		    dOut.close();


you are opening the file 20 times
            
              try {
  	           FileOutputStream fOut = new FileOutputStream(filename);
                   DataOutputStream dOut = new DataOutputStream(fOut);
                   for(n=0; n<=20; n++){
  			dOut.writeInt(n);
                   }
                   dout.close();
  		   message = "Done";
  		   taDisplay.setText(message);
  		}
  		catch(IOException e){
  			taDisplay.setText("Error");
  		}



Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2