-
Can Somebody check this code?
Hello,
Can somebody check this code, cause I literally cant save or load anything ?! :S
package pkgtry.catchexercises;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class FileDemo1 extends JFrame implements ActionListener{
private TextField inputTextArea = new TextField(20);
private TextField outputTextArea = new TextField(20);
private JButton saveButton = new JButton("save");
private JButton loadButton = new JButton("load");
private FileWriter outFile;
public static void main(String[] args){
new FileDemo1();
public FileDemo1(){
setSize(350, 300);
setTitle("File Output Demo");
add("East", inputTextArea);
add("West", outputTextArea);
JPanel bottom = new JPanel();
add("South", bottom);
bottom.add(loadButton);
bottom.add(saveButton);
loadButton.addActionListener(this);
saveButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent evt){
if(evt.getSource()==saveButton){
try{
outFile = new FileWriter("testout.txt", true);
outFile.write(inputTextArea.getText());
outFile.close();
}catch(IOException e){
System.err.println("File Error:"+e);
System.exit(1);
}
}
if(evt.getSource()==loadButton){
try{
inFile = new BufferedReader(new FileReader("FileDemo1.txt"));
outFile.write(inputTextArea.getText());
outArea.setText("");
String line;
while(line = inFile.readLine())! =null){
outputArea.append(line + "\n");
}
inFile.close();
}catch(IOException e){
JOptionPane.showMessageDialog(this, "File Not Found");
}
}
}
-
Re: Can Somebody check this code?
Quote:
I literally cant save or load anything
What is the program supposed to do?
Please explain what the program does and why that is not what you want it to do.
Try debugging the code by adding printlns to show where the execution flow goes and the values of variables as they change.
-
Re: Can Somebody check this code?
Quote:
Originally Posted by
Norm
What is the program supposed to do?
Please explain what the program does and why that is not what you want it to do.
Try debugging the code by adding printlns to show where the execution flow goes and the values of variables as they change.
Well actually its suppose to be able to open up a GUI which already does and there are two individual columns of which one consists of save and the other load !
And on the save column its suppose to save the text after clicking on save and when clicked on load button its supposed to load that saved text eventually !
And I CANT DO THAT SOMEHOW :S !
Thats my problem hope it helps .... for you to come up with a solution if you know the answer ?!
Thanks
Kind Regards,
manager00104
-
Re: Can Somebody check this code?
Please explain in some detail.
What happens when you execute the code?
Did you try this:
Try debugging the code by adding printlns to show where the execution flow goes and the values of variables as they change.
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
-
Re: Can Somebody check this code?
Quote:
Originally Posted by
Norm
Please explain in some detail.
What happens when you execute the code?
Did you try this:
Try debugging the code by adding printlns to show where the execution flow goes and the values of variables as they change.
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
Look man,
When I execute the code it literally displays a GUI which I already told you one column a save one and the other load column. And WHAT ITS SUPPOSE TO DO IS .... ITS SUPPOSE TO SAVE THE FUCKING FILE THE (TEXT) AND THEN LOAD THE GOD DAMN FILE(TEXT) !!!!
WHICH THE TEXT WILL BE DISPLAYED ON THOSE 2 COLUMNS THAT WERE CREATED WELL ITS ACTUALLY GOING TO BE SEEN IN LoAD COLUMN NORMALLY, CAUSE THE SAVE COLUMN IS EDITABLE !!!!
DOES THAT ANSWER YOUR GOD DAMN QUESTION ?? ... CAUSE I REALLY DONT KNOW HOW TO SUMMARIZE THIS IN A WAY WHERE I CAN GET YOU TO UNDERSTAND THIS EVEN MORE SPECIFIC !
-
Re: Can Somebody check this code?
Look dude,
It displays these Exceptions !!!
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
at pkgtry.catchexercises.FileDemo1.main(FileDemo1.jav a:20)
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: variable inFile
location: class pkgtry.catchexercises.FileDemo1
at pkgtry.catchexercises.FileDemo1.actionPerformed(Fi leDemo1.java:51)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.jav a:6504)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
at java.awt.Component.processEvent(Component.java:626 9)
at java.awt.Container.processEvent(Container.java:222 9)
at java.awt.Component.dispatchEventImpl(Component.jav a:4860)
at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
at java.awt.Component.dispatchEvent(Component.java:46 86)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713 )
at java.awt.Component.dispatchEvent(Component.java:46 86)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:707)
at java.awt.EventQueue.access$000(EventQueue.java:101 )
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 677)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:90)
BUILD SUCCESSFUL (total time: 11 seconds)
OH BTW, IM WORKING ON NETBEANS !! (THE LATEST VERSION)
-
Re: Can Somebody check this code?
I can not see what happens when you execute the code so I am asking you to tell me what you do and what happens when you do it. So far you have said the program starts and displays a GUI. Then what??? Do you do anything? If so, what do you do? What does the program do then?
Are you expecting the program to read your mind about what you want it to do?
Do you press any of the buttons shown on the GUI?
If you don't press any of the buttons, the program will NOT do anything but wait for you.
-
Re: Can Somebody check this code?
IT NORMALLY DISPLAYS THIS !!!! FOR AN EXCEPTION THE OTHER ONE MIGHT BE THE WRONG ONE I SENT YOU !!!
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - illegal start of expression
at pkgtry.catchexercises.FileDemo1.main(FileDemo1.jav a:20)
-
Re: Can Somebody check this code?
Quote:
Uncompilable source code - illegal start of expression
at pkgtry.catchexercises.FileDemo1.main(FileDemo1.jav a:20)
That looks like a compiler error.
What statement is on line 20?
-
Re: Can Somebody check this code?
look,
when i press those buttons will not function thats my concern the problem is within those lines !! And i dont know where those problems are in the code i sent you in the very beginning ? if you know what i mean ...
-
Re: Can Somebody check this code?
Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
-
Re: Can Somebody check this code?
What statement is on line 20?
-
Re: Can Somebody check this code?
Its says public FileDemo1(){
-
Re: Can Somebody check this code?
public FileDemo1(){
setSize(350, 300);
setTitle("File Output Demo");
add("East", inputTextArea);
add("West", outputTextArea);
JPanel bottom = new JPanel();
add("South", bottom);
bottom.add(loadButton);
bottom.add(saveButton);
loadButton.addActionListener(this);
saveButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
-
Re: Can Somebody check this code?
Did you try this on NetBeans ?????
-
Re: Can Somebody check this code?
That statement looks like the beginning of a constructor definition. However, it is inside of the main() method.
You should end the main() method with a } before starting the definition of the constructor.
-
1 Attachment(s)
Re: Can Somebody check this code?
Look at the attachment !!!
-
Re: Can Somebody check this code?
Please post your questions on the forum.
-
1 Attachment(s)
Re: Can Somebody check this code?
Look dude we are learning now, btw im just a beginner ...
about Exceptions in Java !
and here is the ppt file that we looked through
Attachment 1005
-
Re: Can Somebody check this code?
And that code is about this ppt file !
-
Re: Can Somebody check this code?
Do you have any questions or problems? If so, please post your specific questions here with some details about them.
-
Re: Can Somebody check this code?
Well, could you explain how I am going to solve that code, I GAVE YOU ... IN THE VERY BEGINNING I DONT SEE WHATS SO UNCLEAR ABOUT IT, IF YOU ARE AN EXPERT IN JAVA, YOU WONT BE HAVING ANY ISSUES OR PROBLEMS WHAT SO EVER TO TELL ME WHATS WRONG WITH IT ... AND YOU ARE ASKING ME WHAT THE CODE NEEDS TO DO ! ... SERIOUSLY MAN I TOLD YOU WHAT MY PROBLEMS WERE !!
I REALLY HAD IT ... HERE IS ANOTHER TRY MY CODE IS BELOW AGAIN FOR YOU TO SEE AND MANIPULATE & IMPLEMENT IT !
package pkgtry.catchexercises;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class SaveLoadBrowser extends JFrame implements ActionListener{
private TextField inputTextArea = new TextField(20);
private TextField outputTextArea = new TextField(20);
private JButton saveButton = new JButton("save");
private JButton loadButton = new JButton("load");
private FileWriter outFile;
public static void main(String[] args){
new SaveLoadBrowser();
public SaveLoadBrowser(){
setSize(350, 300);
setTitle("File Output Demo");
add("East", inputTextArea);
add("West", outputTextArea);
JPanel bottom = new JPanel();
add("South", bottom);
bottom.add(loadButton);
bottom.add(saveButton);
loadButton.addActionListener(this);
saveButton.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent evt){
if(evt.getSource()==saveButton){
try{
outFile = new FileWriter("testout.txt", true);
outFile.write(inputTextArea.getText());
outFile.close();
}catch(IOException e){
System.err.println("File Error:"+e);
System.exit(1);
}
}
if(evt.getSource()==loadButton){
try{
inFile = new BufferedReader(new FileReader("SaveLoadBrowser.txt"));
outFile.write(inputTextArea.getText());
outArea.setText("");
String line;
while(line = inFile.readLine())! =null){
outputArea.append(line + "\n");
}
inFile.close();
}catch(IOException e){
JOptionPane.showMessageDialog(this, "File Not Found");
}
}
}
-
Re: Can Somebody check this code?
THIS IS FAR AS I CAN GO BY PUTTING IN THE CODE, AND FURTHER PROBLEMS OCCUR AS I SAID BEFORE ... SO IF YOU KNOW ANY OTHER SOLUTION TO THESE PROBLEMS TELL ME WHAT TO CODE NEXT OR CHANGE CODE(S) IF THERE IS SOMETHING WRONG WITH IT!
-
Re: Can Somebody check this code?
I guess you are going to have to fire me and hire someone else to help you.
If you are not going to explain the problems you are having with the code, there is no way anyone can help you.
If you want help post the full text of the error messages. Don't expect anyone to do your work for you. Your job is to tell us exactly what the problem is you are having. If you don't tell us, we can't see over your shoulder to see what you are seeing on your PC.
-
Re: Can Somebody check this code?
It's a waste of time for me to post coding suggestions. The code in post#22 does not contain the changes I suggested in post#16