4 Replies Latest reply: Oct 9, 2011 1:34 PM by baftos RSS

    Multiply the integers in an array by 5

    893358
      Write static methods which take an array of integers and an integer, and multiply all the integers in
      the array by the integer argument. So, for example, if the array argument is 1 3 5 6 8
      and the integer argument is 5, the result will be the array: 5 15 25 30 40.

      I don't know how to change all the values in the array. I've tried to copy the array first, and then multiply the integers by 5 but I'm not doing it right.

      This is what I've got

      import java.util.*;
      import javax.swing.*;

      class multiplyArray

      {
           public static void main(String[] args) throws Exception
           {
           Scanner input = new Scanner(System.in);
           System.out.println("Enter some numbers (all on one line, separated by spaces):");
           String line = input.nextLine();
           String[] numbers = line.split(" +");
           int[] a = new int[numbers.length];
           for(int i=0; i<a.length; i++)
           a=Integer.parseInt(numbers[i]);

           // String Number=JOptionPane.showInputDialog("Enter a number");
           //int Numbers=Integer.parseInt(Number);
           System.out.println(copy(a));

           }

           public static int[] copy(int[] a)
           {
           int[] b = new int[a.length];
           for(int i=0; i<a.length; i++)
           
           b[i]=a[i]*5;
           return b;
           }
      }
        • 1. Re: Multiply the integers in an array by 5
          796440
          When posting code, please use code tags: https://forums.oracle.com/forums/ann.jspa?annID=1429

          As for your question, yes, you do know how to do it. You clearly know how to set a value in an array, since you're doing that, and you clearly know how to multiply numbers, as I see that in your code.

          Your problems are

          1) You're hardcoding a multiplier of 5, rather than using what the user enters.

          2) You're assuming that passing an array to println() will print out that array's contents. It doesn't. You need to either print the elements 1-by-1, or call java.util.Arrays.toString().
          • 2. Re: Multiply the integers in an array by 5
            baftos
            There are a few lines where you should get compilation errors. It seems that you don't know how to access an element at a specific index in an array. The first element (at index 0) is numbers[ 0 ], the second is numbers[ 1 ] etc.
            If i is a variable that holds the array index, like in your loops, you access the specific array element as numbers[ i ].

            Edited by: baftos on Oct 9, 2011 2:34 PM
            • 3. Re: Multiply the integers in an array by 5
              796440
              baftos wrote:
              There are a few lines where you should get compilation errors. It seems that you don't know how to access an element at a specific index in an array. The first element (at index 0) is numbers[ 0 ], the second is numbers[ 1 ] etc.
              He has that part of it. Reply and quote to see the code before the lack of code tags leads the forum software to fuglinate it. Or did I miss something?
              • 4. Re: Multiply the integers in an array by 5
                baftos
                I see it now. Nostra culpa (OP, Oracle and me).