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

Thread: Comparing Input to array

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Location
    Boston, MA
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Comparing Input to array

    Hello, I'm looking for some help on the following program.
    The program generally should follow this concept

    Create a program name Sequence.java which accepts an integer input from com-
    mand line and outputs “found” if the input is found in sequence array and “not
    found” otherwise. You need to define sequence to be of type array of int and
    statically initialize it as follows:
    int[] sequence = { 2,3,4,10,-11,25,31,-21, 14, 18, 131, 99, 721, 23, 44, 802, 820,
    -603, 102, 53, 64, -912, 1201, 3001 };

    Here is the code i wrote


    public static void main(String[] args) {



    int[] seq = {2, 3, 4, 10, -11, 25, 31, -21, 14, 18, 131, 99, 721, 23, 44, 802, 820, -603, 102, 53, 64, -912, 1201, 3001};

    int input = Integer.parseInt(args[0]);

    if (input == seq.length)
    System.out.println("Found");

    else
    System.out.println("Not Found");


    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Comparing Input to array

    First off, I really don't think that the assignment wants you to pass the arguments in through the command line arguments. I would look into the Scanner class if I were you.

    Secondly, why are you comparing the input value to the array's length? What do you expect that to accomplish?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Location
    Boston, MA
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparing Input to array

    I want to check if the input is part of the array not the length
    something that would look like this when i run different inputs


    java Sequence 4
    Found
    java Sequence -912
    Found
    java Sequence 555
    Not found

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Comparing Input to array

    Okay, I guess that's a logical first step. But why are you comparing the length?

    Hint: this looks like a job for a for loop.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    hbonh (November 28th, 2011)

  6. #5
    Junior Member
    Join Date
    Nov 2011
    Location
    Aarhus, Denmark
    Posts
    28
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    When you get the scanner working to retrieve input, you might want to read about for loops and array iteration.

    Sent from my GT-I9000 using Tapatalk

  7. The Following User Says Thank You to rolfba For This Useful Post:

    hbonh (November 28th, 2011)

  8. #6
    Junior Member
    Join Date
    Nov 2011
    Location
    Boston, MA
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Comparing Input to array

    Ok, Thank You guys... I think I got it with the "for loop"
    Last edited by hbonh; November 28th, 2011 at 10:55 AM.

Similar Threads

  1. Comparing similar elements in an array
    By FJIW in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 25th, 2011, 10:22 AM
  2. Taking Input in String Array
    By syehjafa in forum Collections and Generics
    Replies: 8
    Last Post: July 25th, 2011, 07:18 AM
  3. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 12:39 PM
  4. URGENT!!!! help with array element comparing
    By Neo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 3rd, 2011, 08:52 AM
  5. Different operation on Array
    By jempot in forum Collections and Generics
    Replies: 4
    Last Post: January 27th, 2009, 06:07 AM

Tags for this Thread