aspose file tools*
The moose likes Java in General and the fly likes adding value to an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "adding value to an array" Watch "adding value to an array" New topic
Author

adding value to an array

Gavin Walsh
Greenhorn

Joined: Oct 19, 2005
Posts: 14
Hey all,

so i am having trouble figuring out how to add a value (that i have just prompted the user for) to an array..

___
import java.io.*;
import java.util.ArrayList;

public class prac7Rainfall1
{
public static void main(String args[]) throws IOException
{

int[] rainArray; // declare an array of integers
rainArray = new int[12];// create an array of integers
String myString;

//Reading from the keyboard
BufferedReader myInput = new BufferedReader(new InputStreamReader( System.in));

// assign a value to each array element and print
for (int i = 0; i < rainArray.length; i++) {
rainArray[i] = i;

System.out.print("Enter amount for Month 1: ");
myString = myInput.readLine (); //need to insert some code here that will allow me to add value to array


System.out.println(rainArray[i] + " ");



}

System.out.println();
}
}




thanks
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I don't know what you mean by "add a value" -- int addition, assignment, etc...
But it seems to me you have an int array and a String, so you need to
convert that String into an int. I wonder if java.lang.Integer has any
methods that might help (hint, hint)...


There is no emoticon for what I am feeling!
Gavin Walsh
Greenhorn

Joined: Oct 19, 2005
Posts: 14
Hey,

sorry,

i want to prompt the user for a value and then add that to an array and then print out all the values at the end that are in the array..

ignore the string bit because im not sure that is the best way of doing it..


thanks
gavin
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You still haven't defined "add that to an array":
1. Append a new value into an array, incrementing its size by one?
2. Take that number and add that to the value already held at a certain offset?
3. Store that number at a certain offset, overwriting what was there before?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14107
    
  16

If you mean: making the array longer by one element and assigning the entered value to the new element, it's not going to work with a regular array.

Arrays in Java have a fixed length. If you need a variable length list of numbers, you should use the collection classes, such as ArrayList (which is already imported in your program, but you're not using it yet!).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Pratik Lohia
Ranch Hand

Joined: May 05, 2005
Posts: 88
Use Integer.parseInt(myString) to convert the string to an integer which then you can assign to your array. Here however it is important that the data input by the user is an integer, otherwise your method will throw a NumberFormatException
 
Don't get me started about those stupid light bulbs.
 
subject: adding value to an array