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: Won't display its Location for Linear Searching.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Won't display its Location for Linear Searching.

    So Im trying to display the location for the odd number which is 31. But it won't display the location of it.
    as you see in the code. Im using bluej there's no error in compiling it's just i feel there's something missing with the location. I can't pin point what is it..

    import java.util.*;
    import java.io.*;
    import java.util.Arrays;
    import org.apache.commons.lang.ArrayUtils;
     
    public class NumberTestingWithSearching
    {
        public static void main(String[]args)
        {
          int[] Numbers = new int[] {10,41,21,24,34,15,40,12,32,23,13,25,30,31,22,33,14,35,20,11};
          List<Integer> oddarr = new ArrayList<Integer> ();
          List<Integer>evenarr = new ArrayList<Integer>();
     
          int[] OddSearch = {31};
          int EvenSearch = 32;
     
          //organizing Even and Odd Numbers
           System.out.printf("Numbers: ");
           for (int a: Numbers){
     
             if ((a & 1 ) == 1){
                 oddarr.add(a);
               }
             else {
                 evenarr.add(a);
     
            }
            System.out.printf(" "+a);
                 }   
          System.out.println();
          System.out.println("Odd Numbers : " +oddarr);
          System.out.printf("Even Numbers : " +evenarr+"\n\n");
     
          //Converting Thing
          Integer[] OddNumsearch = oddarr.toArray(new Integer[oddarr.size()]);
          int[] OddArrInt = ArrayUtils.toPrimitive(OddNumsearch);
          System.out.println(Arrays.toString(OddArrInt));
          System.out.println();
     
          //Linear Search Testing
          for (int b = 0; b < OddArrInt.length;b++){
     
            if (OddArrInt==(OddSearch)){
               System.out.println("Linear Search Location for Number 31 :"+OddArrInt);
                    }
     
          }
     
          //Sorting Even numbers properly
          Collections.sort(evenarr);
          System.out.println("Sorted Even Numbers: "+evenarr);
     
     
        }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Won't display its Location for Linear Searching.

    What's the purpose of this line:

    int[] OddSearch = {31};

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Won't display its Location for Linear Searching.

    Compatibility with the converted int since int[] and int is not comparable types so i made it an array for it to be equal
    and the number that its trying to search which is 31.
    if (OddArrInt==(OddSearch)){
               System.out.println("Linear Search Location for Number 31 :"+OddArrInt);
                    }

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Won't display its Location for Linear Searching.

    Please follow Java's naming conventions: variable and method names should begin with lowercase letters. Class names begin with uppercase letters. Not following these conventions makes reading your code difficult.

    Why don't you just search the array Numbers (should be named 'numbers') for the int value 31 as in:

    if ( numbers[i] == 31 ) ?

    I don't think this condition:

    if (OddArrInt==(OddSearch))

    will ever be true.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Won't display its Location for Linear Searching.

    because the numbers are being separated with 2 var which is the odd array and even array, to separate the odd and even numbers and I'm trying to search the numbers in the odd array specifically.
    I research online that i'd need to convert the arraylist into primitive int so the program could search the number 31 and display its location. So i sorta made the odd number search into an array for compatibility.

    That's what im trying to solve and i have a feeling something's missing.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Won't display its Location for Linear Searching.

    You have one int[] Array and 2 ArrayList<Integer>s. All 3 of those collections can be iterated and searched for a desired value to return the index of that value with few significant differences in technique. In fact, if you're familiar with the enhanced for loop, the mechanics of iterating all 3 collections would be virtually the same. ArrayList even has a method indexOf() that further simplifies the task. If you can't use the enhanced for loop or indexOf(), then simple modifications to a regular for loop would accomplish the same iterations.

    What you're doing - even though I don't understand it - is adding unnecessary complication and, as you've already discovered, doesn't provide the desired results.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Won't display its Location for Linear Searching.

    is there an example of using them the index i mean? Because before I used arraylist i actually use the normal int odd = new int[]; but didn't like putting limit numbers so i switched to arraylist.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Won't display its Location for Linear Searching.

    I'm not sure what you're asking, and I don't know what "didn't like putting limit numbers" means. Ask a specific question: "How do I iterate an ArrayList and find the desired index?" might be what you mean, but I'm not sure, and I don't like guessing.

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Won't display its Location for Linear Searching.

    yea the "How do I iterate an ArrayList and find the desired index?"

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Won't display its Location for Linear Searching.

    Here are examples:
    import java.util.*;
     
    // demonstrates finding values in an ArrayList using indexOf() and iteration
    public class TestClass3
    {
        public static void main( String[]args )
        {
            // your array of numbers and a single ArrayList of Integers
            int[] numbers = new int[] {10, 41, 21, 24, 34, 15, 40, 12, 32, 23, 13, 25, 30, 31, 22, 33, 14, 35, 20, 11};
            List<Integer> arrayListOfNumbers = new ArrayList<Integer>();
     
            // add each primitive to the ArrayList
            for ( int integer : numbers )
            {
                arrayListOfNumbers.add( integer );
            }
     
            // finding the index of 31 without iteration
            int indexOfTarget = arrayListOfNumbers.indexOf( 31 );
     
            System.out.println( "Index of 31 in array list without iteration = " +
                indexOfTarget );
     
            int i = 0;
     
            // finding the first index of 31 in the ArrayList using iteration
            for ( Integer integer : arrayListOfNumbers )
            {
                // try this without the intValue() method )
                if ( integer.intValue() == 22 )
                {
                    indexOfTarget = i;
                    break;
                }
     
                // increment the index (or a regular for loop could be used)
                i++;
            }
     
            System.out.println( "Index of 22 in array list with iteration = " +
                indexOfTarget );
     
        } // end method main()
     
    } // end class TestClass3

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    Evilreaper (November 24th, 2013)

  12. #11
    Junior Member
    Join Date
    Dec 2012
    Posts
    17
    My Mood
    Depressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Won't display its Location for Linear Searching.

    Thanks I'll give it a try.

Similar Threads

  1. [SOLVED] It won't display the invalid selection part
    By Evilreaper in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 29th, 2013, 01:15 PM
  2. Linear Search / Index Location of Strings
    By xJavaLover in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 07:11 AM
  3. Why won't this image display?
    By mkrage in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 3rd, 2012, 09:05 PM
  4. Won't display calculated fahrenheit and celsius when running.
    By smithmar in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 17th, 2012, 09:00 PM
  5. String won't display in my window
    By JamEngulfer221 in forum AWT / Java Swing
    Replies: 6
    Last Post: November 24th, 2011, 04:23 PM

Tags for this Thread