在对象数组上使用toString [英] Use toString on array of objects
问题描述
import java.io.*;
class Main0
{
public static void main(String args[])
{
String onoma, epitheto, key01;
short AriMit, EtosEis, AM, key02;
int i, pos;
Foititis pinakas[] = new Foititis(3);
for (i = 0; i < pinakas.length; i++)
{
System.out.println("Ola ta stoixeia na einai taksinomimena symfwna me ton Arithmo Mitroou." + "\n" + "Dwste Onoma.");
do
{
onoma = FUserInput.getString();
if (onoma == "x")
System.out.println("Mh egkyrh timh. Epanalagete");
} while (onoma == "x");
System.out.println("Dwste Epitheto.");
do
{
epitheto = FUserInput.getString();
if (epitheto == "x")
System.out.println("Mh egkyrh timh. Epanalagete");
} while (epitheto == "x");
System.out.println("Dwste Arithmo Mitrwou.");
do
{
AriMit = FUserInput.getshort();
if (AriMit == -1)
System.out.println("Mh egkyrh timh. Epanalagete");
} while (AriMit == -1);
System.out.println("Dwste Etos Eisagwghs.");
do
{
EtosEis = FUserInput.getshort();
if (EtosEis == -1)
System.out.println("Mh egkyrh timh. Epanalagete");
} while (EtosEis == -1);
pinakas[i] = new Foititis(onoma, epitheto, AriMit, EtosEis);
}
pos = 1;
System.out.println(Foititis.toString(pos, pinakas));
}
}
class Foititis
{
private String onoma, epitheto;
private short AriMit, EtosEis;
public Foititis (String on, String ep, short AM, short EE)
{
onoma = on;
epitheto = ep;
AriMit = AM;
EtosEis = EE;
}
public String getEpwnymo() //alles klaseis
{
return epitheto;
}
public String toString(int j, Foititis b[])
{
String emf;
emf = b[j].onoma;
}
}
class FUserInput
/*dedomenwn kateli3a na xrisimopoihsw thn "FUserInput" gia ola ta dedomena, anti na kanw "catch" to "exception" gia kathe scanner ksexwrista, kathws eixa skopo na xrisimopoihsw mono scanner)*/
{
static String getString()
{
String line;
InputStreamReader eisodosDouble = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(eisodosDouble);
try
{
line = br.readLine();
return line;
}
catch(Exception e)
{
return "x";
}
}
static short getshort()
{
String line;
InputStreamReader eisodosDouble = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(eisodosDouble);
try
{
line = br.readLine();
short i = Short.parseShort(line);
return i;
}
catch(Exception e)
{
return -1;
}
}
}
任何东西,我不是在问我的字符串方程式,而是在问我想使用的toString方法!!!!
This is not a duplicate of anything, I am not asking about my string equations but about the toString method that I want to use!!!!
所以我有这段代码...是创建对象类型为 Foititis
的对象数组,命名为 pinakas
,并具有四个属性。如果查看名为 Foititis
的类,您会注意到 toString
方法,我要做的就是使用 toString
方法以在屏幕上显示某些内容(在此示例中,它简化为数组对象的 onoma
属性) 。请注意,我还希望发送要显示的数组的特定位置(pos-> j),在此示例中,该位置是固定的(设置为1),但在实际代码中不会。
So I have this code... all it does is to create an array of objects of type Foititis
, named pinakas
and having the four attributes. If you look at class named Foititis
you will notice a toString
method, all I want to do is to use the toString
method to show something on the screen (at this example it is simplified to the onoma
attribute of the array-object). Note that I also want to send the certain position of the array that I want to be seen(pos -> j), at this example the position is fixed (set to 1) but in the actual code it won't.
我尝试编译它,但遇到这两个错误,第二个错误可能是合理的,因为我没有以正确的方式编写代码,但是第一个呢?我又做了同样的事情,没有任何问题(创建对象数组)。
I try to compile it and I get these 2 errors, the second one is probably justified because I haven't written the code in the proper way, but what about the first? I have done the same thing again and I had no problem (at creating an array of objects).
推荐答案
您正在尝试声明一个数组,因此应该是:
You are trying to declare an array, so it should be:
Foititis pinakas[] = new Foititis[3];
这篇关于在对象数组上使用toString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!