Solved

"else" statement error

Posted on 2007-03-28
9
189 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:mayankeagle
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

Expert Comment

by:mayankeagle
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:mayankeagle
ID: 18827712
So what was the problem?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 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

23 Experts available now in Live!

Get 1:1 Help Now