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
Solved

"else" statement error

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

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
mysql jsp example issue 32 48
Problem to Alipay 10 43
junit as external jar or library 7 22
passing enum to a method 4 20
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

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