• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

adding value to an array

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)...
 
Gavin Walsh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!).
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic