Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Storing to another class

Is it possible to, say, ask a user to input a String and this is then stored in another class definitely?

I'll make an example:

import java.io.*;
public class askUser
{
public void userInput()
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)) throws IOException
String a = in.readLine;
}
}

public class array
{
public String[] stored()
{
String b [] = new String[100];
b[0] = "A";
b[1] = "B";
for (int i = 2; i<100; i++)
b[i] = "";
return b;
}
}

What I'd like to do is that the user input from class 'askUser' is stored as b[2] in class 'array', so that when variable 'b' is returned whenever this class is compiled, b[2] remains the String input by the user, and when 'askUser' is compiled again, the next input is stored in b[3], then b[4] etc.....

I hope I've been clear enough and that I don't need to read something like Java for dummies.....!! :cool:

Thanks.
[1260 byte] By [Dreamer] at [2007-11-11 7:52:50]
# 1 Re: Storing to another class
I added a main method. And a few method and a variable.

import java.io.*;
public class askUser
{
int i=0;
public static void main(String args[])
{
askUser NewAskUser = new askUser();
NewAskUser.userInput();
}

public void userInput()
{

try
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String a = in.readLine();
i++;
array newArry = new array();
newArry.stored(a,i);
}
catch (IOException e)
{
System.out.println("Input error:\n" + e);
}


}

}

array class

public class array
{

public String[] stored(String uinput, int increment)
{
String b [] = new String[100];
b[0] = "A";
b[1] = "B";
b[increment] = uinput;
System.out.println("b" + increment + " Contains:" + b[increment]);

return b;
}
}

The array class prints out the input the user typed in. Hope that works for you, let me know if it does.
major at 2007-11-11 22:37:06 >
# 2 Re: Storing to another class
Thanks very much major, it worked fine! :WAVE:
Dreamer at 2007-11-11 22:38:11 >