The Artima Developer Community
Sponsored Link

Java Answers Forum
What'd I do wrong?

5 replies on 1 page. Most recent reply: Jul 7, 2003 2:11 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

What'd I do wrong? Posted: Apr 3, 2002 11:02 AM
Reply to this message Reply
Advertisement
Ok, I've been fooling with this a while - this program is supposed to sort an array of strings, and it works fine...as long as I've defined the size of the array beforehand, and I want it to be of a variable size, as in one time you run the program, you may enter 3 items, another time, 5. Here's my code:

import java.util.*;
import java.io.*;
import java.lang.String;
//alphabetical sort of names
public class SortNames
{
//with the array filled like this (and the rest of the program modified)
//this program works fine.
//private static String [] name = {"John","Joe","Richard","Paul"};
private static BufferedReader stdin;
SortNames()
{
}
public static void main(String[] args) throws IOException
{
//50 seems to be a nice number
String [] name1 = new String[50];
//"initialize" entered
String entered = "nothing";
stdin = new BufferedReader (new InputStreamReader(System.in));
System.out.println("Enter the names to be sorted: ");
int index = 0;
while (entered.length() != 0) //i.e. nothing on line
{
entered = stdin.readLine(); //read a string
if (entered.length() == 0) break; //if nothing is entered, get out
name1[index] = entered; //put the string into the name1 array
index++;
}
System.out.println("index= "+index); //for debugging
String [] name = new String[index]; //idea behind this: create a variable length array
Arrays.sort(name); //sort in java.util.Array
for(int j=0;j<name.length;j++)
System.out.println("name= "+name[j]);
}
}

I get a NullPointerException after entering the strings (once the program comes to the "Arrays.sort" line). I can't seem to figure out what the problem is. What did I do wrong?

Thanks!

Lynn.


steve strongheart

Posts: 12
Nickname: stronghear
Registered: Apr, 2002

Re: What'd I do wrong? Posted: Apr 4, 2002 6:26 PM
Reply to this message Reply
Lynn ~
A couple of things,

1 the String array object was initialized, true, but probrably most of the elements are null. you should use Arrays.fill() to initialize wach element to an initial value, or be sure too check for nulls, which might screw up the Arrays.sort() , so initialize to "" or a non alpha character at the end of the alphabet in ascii.

2 to overcome the array problem, usethe dynamic array of java.util.Vector class.

Vectors can be flexible allowing growth,
Vectors convert to arrays easily even allowing various types of arrays to be used.
Vectors are pre initialized

Vector V = new Vector(24,8);  // starts out size=24, grows 8 each time the load is increased
 
while(true)
{
String entry=stdin.readline();
if(entry==null||entry.length<1)break;
V.add(entry);
}
String[] names=new String[V.size]; // build the correct sized String[] ... exactly
V.toArray(names); /// fill names, casting the Objects stored in V to Strings 
names.sort();
k=0;
while (k< names.length) System.out.println(new StringBuffer(k).append("\t").append(names[k++]);

steve strongheart

Posts: 12
Nickname: stronghear
Registered: Apr, 2002

Re: What'd I do wrong? Posted: Apr 4, 2002 6:44 PM
Reply to this message Reply
Lynn ~
A couple of things,

1 the String array object was initialized, true, but probrably most of the elements are null. you should use Arrays.fill() to initialize wach element to an initial value, or be sure too check for nulls, which might screw up the Arrays.sort() , so initialize to "" or a non alpha character at the end of the alphabet in ascii.

2 to overcome the array problem, usethe dynamic array of java.util.Vector class.

Vectors can be flexible allowing growth,
Vectors convert to arrays easily even allowing various types of arrays to be used.
Vectors are pre initialized

Vector V = new Vector(24,8);  // starts out size=24, grows 8 each time the load is increased
Reader stdin = new BufferedReader (new InputStreamReader(System.in));
while(true)
{
  try
  {
    String entry=stdin.readline();
    if (entry==null||entry.length<1)break;
    V.add(entry);
  }catch(Exception XX){XX.printStackTrace();}
}
String[] names=new String[V.size]; // build the correct sized String[] ... exactly
V.toArray(names); /// fill names, casting the Objects stored in V to Strings 
names.sort();
k=0;
while (k< names.length) System.out.println(new StringBuffer(k).append("\t").append(names[k++]);

Lynn Hollerman

Posts: 67
Nickname: gmholler
Registered: Mar, 2002

Re: What'd I do wrong? Posted: Apr 5, 2002 7:06 AM
Reply to this message Reply
Thanks! I've got it now! I'm going to try re-writing the program using the Vector class - I need to learn how to use that!

Thanks again!

Lynn.

Scuba Steve

Posts: 4
Nickname: scubasteve
Registered: Jul, 2003

Re: What'd I do wrong? Posted: Jul 7, 2003 1:40 PM
Reply to this message Reply
Just an FYI...Vector is thread-safe and carries the related overhead. ArrayList also implements the List interface, but is not synchronized. Thus, you should use ArrayList instead of Vector wherever possible and only refer to the specific type (Vector or ArrayList) at creation time...never in the rest of your code. All other references should simply refer to a List collection.

cheers,
Steve

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: What'd I do wrong? Posted: Jul 7, 2003 2:11 PM
Reply to this message Reply
Hi Stephen,

I had this same belief and wrote some tests to see how Vector and ArrayList stacked up. They were very close, but to my surprise, Vector came out faster. Of course, these were pretty simple tests and completely lacking in scientific vigor, I'm sure. I wonder if there are some more rigorous benchmarks posted somewhere that would justify the ArrayList-instead-of-Vector-except-when-you-need-synchronization argument? Maybe there are other considerations like memory efficiency to consider, as well?

As far as using List references and only using the the specific object type at creation, I agree completely. You really only need to use an ArrayList reference when you need a method that ArrayList has, but List does not.

Flat View: This topic has 5 replies on 1 page
Topic: Have a problem in Modal dialog Previous Topic   Next Topic Topic: IMPORTING USER DEFINED CLASSES

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use