Solved

"else" statement error

Posted on 2007-03-28
9
194 Views
Last Modified: 2013-11-23
Why am I getting an error for the "else" statement?
import java.util.Scanner;
import java.io.*;
import javax.swing.*;

public class DisplayFile
{
   //-----------------------------------------------------------------
   //  Opens a file chooser dialog, reads the selected file and
   //  loads it into a text area.
   //-----------------------------------------------------------------
   public static void main (String[] args) throws IOException
   {
      JFrame frame = new JFrame ("Display File");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      JTextArea ta = new JTextArea (20, 30);
      JFileChooser chooser = new JFileChooser();

      int status = chooser.showOpenDialog (null);

      if (status != JFileChooser.APPROVE_OPTION)
         ta.setText ("No File Chosen");
      else
      {
         File file = chooser.getSelectedFile();
         Scanner scan = new Scanner (file);

         String info = "";
         while (scan.hasNext())
            info += scan.nextLine() + "\n";

         ta.setText (info);
      }

      frame.getContentPane().add (ta);
      frame.pack();
      frame.setVisible(true);
   }
}
0
Comment
Question by:jmurguia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 18811158
this should work better:

 if (status != JFileChooser.APPROVE_OPTION)
   {
         ta.setText ("No File Chosen");
    }
        else
      {
  ....

similary, use { } for while also, even if it's only a single line of code:

while (scan.hasNext())
{
            info += scan.nextLine() + "\n";
}

looks like more work/code, but I assure you it will be alot easier to read...


0
 
LVL 14

Expert Comment

by:CPColin
ID: 18811160
What error are you getting?
0
 

Author Comment

by:jmurguia
ID: 18811260
Error = "else" without "if".

Changed to above and the error went the "if" statement. "{" expected. Now what?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
ID: 18811300
It looks like you have not posted the correct code - that one looks fine. There is an extra { or } hanging somewhere in between.
0
 

Author Comment

by:jmurguia
ID: 18811423
Is that a good ending to this file?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18812228
Looks like - can you post the updated code
0
 
LVL 1

Expert Comment

by:sirjulio
ID: 18812957
hey jm,
Like mayan and colin, I don't see the error. The code is compiling and running fine in current state.
0
 
LVL 4

Expert Comment

by:ashwanilabs
ID: 18814573
Which editor you are using? I think you should use eclipse then you will not face such problems. Even if you get into some problem like this eclipse show the recommended solution on the same line. So strongly suggest you to use eclipse. It is THE BEST
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18827712
So what was the problem?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

751 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