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

Thread: Throwing arrays as objects

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Throwing arrays as objects

    Hey everyone, new here. I'm a beginner in Java really (started about a month ago)

    Need help with this program I'm trying to do...

    Basically, it needs to ask me for the size of the array.

    So let's say I input '4'

    it will ask me 'Enter name' and then 'Enter age' 4 times. Once you've finished inputting data, it will print it out in different lines (since they are arrays after all). I'm using BufferedReader and InputStreamReader as well as functions to print.

    I'm really confused with the logic...

    here's the code so far:

    import java.io.*;
    public class ObjFunc {
     
    	int size=3;
     
    	InputStreamReader in=new InputStreamReader(System.in);
        BufferedReader strbuf=new BufferedReader(in);
     
         void printArr(String strArr[])
         {
         	for(int x=0;x<strArr.length;x++)
         	{
         	System.out.println(strArr);
         	}
         }   
     
        String [] populateArr(String myArray[])
        {
        	myArray[0]="one";
        	myArray[1]="two";
        	myArray[2]="three";
        	return myArray;
        }
     
     
    	int getSizeArray() throws IOException
    	{
    	 	System.out.println("Enter size of the array");
            int size=Integer.parseInt(strbuf.readLine());
            System.out.println("The size of the array is: "+size);
            return size;
    	}
        public static void main(String[] args) throws IOException {
     
            ObjFunc Of=new ObjFunc();
            Of.getSizeArray();
     
            System.out.println("Enter name");
            String name=Of.strbuf.readLine();
            System.out.println("Name is: "+name);
     
            System.out.println("Enter " + name + "'s age");
            int age=Integer.parseInt(Of.strbuf.readLine());
            System.out.println("Age is: "+age);
     
     
            String nameArr[];
            nameArr=new String [Of.size];
     
          String newArray[]=Of.populateArr(nameArr);
          Of.printArr(newArray);
        }
    }

    I basically jsut need guidelines on what to do next...

    thanks


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Throwing arrays as objects

    For beginning programmers (and plain lazy programmers like me), I'd recommend using the Scanner class for user input.

    FYI: you can't throw arrays as objects, but via polymorphism you can view arrays as objects, and they can be passed to methods by reference.

    public static void main(String[] args)
    {
         // read from the console
         Scanner reader = new Scanner(System.in);
     
         // get size of array from the user
         System.out.println("How many do you want to read in?");
         int num = reader.nextInt();
     
         // create the array
         Object[] array = new Object[num];
         for (int i = 0; i < array.length; i++)
         {
              // read in anything you want for your array here
              System.out.println("Input an integer: ");
              array[i] = new Integer(reader.nextInt());
         }
    }

Similar Threads

  1. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM
  2. Java program to encrypt an image using crypto package
    By vikas in forum Java Networking
    Replies: 1
    Last Post: July 7th, 2009, 11:00 AM
  3. Replies: 6
    Last Post: May 15th, 2009, 05:06 PM
  4. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM
  5. Java error in using Arrays
    By AnithaBabu1 in forum Collections and Generics
    Replies: 4
    Last Post: November 4th, 2008, 07:50 AM