Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: What am I doing worng?

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What am I doing worng?

    I am supposed to get these names, sort them, then using a for loop I am supposed to print them out like so:

    Ascend: Descend:
    Agnes Thomas
    Alfred Mary
    ... ...

    Here's my code:

    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++)
            {
                ss[x] = x+1;
                System.out.print(ss);
            }
        }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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."

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default 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.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What am I doing worng?

    How is it I would go about fixing this? Do I have to caste it?

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: What am I doing worng?

    What are you trying to do with ss[x] = x + 1; ?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

  10. #10
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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.
    Last edited by newbie; March 2nd, 2011 at 03:35 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  11. #11
    Junior Member
    Join Date
    Mar 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What am I doing worng?

    You're just overthinking the whole concept, here is the solution; it compiled and ran fine:

    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)
    Last edited by W00tbeer1; March 3rd, 2011 at 12:50 AM.