CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2011
    Posts
    2

    java program using array's help

    What I need to do is create an array of 1001 numbers and have the user enter as many positive numbers up to 1001 numbers into the program and terminate the array with a 0 or negative number. In this example, the user entered 19 13 7 21 11 9 15 17 -1. Then I need to have the program ask the user to enter 2 of the numbers that appeared/or didn't in the list they originally entered and have the program (For example 13 and 9) and print out these three things-

    1) The 2 additional numbers appear in the original list in the same order (not necessarily consecutive).
    2)The 2 additional numbers appear in the original list, but not in the same order ( and, again, not necessarily consecutive).
    3)These 2 additional numbers do not appear together in the original list (although one of them might).

    For example to see what I am talking about:

    1) That pair (13 and 9) could be found, in that order, in the following list:
    19 13 7 21 11 9 15 17
    2) That pair (9 and 13) could be found, in that order, in the following list:
    19 13 7 21 11 9 15 17
    3) That (8 and 9) could not be found, at all, in the following list:
    19 13 7 21 11 9 15 17

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: java program using array's help

    OK. So let's see what you've got so far, and tell us exactly where you're stuck.

    Good teaching is more a giving of the right questions than a giving of the right answers...
    J. Albers
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    May 2011
    Posts
    2

    Re: java program using array's help

    import java.util.Scanner; // imports the scanner into the program
    public class Prog4
    {
    public static void main(String[] args) // starts the main method
    {
    Scanner keyboard = new Scanner(System.in); // obtains the scanner keyboard
    int i = 0; // initializes the position number
    int current; // declares the number to be entered into the program
    int[] number; // declares the array
    int size = 0; // initializes the size of the array
    number = new int[1001]; // declares a name "number" for the array
    System.out.println("Enter a series of positive " // asks to user to input numbers into program
    + "numbers\n\t(ending with a negative number): ");

    current = keyboard.nextInt(); // gets the numbers entered by the user

    while((current > 0) && (size < 1001)) // while loop that states when the number is
    { // positive and the amount of numbers is less then 1001
    number [size++] = current; // gives the number entered to the position "size" in the array
    i += 1; // increments the position in the array
    current = keyboard.nextInt(); // gets the number entered by the user and assigns it to "current"
    }

    System.out.println("Enter 2 numbers to be found\n\tin the " +
    "list you just entered: ");
    int any = keyboard.nextInt();

    System.out.println("That pair( and )")
    }
    }

  4. #4
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: java program using array's help

    magictricks1020, you've obviously put in so much effort so far, I hate to trouble you to put in even more effort, but maybe you could post the code in code tags, properly indented, and also answer the second part of dlorde's question? If it isn't too much effort that is. Of course, feel free to ignore my post like you ignored 50% of dlordes. After all, its our job to make sure you get that college degree, not yours.

  5. #5
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: java program using array's help

    I wonder if some people go to the doctor, and when asked exactly what's wrong, just say "Oh I'm ill, doctor; you tell me."

    Questions are the important thing, answers are less important. Learning to ask a good question is the heart of intelligence...
    R. Schank
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  6. #6
    Join Date
    Apr 2011
    Posts
    9

    Re: java program using array's help

    Why don't you use LinkedList?

    What if list is like this

    10 5 6 5 10

    and I enter 5 10?

    Does it look for the first apperance of a number or the last?

    LinkedList class has a useful method indexOf ( http://download.oracle.com/javase/6/...va.lang.Object) )

    Then you just add an if statment.

  7. #7
    Join Date
    Apr 2007
    Posts
    442

    Re: java program using array's help

    You code is not that complete, heres some general tips:

    Ask both numbers from the user before doing any testing. Then iterate the array looking for the first number, if it is found, the number stored in the next index has to match the second number, right? This can be made ever more simple using more sophisticated objects, such as earlier mentioned ArrayList. Look into the API of that and you will see for yourself. If that is an option for you (arrays is not specifically required) best to use those.

    Usual mistake seen here is not getting all the relevant input data before initiating the logics. So get all the input data beofrehand, and you will see that the logic part becomes much more simple and much more efficient. It is usefull, for any future purpose, to aim to design the "user interface" (or data retrieval) part separate from the logic part. In this case you would simply desing a getter for int[] array of two (the numbers to be tested) and a getter for a larger int array, against which the test is made. Test itself would be encapsulated in another separate method. This puts you in the path to consider testability, re-usability and properly segmenting your desing / implementation.

    For testing, skip the mandatory manual seeding of the array and test numbers. Create it eg. with Math.random(), or with Random class's nextInt(). This provides better base to test your logic part, because you will get wider scale of cases. If you seed them by hand, human laziness would result in you having a smaller and smaller, and even mostly the same, test case. Logic such as this should be tested against "any" eventuality. Of course you can define specified sets of test data to cover the evident cases.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured