Solved

an array designed and implemented by user input

Posted on 2006-06-11
7
190 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:
mayankeagle 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:mayankeagle
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:mayankeagle
ID: 16881150
>> int[] numbers = new int[];

Leave it as int[] numbers ;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Expert Comment

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

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

Expert Comment

by:mayankeagle
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:mayankeagle
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now