?
Solved

an array designed and implemented by user input

Posted on 2006-06-11
7
Medium Priority
?
198 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 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:
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 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