-
November 9th, 2001, 09:49 PM
#1
Array name list not printing out
Hi, I am trying to create a program that is an array of Strings called names[] that holds 10 names, and prints out the names. I'm having trouble getting the names to print out. I thought I coded it right, but obviously not cause it compiles, but doesn't print out the names. Could someone help me figure out why? thanks
// A program that creates an array of Strings called names[] that hold 10 names.
import java.util.List;
import java.util.Iterator;
public class Names
{
private String values[] =
{ "Jim", "Helen", "Jenny", "Daniel", "Amy", "Dianne", "Chuck", "Pat", "Laura", "Shaun" };
private List list;
// An array to hold the names
static String[] names;
// The number of names currently stored in the array.
static int nameCount;
public static void main (String args[])
{
// Start with space for 10 names.
names = new String[10];
// Currently, there are no names in the array.
nameCount = 0;
try
{
List list = createList(args);
java.util.Collections.sort (list);
Iterator i = list.iterator();
while (i.hasNext()) {
String s = (String) i.next();
System.out.println (s);
}
}
catch (Exception e)
{
System.out.println ("Exception: " + e.getMessage() + "\n");
}
}
// Implementation
private static List createList (String args[])
throws Exception
{
List list;
if (args.length == 0 || args[0].equals("ArrayList"))
{
list = new java.util.ArrayList();
System.out.println ("Using ArrayList");
}
else if (args[0].equals("LinkedList"))
{
list = new java.util.LinkedList();
System.out.println ("Using LinkedList");
}
else
{
throw new Exception (
"Invalid Argument " + args[0]
+ ". Valid options are: ArrayList or LinkedList");
}
fillList (list);
return list;
}
private static void fillList (List list)
{
for (int i = 0; i < nameCount; i++)
System.out.print(names[i]);
System.out.println();
}
}
Thanks, Dianne
-
November 10th, 2001, 10:11 AM
#2
Re: Array name list not printing out
What does the program do now? It would help someone else solve your problem if you would describe in more detail what the problem is. "Having trouble" doesn't really say what the problem is. If you would Copy and paste the console output here that would help
Also what are the parms to the program? I see that you are using String[] args. What is in it? Some documentation via comments in the code is useful there.
Norm
Norm
-
November 10th, 2001, 01:22 PM
#3
Re: Array name list not printing out
The names don't print out because they are never put into the list. Your createList() method calls fillList() which, instead of putting the names in the list, tries to print out the names which aren't there yet...
Incidentally, nameCount is never explicitly initialized.
Dave
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
November 10th, 2001, 10:34 PM
#4
Re: Array name list not printing out
nameCount is initialized in the main method.
----------------
You can contact me directly at Christoph.Schulze@gmx.co.uk
Don't forget your parsley cause you can't eat your dog after having stolen him from some animal shelter and having drowned him in the Atlantic Ocean.
-
November 11th, 2001, 11:58 AM
#5
Re: Array name list not printing out
Oops! yes, you're right. The problem is that it never changes... ;-)
Dave
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
November 11th, 2001, 06:40 PM
#6
Re: Array name list not printing out
Its problem is that it needs to be set to 10.
Norm
Norm
-
November 12th, 2001, 03:10 PM
#7
Re: Array name list program is working now.
I finished my program, so I thought I would post what I did. I actually broke it into two files instead of one. The purpose was to have a program that creates an array of Strings called names[] that holds 10 names. Thanks for everyone's help.
ListNames.java
// A program that creates an array of Strings called names[] that holds 10 names.
import java.util.*;
import java.lang.*;
import java.util.List;
import java.util.Iterator;
public class ListNames
{
private String names[] =
{ "Jim", "Helen", "Jenny", "Daniel", "Amy", "Dianne", "Chuck", "Pat", "Laura", "Shaun" };
private List list;
public ListNames()
{
}
public void print()
{
//print the list
for (int i = 0; i < 10;i++)
{
System.out.println(names[i]);
}
System.out.println( "\n");
}
public void sort()
{
Arrays.sort(names);
}
public int search(String Name)
{
int isThere = 0;
isThere = Arrays.binarySearch(names, Name);
return isThere;
}
public ArrayList createNameList()
{
ArrayList nameList = new ArrayList();
boolean ok = false;
for (int i=0; i < 10; i++)
{
ok = nameList.add(names[i]);
}
if (!ok) System.out.println("nameList is not working");
//print out the list
Iterator it = nameList.iterator();
while (it.hasNext())
{
System.out.println("Names in the List: " + it.next());
}
return nameList;
}
public void createNewArray(ArrayList arraylist)
{
ArrayList al = arraylist;
Object names2[] = new Object[10];
names2 = al.toArray();
for (int i = 0; i < 10; i++)
{
System.out.println("names2: " + names2[i]);
}
}
}
TestListNames.java
// A program that tests a program that has an array of Strings called names[] that holds 10 names.
import java.util.*;
import java.lang.*;
import java.util.List;
import java.util.Iterator;
public class TestListNames {
// main for testing
public static void main(String[] args) {
//create new object
ListNames ln = new ListNames();
//print strings
ln.print();
//sort list
ln.sort();
//print again
ln.print();
//search for a name that is there and one that is not
int isThere = ln.search("Pat");
if (isThere > 0) {
System.out.println("The item Pat was found");
} else
System.out.println("The name Pat was not found");
isThere = ln.search("Bob");
if (isThere > 0) {
System.out.println("The item Bob was found");
} else
System.out.println("The name Bob was not found");
System.out.println("\n");
//create the nameList
ArrayList al = ln.createNameList();
//create a new array from the ArrayList
ln.createNewArray(al);
}
}
Thanks, Dianne
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|