• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unsorted array list

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an unsorted string array list that I must return the smallest array.

public String smallest () {
//Returns smallest element in the list
String smallest = list[0];
for (int i = 1; i < list.length; ++i) {
if (list[i] < smallest) {
smallest = list[i];
}
}
return new String (list[0]);

}
}
I get an error: operator > cannot be applied to Java.lang.String...

Thanks

Steph
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're looking for the "shortest" String, then you will want to call the length() method on your Strings and compare these int values.
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc,

Can you be more specific?

Steph
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String a = "hello";
int size = a.length();


size will be 5 for how long the "hello" String is.

you cannot compare strings with the less than or greater than sign, it is not allowed.

so if you just change one line of your code the one with the "<" to have each String call its length() method, then you are working.

Mark
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't really understand what order do you want to be sorted, but i think, if you want to compare the string, not the length of the string, you can use compareTo method.

Example:
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an unsorted string array list that I must return the smallest array.

Do you mean the smallest element in the ArrayList?



//Returns smallest element in the list

Does "smallest element" mean "shortest String"?


if (list[i] < smallest)
I get an error: operator > cannot be applied to Java.lang.String...


"list[i]" is a String. "smallest" is a String.

if (String < String)

is not valid syntax in Java. For example
if ("hello" < "goodbye")
is not valid.

The '<' operator is valid for numbers. If you are looking for the shortest String, you need to find out the length of each String in the list. The way you find the length of a String is by using the length() method of String. For example,
int helloLength = "hello".length();
The length of "hello" is 5.

If you mean something else by "smallest element", please be more specific.
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need the smallest element in the array.

The array list is:

beta
gamma
alpha
delta

The result should be alpha.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So by smallest, I presume you mean in alphabetic order? Because the smallest in length in the list you give is "beta". If that is the case, your easiest option might be to just sort the array into alphabetical order and return the first element. Or, if this isn't feasible, you could do character comparisions. There may be a method out there to compare two strings and return the one which should come first, but I dont know of any right off the top of my head without digging into the API.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ricky already gave you that method: compareTo. compareTo (and compareToIgnoreCase) compares strings lexographically - so alpha comes before beta, and abc comes before abcd.
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it to compile, but got this error.

Exception in thread "main" java.lang.NullPointerException
at UnsortedStringList.smallest(UnsortedStringList.java:107)
at TestUnsortedStringList1.main(TestUnsortedStringList1.java:23)

Code:
public String smallest ()
//Returns smallest element in the list
{
String smallest = list[0];
for (int i = 1; i < list.length; ++i) {
if(list[i].compareTo(smallest) >0) {
smallest = list[i];
}
}
return new String (list[0]);

}
}
 
Jody Brown
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like your list is empty, at least in part. Make sure you have initialised it properly with the strings you want to compare and that it doesnt contain any gaps (nulls).
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


public String smallest ()
//Returns smallest element in the list
{
String smallest = list[0];
for (int i = 1; i < list.length; ++i) {
if(list[i].compareTo(smallest) >0) {
smallest = list[i];
}
}
return new String (list[0]);

}
}



this method will return list[0], not smallest, or will it?
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does your main method look like?
 
Stephanie Dears
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe this will also help. This is my assignment:

The Unsorted List ADT (for UnsortedStringList) is to be extended with an operation, smallest, which returns a copy of the "smallest" list element. It is assumed that the operation will not be invoked if the list is empty.

Write a method to implement the operation. The method should be added to the attached file UnsortedStringList.java.

The result is to be: The smallest list element is: alpha

This is the main method:

// class TestUnsortedStringList1
// This test program tests the 'smallest' operation

public class TestUnsortedStringList1
{
public static void main(String[] args)
{
UnsortedStringList usl = new UnsortedStringList();

// insert some list elements
usl.insert("beta");
usl.insert("gamma");
usl.insert("alpha");
usl.insert("delta");

// determine the smallest element
String smallestElement = usl.smallest();
System.out.println("The smallest list element is: " + smallestElement);
}
}

Below is the entire file that goes with my code:

public class UnsortedStringList

{
protected String[] list; // Array to hold list elements
protected int numItems; // Number of elements in this list
protected int currentPos; // Current position for iteration

public UnsortedStringList(int maxItems)
// Instantiates and returns a reference to an empty list object with room for maxItems elements
{
numItems = 0;
list = new String[maxItems];
}

public UnsortedStringList()
// Instantiates and returns a reference to an empty list object with room for 100 elements.
{
numItems = 0;
list = new String[100];
}

public boolean isFull()
// Returns whether this list is full.
{
return (list.length == numItems);
}

public int lengthIs()
// Returns the number of elements in this list.
{
return numItems;
}

public boolean isThere (String item)
// Returns true if item is on this list, otherwise returns false.
{
boolean moreToSearch;
int location = 0;
boolean found = false;

moreToSearch = (location < numItems);

while (moreToSearch && !found)
{
if (item.compareTo(list[location]) == 0) // if they match
found = true;
else
{
location++;
moreToSearch = (location < numItems);
}
}

return found;
}

public void insert (String item)
// Adds a copy of item to this list.
{
list[numItems] = new String(item);
numItems++;
}

public void delete (String item)
// Deletes the element that matches item from this list.
{
int location = 0;

while (item.compareTo(list[location]) != 0)
location++;

list[location] = list[numItems - 1];
numItems--;
}

public void reset()
// Initializes current position for an iteration through this list.
{
currentPos = 0;
}

public String getNextItem ()
// Returns copy of the next element in this list.
{
String next = list[currentPos];
if (currentPos == numItems-1)
currentPos = 0;
else
currentPos++;
return new String(next);
}

public String smallest ()
//Returns smallest element in the list
{
String smallest = list[0];
for (int i = 1; i < list.length; ++i) {
if(list[i].compareTo(smallest) > 0) {
smallest = list[i];
}
}
return new String(list[0]);

}
}
[ November 12, 2005: Message edited by: Stephanie Dears ]
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The problem is that you have constructed an array of 100 elements, but you have only filled in 4 of those 100 slots with actual content. The other 96 are still null. So when you try to compare list[4], which is null, to "smallest", you get a NullPointerException. You might want to check for "null" elements before you do the compare.
[ November 12, 2005: Message edited by: Marilyn de Queiroz ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic