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

Thread: Problem with LinearArray

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with LinearArray

    Welcome, people

    I have a problem connected with LinearArray. I can search elements with this method but I have a problem with repeated elements. For example, I have elements 1 2 3 1. When I make my method it shows: " Your number is in position 0"? I want to do it with all positions

    --- Update ---

    I mean : " Number 1 is in position 0 , 3.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Problem with LinearArray

    Post your code.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Problem with LinearArray

    Sounds like your code is stopping as soon as it finds a match. You need to keep searching the entire array.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Sep 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with LinearArray

    public class LinearArray {

    private int data[];
    private static Random generator = new Random();

    public LinearArray(int size) {
    data = new int[size];
    for (int i = 0; i < size; i++) {
    data[i] = 10 + generator.nextInt(91);
    }
    }

    public int lineararray(int searchKey) {
    for (int index = 0; index < data.length; index++) {
    if (data[index] == searchKey) {
    return index;
    }
    }
    return -1;
    }

    public String toString() {
    String temporary = "";
    for (int element : data) {
    temporary += element + " ";
    }
    temporary += " ";
    return temporary;
    }
    }

    --- Update ---

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int searchInt;
    int position;

    LinearArray line = new LinearArray(10);
    System.out.println(line);

    System.out.print("Enter number(-1 to quit): ");
    searchInt = input.nextInt();

    while (searchInt != -1) {
    position = line.lineararray(searchInt);
    if (position == -1) {
    System.out.println("The integer " + searchInt
    + " was not found.\n");
    } else {
    System.out.println("The integer " + searchInt
    + " was found in position " + position + ".\n");
    }
    System.out.print("Enter number(-1 to quit)");
    searchInt = input.nextInt();
    }
    }
    }

Similar Threads

  1. Problem with Project Euler problem 18
    By sara_magdy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 19th, 2013, 12:46 PM
  2. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  3. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  4. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM