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 7 of 7

Thread: Method not being applied

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Method not being applied

    Hello there, I'm doing an exercise we're you're supposed to sort strings in alphabetical order, without importing anything , aka, not using the Arrays.sort() method.

    I think I got the method down partially right, or it is on the right track, but it is completely not being applied to my answer. All it prints out in the console is the actual String array twice, without sorting anything. Any ideas?

    Thanks in advance.

    public class arrayofstrings {
     public static void sort(String[] a) {
     String temp= "";
      int min;
      int i= 0;
      for (int j=0; j<a.length-1; j++) { 
       min = i;
       for (i=j+1; i<a.length; i++) 
        if (a[i].compareTo(a[min]) <0) 
        min = i; 
        if(min!= j)
     
             temp = a[j];
             a[j] = a[min]; 
             a[min] = temp;
     
     }
    }
     public static void main(String[] args) {
       String[] a={"Rokas, Ausra, Tomas, Agne, Zenius"};
      for (int i=0; i<a.length; i++) System.out.print(a[i]+" ");
      System.out.print("\n");
      sort(a);
      for (int i=0; i<a.length; i++) System.out.print(a[i]+" "); 
      System.out.print("\n");
     }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Method not being applied

    On quick glance, you should first check your variable and conditional scoping (eg brackets). For instance
    if ( false ){
        System.out.println("First");
        System.out.println("Second"); 
    }//prints nothing
    Is not the same as
    if ( false )
        System.out.println("First");
    System.out.println("Second"); 
    //prints 'Second'

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    20
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Method not being applied

    Quote Originally Posted by copeg View Post
    On quick glance, you should first check your variable and conditional scoping (eg brackets). For instance
    if ( false ){
        System.out.println("First");
        System.out.println("Second"); 
    }//prints nothing
    Is not the same as
    if ( false )
        System.out.println("First");
    System.out.println("Second"); 
    //prints 'Second'
    Added brackets, to no obvious effect. I think the method works fine, it's just not being applied.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,302
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Method not being applied

    Please post updated code.

    Quote Originally Posted by import.Snupas View Post
    I think the method works fine, it's just not being applied.
    What evidence do you have to support this? Have you added println's to try and debug the code?

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Method not being applied

    Could you please post the recent code again?
    Thanks and regards,
    Sambit Swain

  6. #6
    Member
    Join Date
    Sep 2013
    Posts
    68
    My Mood
    Confused
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Re: Method not being applied

    If you try to create an array of String in you code then this
    String[] a={"Rokas, Ausra, Tomas, Agne, Zenius"};
    is wrong.

    It should be like this :
    String[] a={"Rokas", "Ausra", "Tomas", "Agne", "Zenius"};

    and your code to sort the array going to throw a ArrayIndexOutOfBounds Exception so first rid of the above and then go for that exception.

  7. #7
    Member
    Join Date
    Oct 2013
    Location
    United Kingdom
    Posts
    62
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Method not being applied

    Please could you go through your logic again and put an updated code here.
    Thanks and regards,
    Sambit Swain

Similar Threads

  1. [SOLVED] method in class cannot be applied
    By ange in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 30th, 2013, 05:03 AM
  2. ()Method cannot be applied to ()
    By CrimsonFlash in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 21st, 2011, 11:29 AM
  3. lookandfeel() cannot be applied to JFrame?
    By emigrant in forum AWT / Java Swing
    Replies: 1
    Last Post: February 15th, 2010, 11:05 AM
  4. Inputs not being applied.
    By Norflok669 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2010, 12:25 AM
  5. [SOLVED] Facing java error "cannot be applied to (java.lang.String)"
    By tazjaime in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2009, 10:19 AM