Re: What am I doing worng?
Hello ThunderChunkier. Welcome to the Java Programming Forums.
In future, please use the highlight tags around your code (see my signature)
What is wrong with your code? Please give us as description of your problem.
Looking at your code, I can see one problem located within your for loop. What are you attempting to do here?
Re: What am I doing worng?
Well I need these names to be printed in both ascending and descending order. First of all I'm not even sure if I'm using the right kind of loop, maybe it should be a while loop. It is giving me an incompatible types error - "found int but expected String."
Re: What am I doing worng?
Quote:
Originally Posted by ThunderChunkier
It is giving me an incompatible types error - "found int but expected String."
That is because ss[] is a String array. The clue is in the error..
x is an integer and so is 1. You canno't make Bill an integer :)
The 'for loop' is a good loop to use. I suggest making sure it doesn't try to loop more times than the size of your array.
Re: What am I doing worng?
How is it I would go about fixing this? Do I have to caste it?
Re: What am I doing worng?
What are you trying to do with ss[x] = x + 1; ?
Re: What am I doing worng?
Well I figured that I should add one every time so that it moves to the next element of the array.
Re: What am I doing worng?
But X is already being incremented each pass of the for loop.
The first time you enter the loop, ss[x] is going to reference the first index, and the second pass, ss[x] is going to reference the next position of the array and so forth.
Re: What am I doing worng?
Yes, that's exactly what I want, how do I go bout achieving that? I understand that I should take off the x + 1 and just have it incremented, but what do I do from there?
Re: What am I doing worng?
Stop trying to insert an Integer value into String Array
Only repeat the loop for the length of the Array and not more.
Print one element at a time from the Array.
Re: What am I doing worng?
You're just overthinking the whole concept, here is the solution; it compiled and ran fine:
Code Java:
import java.util.*;
import java.io.*;
public class SortStringArray
{
public static void main(String args[])
{
String ss[] = {"Bill", "Mary", "Lee", "Agnes", "Alfred", "Thomas", "Alvin", "Bernard", "Erza", "Herman"};
Arrays.sort(ss);
for(int x = 0; x<10; x++)
{
System.out.print(ss[x]);
}
}
}
I took away your increment because it was already being done in the loop's parameters. I just changed your 'test' in the increment to < as the array goes up to 9 (0-9 = 10 people)