8 Replies - 225 Views - Last Post: 23 March 2011 - 04:53 PM Rate Topic: -----

#1 sigfrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 10

Exception

Posted 23 March 2011 - 05:01 AM

hi, my program is about stacks.. my only problem is there's an error in my program, I can't seem to solve it,..
it says that I need to throw an exception. But I already did,. and if I put exception on the listener, there's more error.. The program is about stacks.. even if you close the program, all elements inside the stacks will remain.. If I open it again, the elements will be there, nothing will change.. here's my program..
import java.util.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class STACKS extends JFrame implements ActionListener
{
	private JButton push,pop,peek,display,exit;
	private JLabel Stack;
	static int arr[],x=0,b=0,xy=0,c=0;
	static String in="",INDEX="",d="",s="";
	static String id[]={};

	public STACKS()
	{
		super("STACKS MENU!^_^/>");
		Container c=getContentPane();
		c.setLayout(new FlowLayout());
		Stack=new JLabel("STACKS MENU!^_^/>");	c.add(Stack);
		push=new JButton("Push!^-^/>");
		pop=new JButton("Pop!^-^/>");
		peek=new JButton("Peek!^-^/>");
		display=new JButton("Display!^-^/>");
		exit=new JButton("Exit!^-^/>");
		push.addActionListener(this); 			c.add(push);
		pop.addActionListener(this);			c.add(pop);
		peek.addActionListener(this); 			c.add(peek);
		display.addActionListener(this);		c.add(display);
		exit.addActionListener(this); 			c.add(exit);
		setVisible(true);
		setSize(150,250);
	}
	public static void saveMe() throws IOException
	{
		File data1=new File("Sample.txt");		//a file was created...
		PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(data1,false)));
		for(int x=0;x<4;x++)
		{
			out.print(":"+arr[x]);			//here in this section, I put : in every elements inside the array..
		}

		out.close();

	}
	public static void readMe() throws IOException
	{
		Scanner txtFile=(new Scanner("Sample.txt"));

		for(int y=0;x<id.length;y++)		
		{
			s=txtFile.nextLine();
			id=s.split(":");						//I used split method to separate elements and 
			arr[y]=Integer.parseInt(id[y]);			//change them into an integer again
		}

	}
	public  void actionPerformed(ActionEvent a)
	{
		readMe();				//here's my problem.. I called the readMe method to read the file..
		if (xy==0)
		{
			INDEX=JOptionPane.showInputDialog(null,"Enter LENGTH of the array!");
			c=Integer.parseInt(INDEX); xy=1;
			arr=new int[c];
		}
		if(a.getSource()==push)
		{
			in=JOptionPane.showInputDialog(null,"Enter integer to be pushed!");
			b=Integer.parseInt(in);
			arr[x]=b;	x+=1;
			if(x==c)
			{
				JOptionPane.showMessageDialog(null,"WARNING! The stacks are full, please pop something!^-^/>");
			}
			if(x>c)
			{
				JOptionPane.showMessageDialog(null,"Sorry, the stacks are full,please pop something first!^-^/>");
				x-=1;
			}
		}
		else if(a.getSource()==pop)
		{

			arr[x-1]=0;x-=1;
			JOptionPane.showMessageDialog(null,"The value has been popped!^-^/>");
			if(x==0)
			{
				JOptionPane.showMessageDialog(null,"The stacks are empty, push something!^-^/>");
			}

		}
		else if(a.getSource()==peek)
		{
			JOptionPane.showMessageDialog(null,"The value is "+arr[x-1]+"! ^-^/>");
		}
		else if(a.getSource()==display)
		{
			for(int y=c-1;y>-1;y--)
			{
				d+="*** "+arr[y]+" ***\n";
			}
			JOptionPane.showMessageDialog(null,"The value inside the stacks are:\n"+d);
			d="";
		}
		else if(a.getSource()==exit)
		{
			System.exit(0);
		}
		saveMe();	//here is where I called the saveMe() method to save it...
	}
	public static void main(String args[])
	{
		STACKS pot=new STACKS();
		pot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
}




Is This A Good Question/Topic? 0
  • +

Replies To: Exception

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10181
  • View blog
  • Posts: 37,595
  • Joined: 27-December 08

Re: Exception

Posted 23 March 2011 - 06:04 AM

Post the specific error message from your compiler, including the line number.
Was This Post Helpful? 0
  • +
  • -

#3 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 239
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Exception

Posted 23 March 2011 - 07:06 AM

Your saveMe and readMe methods need try/catch wrapped around them since they can throw IOExceptions.

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


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


That should stop your compiler problem.
//I copied and pasted this into netbeans and it immediately pointed out the errors, just as any decent IDE would.

Yours,
Shane~

This post has been edited by ShaneK: 23 March 2011 - 07:08 AM

Was This Post Helpful? 0
  • +
  • -

#4 sigfrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 10

Re: Exception

Posted 23 March 2011 - 04:00 PM

View PostShaneK, on 23 March 2011 - 07:06 AM, said:

Your saveMe and readMe methods need try/catch wrapped around them since they can throw IOExceptions.

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


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


That should stop your compiler problem.
//I copied and pasted this into netbeans and it immediately pointed out the errors, just as any decent IDE would.

Yours,
Shane~

thank you! it works!
Was This Post Helpful? 0
  • +
  • -

