Solved

"else" statement error

Posted on 2007-03-28
9
193 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 48
ejb wildfly example 2 69
Facing this issue for maven proxy setting 2 25
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 74
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

680 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