Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Array problem

  Asked By: John    Date: Nov 28    Category: Java    Views: 671
  

Here's the problem:
I have an array. Let's just say the the array is

String[] StudName = new String[5];

I wanna set 5 value to the StudName array FROM console..

FYI, i already know how to input data from the console through BufferedReader..
Right?

What i want to ask are :
-how to SET that value into the array(FROM CONSOLE)?
-how to STORE that value in the array, so that, when I input another value from
console
the previous value won't be overrided?

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Ana Silva     Answered On: Nov 28

>What i want to ask are :
>-how to SET that value into the array(FROM CONSOLE)?
>
>
First, try to get an object of the value from the console. (you already
know how)
Then put the object into the array.

StudName[3] = stud1Object;

>-how to STORE that value in the array, so that, when I input  another value from
>console
> the previous  value won't be overrided?
>
>
Basically you put it in different spots in the array.

StudName[3] = stud2Object; // stud1 will be replaced.

 
Answer #2    Answered By: Dustin Dean     Answered On: Nov 28

Try my sample program:

import java.io.*;


class InputArray
{
public static void main (String[] args) throws Exception
{
String[] sample = new String[5];
input(sample);
display(sample);
}


public static void input  (String[] args) throws Exception
{
BufferedReader in = new BufferedReader (new InputStreamReader
(System.in));

System.out.println ("Enter 5 values of the String array: ");

int i;

for (i = 0; i < args.length; i++)
{
args[i] = in.readLine();
}
}

public static void display (String[] args)
{
System.out.println ("\nDisplaying the values: ");

int i;

for (i = 0; i < args.length; i++)
{
System.out.println (args[i]);
}
}
}

 
Didn't find what you were looking for? Find more on Array problem Or get search suggestion and latest updates.




Tagged: