Solved

an array designed and implemented by user input

Posted on 2006-06-11
7
192 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
backtracking recursion  code 19 57
what is a "java.lang.System Property"   ? 20 65
servlet example issue 6 38
type mismatch (Object[] to double[] 4 23
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

816 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

8 Experts available now in Live!

Get 1:1 Help Now