Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

program does not terminate

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
LearningJava
Asked:
LearningJava
1 Solution
 
CEHJCommented:
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
 
ticoldam12Commented:
Use this line where you want your program to terminate:
System.exit(0);
0
 
ticoldam12Commented:
Im sorry....
To compare strings you have to use:
if (fileName.equals(""))
      System.exit(0);

your problems will dissapear!!!!!

0
Independent Software Vendors: 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!

 
LearningJavaAuthor Commented:
"if (fileName.equals(""))
     System.exit(0);

your problems will dissapear!!!!!"

Where would I place this code in my program?
0
 
CEHJCommented:
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
 
ticoldam12Commented:
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
 
CEHJCommented:
>>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
 
ticoldam12Commented:
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
 
functionpointerCommented:
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
 
CEHJCommented:
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
 
CEHJCommented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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