?
Solved

user inputing an array

Posted on 2003-03-11
5
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 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