• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

"else" statement error

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
jmurguia
Asked:
jmurguia
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
CPColinSenior Java ArchitectCommented:
What error are you getting?
0
 
jmurguiaAuthor Commented:
Error = "else" without "if".

Changed to above and the error went the "if" statement. "{" expected. Now what?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Mayank SAssociate Director - Product EngineeringCommented:
It looks like you have not posted the correct code - that one looks fine. There is an extra { or } hanging somewhere in between.
0
 
jmurguiaAuthor Commented:
Is that a good ending to this file?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Looks like - can you post the updated code
0
 
sirjulioCommented:
hey jm,
Like mayan and colin, I don't see the error. The code is compiling and running fine in current state.
0
 
ashwanilabsCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
So what was the problem?
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now