Solved

"else" statement error

Posted on 2007-03-28
9
190 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
9 Comments
 
LVL 142

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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

896 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now