?
Solved

program does not terminate

Posted on 2003-03-02
11
Medium Priority
?
237 Views
Last Modified: 2011-09-20
Hi:
   The following program loads a file into an array and then prints the array. I get no error messages but the program does not terminate after running. Can someone tell me how to correct the problem?

Thanks.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import javax.swing.JOptionPane;

 
public class TestFile
{  
   public static void main(String[] args)
   {
      String fileName = JOptionPane.showInputDialog(
       "Enter the name of the phonebook file: ");
      if (fileName == null)
       System.exit(0);
      int arraySize = 8;
      Student people [] = new Student[arraySize];
     
     
      try
      {
         FileReader fr = new FileReader(fileName);
         BufferedReader in  = new BufferedReader(fr);
         boolean more = true;
         int itemCount = 0;
         while (more && itemCount < people.length)
      {  
         String k = in.readLine();
         String v = null;
         if (k != null) v = in.readLine();
         if (v != null)
         {  
            people[itemCount] = new Student(k, v);
            itemCount++;
         }
         else more = false;
      }
         
            in.close();
             
      }
      catch (IOException exception)
      {  
         System.out.println(exception);
         System.exit(1);
      }
     
     for ( int i = 0; i < people.length; i++)
     {
        System.out.println(people[i]);
       
      }
     
      System.out.println();
     
      System.out.println(people[7]);
  }

class Student {
String firstName;
String lastName;

public Student(String first, String last) {
firstName = first;
lastName = last;

}

public String toString() {
return firstName + " " + lastName;
}
}

   

0
Comment
Question by:LearningJava
[X]
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
11 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8052434
The program won't terminate properly as you're using a JOptionPane in an otherwise windowless context. If you pass the file name as a command line argument, such as

String fileName = args[0];

you'll find the problem disappears
0
 

Expert Comment

by:ticoldam12
ID: 8052453
Use this line where you want your program to terminate:
System.exit(0);
0
 

Expert Comment

by:ticoldam12
ID: 8052465
Im sorry....
To compare strings you have to use:
if (fileName.equals(""))
      System.exit(0);

your problems will dissapear!!!!!

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:LearningJava
ID: 8052660
"if (fileName.equals(""))
     System.exit(0);

your problems will dissapear!!!!!"

Where would I place this code in my program?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8052692
The reason is as i've given, but if you put

System.exit(0);

as the last line of main, it will serve as a workaround.
0
 

Accepted Solution

by:
ticoldam12 earned 400 total points
ID: 8052700
place it here:


public class TestFile
{  
  public static void main(String[] args)
  {
     String fileName = JOptionPane.showInputDialog(
      "Enter the name of the phonebook file: ");
     if (fileName.equals(""))
      System.exit(0);
     int arraySize = 8;
     Student people [] = new Student[arraySize];



And also dont forget that for making strings comparations, you have to use "<some string>.equals(<some string>)"...

hope this helps you!
 
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8052727
>>if (fileName.equals(""))

If fileName is null, you can't call any method(s) on it and it will throw an exception if you do. Your original code

if (fileName == null)

is correct although the following is preferable:

if (fileName == null || (fileName.trim().equals("")))
0
 

Expert Comment

by:ticoldam12
ID: 8052808
Well.....when i compare two strings using "==" in an "if" statement, the code assumes it is not equal and goes to the "else"...this happened to me in a program that i had to make for a college homework, and a classmate told me: "Use equals", and after changing, it worked. i dont know if only i have the problem when i use "=="...
Well, if im wrong, i accept it, but i would like to know what is the difference between using "==" and "equals" when comparing strings...
0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8055225
Using "==" returns true if the left object arg and the right object arg are the exact same INSTANCE.

equals( obj ) returns true if the objects are equal, but they dont have to be the exact same INSTANCE of the object, they just have the same values.
At least it should. You can override equals( Object obj ) in your classes to behave however you want.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057545
LearningJava, please do not accept wrong answers - it is very misleading to the people who come afterwards to this question.

This will NOT cause the program to exit properly under normal circumstances, which presumably was the point of your question? The only way it will exit at the point is if the user clicks on OK without putting anything into the box.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8057984
ticoldam12, functionpointer's answer to your queries is of course correct, but the point that needs to be re-emphasised i think is that you cannot call an object on a method unless you are sure that its reference is not holding null. In your code which has been accepted as correct for instance:

>>
String fileName = JOptionPane.showInputDialog(
     "Enter the name of the phonebook file: ");
    if (fileName.equals(""))
     System.exit(0);
>>

if the user clicks on Cancel, the String returned by the option pane will be null. If you try and call the method equals on an object which is null, you'll get a NullPointerException thrown. Therefore you'd have to test

if (fileName != null)

first

0

Featured Post

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!

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month12 days, 1 hour left to enroll

752 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