Java newbie question

Status
Not open for further replies.

simmadown

Seniorius Lurkius
34
I need to sort a String array, in alphabetical order.
It wont let me compare the Array elements in the following:
if(text[j] > text[k])

It gives me this error:ARRAY1.java:18: Incompatible type for >. Can't convert java.lang.String to int.
if(text[j] > text[k])

How do I compare each string in the array to another?
This is a beggining class, so the sequence has to be kept pretty basic, as we are not even defining classes yet.
 

Jonny K

Ars Scholae Palatinae
609
Ok, I'm a newbie too, so don't think my reply is correct...


Looks like you're comparing two different types, String and Int. Apparently, you can't do that.

As for what will work, I'm not quite sure. You may be able to "cast" on type into another (convert string to int, or int to string). I think you do that by:

string = (int)string //Convert string to int.

Also, to compare stuff, there are many built-in things, such as "compareTo()" for strings. Look at this mess (do a search) for compare, and you might find something:

http://java.sun.com/products/jdk/1.4/docs/api/overview-tree.html


Jonny K.
 

simmadown

Seniorius Lurkius
34
My only problem now, is my loop:
for(int count=1;count < text.length;count++)
{
if(text[j].compareTo(text[k]) < 0)
{
++j;
++k;
}
else
{
temp=text[j];
text[j]=text[k];
text[k]=temp;
++j;
++k;}
The problem is, the array is only gone through once, so some elements get interchanged. But I need to be able to keep going through the array until all words are in alphabetical order. I don't know what condition to set up, so that when the end of the array is reached in the for loop, it will start over again and keep rearranging. I hope this makes sense.
 
Originally posted by simmadown:
My only problem now, is my loop:
for(int count=1;count < text.length;count++)
{
if(text[j].compareTo(text[k]) < 0)
{
++j;
++k;
}
else
{
temp=text[j];
text[j]=text[k];
text[k]=temp;
++j;
++k;}
The problem is, the array is only gone through once, so some elements get interchanged. But I need to be able to keep going through the array until all words are in alphabetical order. I don't know what condition to set up, so that when the end of the array is reached in the for loop, it will start over again and keep rearranging. I hope this makes sense.

I would go with what Jayde said, but if you're going for a simpler, more primitive solution using only native String methods, then when you do this assignment: (text[j]=text[k];) you must first compare again and make sure you're not putting it in front (or below, if you think in hashtables) of a greatr or smaller value, depending on your sort order... As it stands, your code only checks to see if the current pair of Strings has one greater than the other, there's nothing to compare what its value is in relation to the whole scope of the array you're putting it into... make sense? Your code for comparing is good, but before you assign it to the target array, you have to do another conditional logic statement that figures out the values of the Strings you're putting it ahead of or behind...

Hope that made some sense... there are a million ways to do this... if you're allowed to use the Comparator stuff, go with that... it will probably confuse the hell out of your prof if he doesn't know any better than to tell you to sort Strings like ints. :)
 

KarlShea

Ars Tribunus Militum
2,277
Subscriptor++
This is only related to java newbieness (sp?), not this particular question, because it seems to have already been answered.

The most valuable thing that I have come across in trying to solve java questions is

1. Google (type in problem)
2. Java API (http://java.sun.com/j2se/1.4/docs/api/index.html for 1.4 or http://java.sun.com/j2se/1.3/docs/api/index.html for 1.3.1)

Just click on the class and see all of the methods. Especially usefull if you forget stupid things like specific names for String functions :D

I actually learned how to write a comparator (note: it's really really easy) when I needed to sort a hashtable. Found it in the API and then searched on google.
 

fxt

Ars Scholae Palatinae
604
I didn't want to start another topic for this, so I'm
abusing this thread :D .
The following code will compile without a hitch, but
appletviewer will start and display "Applet not initialized".

I don't have another computer at my disposal right
now, so could someone please compile and run this for
me?

First the applet:
code:<HR>
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Container;

public class abstand extends JApplet
                     implements ActionListener {
            JLabel a1, b1, a2, b2;
            JTextField x1, y1, x2, y2, ausgabe;
            JButton rechnen;
            
            public void init()
            {
            	Container c = getContentPane();
            	c.setLayout( new FlowLayout() );
            	
            	a1 = new JLabel( "x1:" );
            	c.add( a1 );
            	x1 = new JTextField( 10 );
            	c.add( x1 );
            	
            	b1 = new JLabel( "y1:" );
            	c.add( b1 );
            	y1 = new JTextField( 10 );
            	c.add( y1 );
            	
            	a2 = new JLabel( "x2:" );
            	c.add( a2 );
            	x2 = new JTextField( 10 );
            	c.add( x2 );
            	
            	a2 = new JLabel( "y2:" );
            	c.add( b2 );
            	y2 = new JTextField( 10 );
            	c.add( y2 );
            	
            	rechnen = new JButton( "Berechnen" );
            	rechnen.addActionListener( this );
            	c.add( rechnen );
            	
            	ausgabe = new JTextField( 25 );
            	ausgabe.setEditable( false );
            	c.add( ausgabe );
            	
            }
            
            public void actionPerformed( ActionEvent e )
            {
            	double dx1, dx2, dy1, dy2, abstand;
            	
            	dx1 = Double.parseDouble( x1.getText() );
            	dy1 = Double.parseDouble( y1.getText() );
            	dx2 = Double.parseDouble( x2.getText() );
            	dy2 = Double.parseDouble( y2.getText() );
            	
            	showStatus( "Berechnung läuft" );
            	
            	abstand = Math.sqrt ( (dx2 - dx1) * (dx2 - dx1) + (dy2 - dy1) * (dy2 - dy1) );
            	showStatus( "Fertig" );
            	
            	ausgabe.setText( Double.toString( abstand ) );
            }
}

I know there's german in there, but surely that's not the
problem...
My Java instructor wants us to use german expressions,
and,as I intend to finish this semester, I'll humour him.

Just to clear it up:
abstand == distance
rechnen == calculate
ausgabe == output
"Berechnung läuft" == "Calculating"
"Fertig" == "Ready"

The HTML-Code is as follows:
code:<HR>
Code:
 <html>
<applet code="abstand.class" width=500 height=500>
</applet>
</html>

I would appreciate it very much, if one of you
could somehow either run this, so I can kick my
computer, or tell me where I went wrong, so I can
kick myself.

Thx,

FXT
 

fxt

Ars Scholae Palatinae
604
Okay guys, forget about it, I found it.
A mistake from the copy/paste school of
Java programming:

For economical reasons, I decided to copy
this code four times, and finetune it then:

a2 = new JLabel( "y2:" );
c.add( b2 );
y2 = new JTextField( 10 );
c.add( y2 );

so c.add( b2 ); has to produce a nullpointer
exception, because of that bastard a2...

I'm happy I found it :D .
Thx for everyone who bothered to check, and
sorry for wasting your time.

FXT
 
Status
Not open for further replies.