Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

user inputing an array

Posted on 2003-03-11
5
Medium Priority
?
235 Views
Last Modified: 2010-03-31
I can't figure out how to allow a user to input an array's size, and then prompts the users for all the values to put in that array, and finally prints them all.  I need to be able to use exception handling to get a valid user input for some reason.
0
Comment
Question by:zonkcar
4 Comments
 
LVL 3

Accepted Solution

by:
allahabad earned 152 total points
ID: 8116689
import java.io.*;
public class PrintArray{


   public static void main(String args[]) throws IOException {

      BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter ArraySize :");
      String y = stdin.readLine();
      int size = Integer.parseInt(y);
      String[] printAble = new String[size];
      for (int i=0;i<size;i++){
         System.out.print("Enter " + i + " value:");
         printAble[i] = stdin.readLine();

      }
      for (int i=0; i<size;i++){

         System.out.println(printAble[i]);

      }
  }
}
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 148 total points
ID: 8118209
// assuming that its an integer-array

BufferedReader br ;
int size, i, array[] ;

try
{
  br = new BufferedReader ( new InputStreamReader ( System.in ) ) ;
  System.out.println ( "\n Enter size: " ) ;
  size = Integer.parseInt ( br.readLine () ) ;
  System.out.println ( "\n Enter values: " ) ;
  array = new int[size] ;

  for ( i = 0 ; i < size ; i ++ )
    array[i] = Integer.parseInt ( br.readLine () ) ; // end for

  System.out.println ( "\n The values are: " ) ;

  for ( i = 0 ; i < size ; i ++ )
    System.out.println ( array[i] ) ; // end for

} // end of try block

catch ( IOException ioe )
{
  System.out.println ( "\n Exception: " + ioe ) ;

} // end of catch block

catch ( NumberFormatException nfe )
{
  System.out.println ( "\n Exception: " + nfe ) ;

} // end of catch block


I think that's enough as per what you wanted.

Mayank.
0
 

Expert Comment

by:CleanupPing
ID: 9058760
zonkcar:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 35

Expert Comment

by:girionis
ID: 9714289
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Split points between allahabad and mayankeagle

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month10 days, 5 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question