Solved

an array designed and implemented by user input

Posted on 2006-06-11
7
193 Views
Last Modified: 2010-03-31
Write a program that allows the user to input an array's size, and then prompts the user for all the values to put in the array, and finally prints them all. Make sure that you use exception handling to get user input. You may use either the primitive data type or String arrays.

I have come up with the following source code but the program does not compile.

import java.io.*;
public class MyNumbers   {

      public static void main(String args[] )   {

     int[] numbers = new int[];
     int arraySize = 0;
     int i = 0;
     String response1, response2;
     int arrayValue = 0;
     BufferedReader reader;

     reader = new BufferedReader(new InputStreamReader(System.in));

     System.out.print("Please enter the array size: ");
      try   {
          response1 = reader.readLine();
          arraySize = Integer.parseInt(response1);
          System.out.println("The array size is: " + arraySize);
          reader.close();
          }
     catch (IOException ioe  {
           System.out.println("I/O Exception Occurred");
           }          
      try {
          do {
          System.out.print("Please enter the element value: ");
               response2 = reader.readLine();
               arrayValue = INteger.parseInt(response2);
               i++;
               reader.close();
             }
          while (i<arraySize);
         }
       catch (IOException ioe    {
            System.out.println (I/O Exception Occurred");
         }


         for(i=0; i<arraySize; i++)  {
            System.out.println(numbers[i]);
         }
      }
    }
           
0
Comment
Question by:Sarah777
  • 6
7 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 250 total points
ID: 16881139
>> public static void main(String args[] )   {

public static void main(String args[] )   throws Exception {
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16881145
It will help if you can tell what the compilation errors are and at what lines. I guess one of them would be that you did not handle the exception at:

>> reader = new BufferedReader(new InputStreamReader(System.in));

so I suggested you declare main () as throws Exception. Of course, you can also put it in a try/ catch block and catch IOException....
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16881150
>> int[] numbers = new int[];

Leave it as int[] numbers ;
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16881153
>> arrayValue = INteger.parseInt(response2);

arrayValue[i] = Integer.parseInt(response2);
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16881157
Sorry: numbers[i] = Integer.parseInt(response2);
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 16882872
--->  catch (IOException ioe  
one bracket was missing there
       catch (IOException ioe   )

Now it will compime fine, but u will get a runtime error,

--> you are closing the reader and trying to read it the next time
--> numbers array size is not initialized after you get the value

all others are said by mayank,


     reader = new BufferedReader(new InputStreamReader(System.in));

     System.out.print("Please enter the array size: ");
      try   {
          response1 = reader.readLine();
         
          arraySize = Integer.parseInt(response1);
         
          System.out.println("The array size is: " + arraySize);

          numbers = new int[arraySize ] ; // initialize the array

          //reader.close(); this should not be there
          }
     catch (IOException ioe ) {
           System.out.println("I/O Exception Occurred");
           }          
      try {
          do {
          System.out.print("Please enter the element value: ");
               response2 = reader.readLine();
               numbers[i] = Integer.parseInt(response2); // told by mayank
               i++;
               //reader.close();
             }
          while (i<arraySize);
         }
      }
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16883088
>> numbers array size is not initialized after you get the value

Yes, I missed mentioning after this one:

>> >> int[] numbers = new int[];
>> Leave it as int[] numbers ;

After reading arraySize, make it: numbers = new int[arraySize];
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

789 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