Solved

a piece of code

Posted on 1998-07-31
4
210 Views
Last Modified: 2010-03-30
     public void actionPerformed(ActionEvent ae)  {
      //****deals with the buttons pressed****//
            
            String str = ae.getActionCommand();
       
                  if(str.equals("Enter")) {
                        readNums();
                        count++;
                  }
                  else if(str.equals("Reset")) {
                        resetit();
                  }
                  else if(str.equals("Show")) {
                        setVisible(false);
                        s.setVisible(true);
                  }
      }
      public      void readNums() throws IOException{
            try {
            fos = new FileOutputStream("C:/WINDOWS/Desktop/Aljava/Project2/plane.txt");
            } catch (IOException e) {
            }
            
            for (i=0; i<14; i++) {
                  air[i] = String.valueOf(tf[i].getText());
            }
      }            
}
in the code above I wish for the user to enter 14 strings of text then read them to a file each time the enter button is pressed however it appears that you can't throw an exception out of an action performed function does anyone have any ideas how to do this. I can't call it in the main method as the funcion always requires the enter button to be pressed!
0
Comment
Question by:alanmin
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 100 total points
ID: 1229665
OK, I'm attempting to read between the lines here. Your question seems very confusing. So, in an attempt to clarify, I will restate the question, pointing out the assumptions I am making.

Your program contains 14 TextFields which the user can fill in. When he is satisfied with his entries, he presses the Enter button. This is intended to cause the program to move the entries from program memory to disk. (I am basing the direction of the move here on the fact that you have created a FileOutputStream). (This is normally referred to as "writing" (to disk) (not reading).
You think the structure is OK, but the readNums routine (which should probably be called "writeNums") must access IO which necessitates the handling of exceptions, and you are running into a problem there.

From what I see above, I assume that the compiler is complaining that while readNums is potentially throwing an IOException, the actionPerformed method is not handling it.

The solution, is to not declare "readNums" to throw an IOException. There is no need to, as any potential exception from FileOutputStream is being handled internally (by the try - catch block around it.

0
 

Author Comment

by:alanmin
ID: 1229666
public void actionPerformed(ActionEvent ae)  {
      //****deals with the buttons pressed****//
            
            String str = ae.getActionCommand();
       
                  if(str.equals("Enter")) {
                        writeNums();
                        count++;
                  }
                  else if(str.equals("Reset")) {
                        resetit();
                  }
                  else if(str.equals("Show")) {
                        setVisible(false);
                        s.setVisible(true);
                  }
      }
      public      void writeNums(){
            try {
            fos = new FileOutputStream("C:/WINDOWS/Desktop/Aljava/Project2/plane.txt");
            prt = new PrintWriter(fos);
            }
            catch(IOException e){
            }
            for (i=0; i<count; i++) {
                  for (j=0; j<14; j++) {
                  air[i][j] = String.valueOf(tf[j].getText());
                  prt.println(air[i][j]);
                  }
            }
      }
imladris i wish i hadn't started this...my first draft was a simple applet for web release the came along a lot of files...i can't really figure them out...your assumption were correct in what you have said above i guess because i do this all day i expect everyone to know what i'm on about.
What i really can't understand (lordy he wants more, sorry), is why the above doesn't work. I am increasing the count each time enter is pressed to a maximum of 3 times...then sending these values to my file yet the file is always empty??????
TA for your help
0
 
LVL 16

Expert Comment

by:imladris
ID: 1229667
The code as it stands seems reasonable. In other words there are
no obvious problems left. However, you have, politely, not posted
all the code, so various declarations are not visible. It could
be some variable being automatic rather than instance or class,
or something like that. Anyway, the next step I would take, is
to add a console printing line to the output loop. After

prt.println(air[i][j]);

add

System.out.println(air[i][j]);

and maybe before the two for loops add

System.out.println(count);

This may illuminate what it going on. At least it will verify
whether or not the loop is executing as you expect it to. If it
is, the problem is rather mysterious. If it isn't, it will just
be a matter of getting it to execute. Probably relatively
simple.

Let me know what you find either way.

0
 

Author Comment

by:alanmin
ID: 1229668
I got it all sorted
try {
file[0] = new File("C:/WINDOWS/Desktop/Project2/plane0.txt");
fWriter[0] = new FileWriter(file[0]);
for (j=0; j<6; j++) {
fWriter[0].write(air[pCount][j]);
//****to space the files for reading in again !!!don't ask
fWriter[0].write(" ");
}
fWriter[0].close();
}
catch(IOException e){
}
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
javap not working 8 58
SHA2 certs for IIS AND Java? 2 113
hibernate jars 4 45
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question