Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

an array designed and implemented by user input

Posted on 2006-06-11
7
Medium Priority
?
197 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
[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
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

618 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