User input to determine array size?

Handling unknown array size and user input

Page 1 of 1

3 Replies - 21376 Views - Last Post: 13 November 2008 - 08:35 AM Rate Topic: -----

#1 bitterlynew2java  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 12-November 08

User input to determine array size?

Post icon  Posted 13 November 2008 - 12:00 AM

I am trying a program that allows the user to input an array’s size, and then prompts the user for all the values to put in that array, and finally prints all the inputted values. The inputted values must be numeric.

I am stumped on taking in the input and handling an array of unknown size. Below was my attempt but honestly, I am clueless. Is there a code example somewhere of how this type of function works so that I can understand the mechanics

[import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Assn3Prog3t {
public static void main (String[] args) throws IOException{

BufferedReader userInput = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Please input array size");
String arsize = userInput.readLine();
Integer arsizeLength = arsize.length();
if (arsizeLength == 0)
{
System.out.println("empty array size entered");
return;
}
System.out.println("\n Please enter a value in the array");
ArrayList<String> myArr = new ArrayList<String>();
for (int i = 0; i < MAX; i++) {
System.out.println(" - Storing Integer(" + i + ")");
listA.add(new Integer(i));
}
myArr.add(" ");
Integer userdataIndex = nameLength % myArr.size();
System.out.println("\nYour array size is "+arsize+". \n" +
"The values entered were "
+ myArr.get(userdataIndex));

}
}]

Is This A Good Question/Topic? 0
  • +

Replies To: User input to determine array size?

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: User input to determine array size?

Posted 13 November 2008 - 12:39 AM

Please post your code examples using :code:

View Postbitterlynew2java, on 12 Nov, 2008 - 11:00 PM, said:

I am trying a program that allows the user to input an array’s size, and then prompts the user for all the values to put in that array, and finally prints all the inputted values. The inputted values must be numeric.

I am stumped on taking in the input and handling an array of unknown size. Below was my attempt but honestly, I am clueless. Is there a code example somewhere of how this type of function works so that I can understand the mechanics
[import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Assn3Prog3t {
public static void main (String[] args) throws IOException{

BufferedReader userInput = new BufferedReader
(new InputStreamReader(System.in));
System.out.println("Please input array size");
String arsize = userInput.readLine();
Integer arsizeLength = arsize.length();
if (arsizeLength == 0)
{
System.out.println("empty array size entered");
return;
}
System.out.println("\n Please enter a value in the array");
ArrayList<String> myArr = new ArrayList<String>();
for (int i = 0; i < MAX; i++) {
System.out.println(" - Storing Integer(" + i + ")");
listA.add(new Integer(i));
}
myArr.add(" ");
Integer userdataIndex = nameLength % myArr.size();
System.out.println("\nYour array size is "+arsize+". \n" +
"The values entered were "
+ myArr.get(userdataIndex));

}
}


Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: User input to determine array size?

Posted 13 November 2008 - 12:57 AM

I would look at using java.util.scanner() to get user input rather than a BufferedReader. Scanner will let you read integers directly.
# String arsize = userInput.readLine();  
# Integer arsizeLength = arsize.length();  


The length of the string arsize is a not the way to enter a number to be used as the size of another variable. To get an array size of 100 the user will have to type in 100 spaces, 0's, or whatever! :)
Scanner:
...
            Scanner kb = new Scanner(System.in);
...
            ArraySize = kb.nextInt();


Was This Post Helpful? 0
  • +
  • -

#4 BigAnt  Icon User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 101
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: User input to determine array size?

Posted 13 November 2008 - 08:35 AM

If you are going to use an array list then there is no need for the user to input the size, as the Arraylist automatically handles the resizing. If you need for the user to input the size, then when you print out the arraylist size it would be better to check if the size entered matches the actual size, if it doesn't then there is an error in the input sequence that the user entered to many or not enough values.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1