Results 1 to 4 of 4
  1. #1
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Java Array Programming Problem

    Write a Java program that asks the user to store 10 numbers in an array. You should then print the array in the order entered and then print in reverse order. However, instead of putting all the information in the main, you should use a class called PrintIt. The PrintIt class should have an instance variable that is an array to hold the numbers. You should have a method that will print the array in the order entered. You will also need a method to print in reverse oder. Then create a tester class that asks the user to enter 10 numbers and puts them in an array.
    So far I've got this.
    ---------------------------------------------------------------------------------------------------


    public class printIt
    {
    int i;
    int [] numArr = new int [10];

    public printIt () {
    i = -1;
    }

    public void placeNumber (int num) {
    numArr[++i]=num;

    }

    public void printForward () {
    for (int j = 0; j <= i; j++) {
    System.out.println (numArr [j]);
    }
    }

    public void printReverse () {
    for (int j = i; j >=0; j--) {
    System.out.println (numArr [j]);
    }
    }

    }
    ------------------------------------------------------------------------------------------
    public class PrintItTester
    {
    public static void main (String [] args){
    System.out.println(" Enter a number: ");
    Scanner in = new Scanner(System.in);
    int number = in.nextInt();
    printIt myArray = new printIt();
    myArray.placeNumber(number);
    number = in.nextInt();
    myArray.placeNumber(number);
    System.out.println("Forward: ");
    myArray.printForward();
    System.out.println ("Reverse:");
    myArray.printReverse();
    System.out.println ("end");
    }
    }


    --------------------------------------------------
    Output:
    Enter a number:
    8
    34
    Forward:
    8
    34
    Reverse:
    34
    8
    end
    ----------------------------------------------------------
    what i want as an number is being able to print out 10 numbers but this only lets me do two numbers. Why is that so?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default Re: Java Array Programming Problem

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Java Array Programming Problem

    You just get 2 inputs into your array. So just get 10 inputs and store them in the array if you want them.

  4. #4
    jjfstar2009 is offline Member
    Join Date
    Apr 2014
    Posts
    21
    Rep Power
    0

    Default Re: Java Array Programming Problem

    I finally got it!

    ----------------------------------------------
    public class printIt
    {

    int i;
    public printIt()
    {
    i=-1;
    }
    int [] myArray = new int[10];
    public void printForward(int[] theArray) {
    for (int i=0; i<theArray.length; i++) {
    System.out.println(theArray[i]+" ");
    } // end for
    System.out.println();

    } // end printForward()

    public void printReverse(int[] theArray) {
    for (int i=theArray.length-1; i>=0; i--) {
    System.out.println(theArray[i]+" ");
    } // end for
    System.out.println();
    } // end printReverse()
    } // end class PrintIt

    ---------------------------------------------------------

    public class printItTester {

    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    printIt p = new printIt();
    int[] myArray = new int[10];
    for (int i=0; i<10; i++) {
    System.out.print("Enter number: ");
    myArray[i] = in.nextInt();
    }
    System.out.println("Now printing all forward numbers: ");
    p.printForward(myArray);
    System.out.println("Now printing all reverse numbers: ");
    p.printReverse(myArray);

    } // end main()

    } // end class TestForwardReverse
    --------------------------------------------------------------

Similar Threads

  1. Replies: 1
    Last Post: 04-20-2013, 09:16 PM
  2. Replies: 1
    Last Post: 04-20-2013, 09:15 PM
  3. Problem with Graphics OO programming Java.
    By Martino in forum Java 2D
    Replies: 2
    Last Post: 04-27-2011, 02:33 PM
  4. Simple Programming problem loop in array
    By katon in forum New To Java
    Replies: 1
    Last Post: 12-20-2009, 03:14 PM
  5. Java Programming, Array Help
    By Just Incredible in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 03:18 PM

Posting Permissions

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