Help with a java program (arrays)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • falconsx23
    New Member
    • Nov 2008
    • 55

    Help with a java program (arrays)

    Hi, I need help with my java program. I am new with java, so go easy on me if this is a dumb question.

    Ok the question is

    "You will complete two methods for this exercise. The headers (and a comment that describes each one) are below: Create a main method that asks the user for the length of the array, uses readArray to read in the array, and uses printArray to print the resulting array."

    [PHP]public static void readArray(int a[], int length)
    //This method reads "length" values into array a

    public static void printArray(int a[], int length)
    //This method prints the values from array a[/PHP]

    The program that I am working with is:

    [PHP]// Example9.java
    import java.util.Scann er;
    public class Example9{
    public static void main(String[] args){
    Scanner sc = new Scanner(System. in);
    final int MAX = 5;
    int[] a = new int[MAX];
    int i = 0;
    while (i < MAX){
    System.out.prin t("Enter a number: ");
    int n = sc.nextInt();
    a[i] = n;
    ++i;
    }
    System.out.prin tln("The data are:");
    i = 0;
    while (i < MAX){
    System.out.prin tln(a[i]);
    ++i;
    }
    }

    }[/PHP]
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    OK, first of all: Welcome to bytes.com!

    It's great to have you here!

    When you post, please always keep to the Posting Guidelines and when you post code, please post it in [code] ... [/code] tags (as you seemingly have done here :-) ).

    OK, about your task: Do you understand, what the program you have does? For example, what does [code=java]final int MAX = 5;
    int[] a = new int[MAX];[/code] do? That's code you'll have to change!
    Next, you have a while loop: [code=java]while (i < MAX){
    System.out.prin t("Enter a number: ");
    int n = sc.nextInt();
    a[i] = n;
    ++i;
    }[/code] This will have to be changed to - but do you understand, what it does now?
    Last but not least, there's [code=java]while (i < MAX){
    System.out.prin tln(a[i]);
    ++i;
    }[/code] What does that take care of at the moment?

    Oh, I assume you got those fuction headers from your teacher? I don't know if he realises, but this is Java, not C++, so arrays like a have a method a.length(), which gives you the length of the array.

    Otherwise, I'll just wish you the best and hope you enjoy being part of bytes.com!

    Greetings,
    Nepomuk

    Comment

    • falconsx23
      New Member
      • Nov 2008
      • 55

      #3
      [PHP]final int MAX = 5;
      int[] a = new int[MAX]; [/PHP]

      Thanks for the nice welcome

      Ok I understand that I can enter a number a total of 5 times into the java program. The other two you posted I sorta got a good idea of what it means. The first while loop means that I enter a number as the input. That number I enter is "i". "i" is a part of the array. While it goes through the loop, it will be incremented.

      The second while loop means while "i" is less than the max (which is 5) then it will keep outputting.

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Originally posted by falconsx23
        Ok I understand that I can enter a number a total of 5 times into the java program.
        Correct. Now, you want to be able to choose, how many you enter, right? So, you'll have to change something there.
        Originally posted by falconsx23
        The first while loop means that I enter a number as the input. That number I enter is "i". "i" is a part of the array. While it goes through the loop, it will be incremented.
        I'm not quite sure - you may mean the right thing, but you may not, so I'll explain it here:
        You have an array which has the length 5. "i" is a counter (or index), which is set to 0 at first. It is, as you said correctly, incremented, but before that, it is used in the array.
        Now how is it used in the array? An array has a certain number of values it can save, and each of these values has an index number. So, if a is an array, then a[0] is the first element of a. So, i goes through the possible numbers for elements in a, so that you access a different one each time.
        Originally posted by falconsx23
        The second while loop means while "i" is less than the max (which is 5) then it will keep outputting.
        Correct, and "i" is incremented again. (By the way, there's a special type of loop called the for loop for that sort of task, but that's probably still going to be addressed in your course.)

        OK, what else do you have to know? That Scanner called sc can, as you see in the first loop, read input from the user. Now, is there anywhere you would like to read user input, but don't so far?

        Greetings,
        Nepomuk

        Comment

        • falconsx23
          New Member
          • Nov 2008
          • 55

          #5
          Sorry, I don't know what you mean by "Now, is there anywhere you would like to read user input, but don't so far?"

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            Originally posted by falconsx23
            Create a main method that asks the user for the length of the array...
            Greetings,
            Nepomuk

            Comment

            • falconsx23
              New Member
              • Nov 2008
              • 55

              #7
              Ok, how bout this.

              [PHP]System.out.prin t ("Enter length for the array: ");[/PHP]

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                Good start, but that's just output. How do you get the answer from the user and use it?

                Greetings,
                Nepomuk

                Comment

                • falconsx23
                  New Member
                  • Nov 2008
                  • 55

                  #9
                  Originally posted by Nepomuk
                  Good start, but that's just output. How do you get the answer from the user and use it?

                  Greetings,
                  Nepomuk
                  How about

                  [PHP]
                  System.out.prin t ("Enter length for the array: " );
                  int[] a = sc.nextInt(); [/PHP]

                  Comment

                  • Nepomuk
                    Recognized Expert Specialist
                    • Aug 2007
                    • 3111

                    #10
                    Originally posted by falconsx23
                    How about

                    [PHP]
                    System.out.prin t ("Enter length for the array: " );
                    int[] a = sc.nextInt(); [/PHP]
                    Good try, but not quite right. Why not?
                    First of all, you want to use a somewhere else, so choose another name. Maybe n? Or length? Or iCantThinkOfAny thingThatMakesS enseSoIllJustGo ForThis?
                    The other error is, that you define your variable as a int[] here - an array of integers. But you don't want an array of integers, you only want one. Can you figure out how to correct your code?

                    Greetings,
                    Nepomuk

                    Comment

                    • falconsx23
                      New Member
                      • Nov 2008
                      • 55

                      #11
                      ok, then how about this.

                      [PHP]System.out.prin t ("Enter length for the array: " );
                      int n = sc.nextInt(); [/PHP]

                      Comment

                      • Nepomuk
                        Recognized Expert Specialist
                        • Aug 2007
                        • 3111

                        #12
                        Great, that will do the job! Now, what does your code look like, when you use that? Remember, you will want to use the n in some places...

                        Greetings,
                        Nepomuk

                        PS.: You can use [CODE] instead of [PHP] for posting your code - it looks the same at the moment, but it will change at some point. (At least, hopefully.)

                        Comment

                        • falconsx23
                          New Member
                          • Nov 2008
                          • 55

                          #13
                          ok, I think this is correct


                          [PHP]// Example9.java
                          import java.util.Scann er;
                          public class Example9{
                          public static void main(String[] args){
                          Scanner sc = new Scanner(System. in);
                          final int MAX = 5;
                          int[] a = new int[MAX];
                          int i = 0;
                          System.out.prin t ("Enter length for the array:");
                          int n = sc.nextInt();

                          while (i < MAX){
                          System.out.prin t("Enter a number: ");

                          a[i] = n;
                          ++i;

                          }
                          System.out.prin tln("The data are:");
                          i = 0;
                          while (i < MAX){
                          System.out.prin tln(a[i]);
                          ++i;
                          }
                          }

                          }[/PHP]

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by falconsx23
                            ok, I think this is correct
                            No it isn't; what if the user types '7' because she wants the program to process
                            an array with seven elements; your program just processes MAX == 5 elements,
                            no matter what was typed.

                            kind regards,

                            Jos

                            Comment

                            Working...