#5 sigfrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 10

Re: Exception

Posted 23 March 2011 - 04:24 PM

hi, I want to ask what's the problem with my program..
My program is about stacks, there's no error in it, but my problem is, our requirement is do all operations in stacks and then, even if you close the program, the next time you will open it, the previous data should still be displayed..
I used the filewriter and filereader, but every time I close it, then open it again, the previous data doesn't appear..
Any advice?
thanks.. here's my program..
import java.util.*;
import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class STACKS extends JFrame implements ActionListener
{
	private JButton push,pop,peek,display,exit;
	private JLabel Stack;
	static int arr[],x=0,b=0,xy=0,c=0;
	static String in="",INDEX="",d="",s="";
	static String id[]={};

	public STACKS()
	{
		super("STACKS MENU!^_^/>");
		Container c=getContentPane();
		c.setLayout(new FlowLayout());
		Stack=new JLabel("STACKS MENU!^_^/>");	c.add(Stack);
		push=new JButton("Push!^-^/>");
		pop=new JButton("Pop!^-^/>");
		peek=new JButton("Peek!^-^/>");
		display=new JButton("Display!^-^/>");
		exit=new JButton("Exit!^-^/>");
		push.addActionListener(this); 			c.add(push);
		pop.addActionListener(this);			c.add(pop);
		peek.addActionListener(this); 			c.add(peek);
		display.addActionListener(this);		c.add(display);
		exit.addActionListener(this); 			c.add(exit);
		setVisible(true);
		setSize(150,250);
	}
	public static void saveMe() throws IOException
	{
		File data1=new File("Sample.txt");		//a file was created...
		PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(data1,false)));
		for(int x=0;x<4;x++)
		{
			out.print(":"+arr[x]);			//here in this section, I put : in every elements inside the array..
		}

		out.close();

	}
	public static void readMe() throws IOException
	{
		Scanner txtFile=(new Scanner("Sample.txt"));

		for(int y=0;x<id.length;y++)
		{
			s=txtFile.nextLine();
			id=s.split(":"); xy=1;
			arr[y]=Integer.parseInt(id[y]);
		}

	}
	public  void actionPerformed(ActionEvent a)
	{
		try
		{
			readMe();								//here is where the previous data will be read.. 
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null,"File not found! readme !^,^");
		}
		if (xy==0)
		{
			INDEX=JOptionPane.showInputDialog(null,"Enter LENGTH of the array!");
			c=Integer.parseInt(INDEX); xy=1;
			arr=new int[c];
		}
		if(a.getSource()==push)
		{
			in=JOptionPane.showInputDialog(null,"Enter integer to be pushed!");
			b=Integer.parseInt(in);
			arr[x]=b;	x+=1;
			if(x==c)
			{
				JOptionPane.showMessageDialog(null,"WARNING! The stacks are full, please pop something!^-^/>");
			}
			if(x>c)
			{
				JOptionPane.showMessageDialog(null,"Sorry, the stacks are full,please pop something first!^-^/>");
				x-=1;
			}
		}
		else if(a.getSource()==pop)
		{

			arr[x-1]=0;x-=1;
			JOptionPane.showMessageDialog(null,"The value has been popped!^-^/>");
			if(x==0)
			{
				JOptionPane.showMessageDialog(null,"The stacks are empty, push something!^-^/>");
			}

		}
		else if(a.getSource()==peek)
		{
			JOptionPane.showMessageDialog(null,"The value is "+arr[x-1]+"! ^-^/>");
		}
		else if(a.getSource()==display)
		{
			for(int y=c-1;y>-1;y--)
			{
				d+="*** "+arr[y]+" ***\n";
			}
			JOptionPane.showMessageDialog(null,"The value inside the stacks are:\n"+d);
			d="";
		}
		else if(a.getSource()==exit)
		{
			System.exit(0);
		}
			try
		{
			saveMe();					//here is where the file will be saved..
		}
		catch(Exception e)
		{
			JOptionPane.showMessageDialog(null,"File not found!^,^");
		}
	}
	public static void main(String args[])
	{
		STACKS pot=new STACKS();
		pot.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	}
}



Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10181
  • View blog
  • Posts: 37,595
  • Joined: 27-December 08

Re: Exception

Posted 23 March 2011 - 04:36 PM

Duplicate threads merged.

You should flush() the PrintWriter before closing it. The flush() method actually writes the data to the File, and empties the buffer.
Was This Post Helpful? 0
  • +
  • -

#7 sigfrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 10

Re: Exception

Posted 23 March 2011 - 04:48 PM

how?

like this?

public static void saveMe() throws IOException
	{
		File data1=new File("Sample.txt");		//a file was created...
		PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(data1,false)));
		for(int x=0;x<4;x++)
		{
			out.print(":"+arr[x]); xy=1;			//here in this section, I put : in every elements inside the array..
		}
		out.flush();
		out.close();

	}


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10181
  • View blog
  • Posts: 37,595
  • Joined: 27-December 08

Re: Exception

Posted 23 March 2011 - 04:50 PM

Yes. :)
Was This Post Helpful? 0
  • +
  • -

#9 sigfrid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 04-October 10

Re: Exception

Posted 23 March 2011 - 04:53 PM

Sorry, but it still won't display the previous data,..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1