当我运行此代码时,我只得到一长串名称:
Jones, Jon BS, Wells, Lisa HS, Ryes, Ric DR
但我想要的是单独打印每个
Jones, Jon BS
Wells, Lisa HS
Ryes, Ric DR
我正在使用这个程序的两个课程,主要是
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class someName
{
public static void main (String[] args)
{
....//set all my variables and prompted user for number of Inputs
{
fName = JOptionPane.showInputDialog(null,"Enter "+i+" First Name: ");
lName = JOptionPane.showInputDialog(null,"Enter "+i+" Last Name: ");
level = JOptionPane.showInputDialog(null,"Enter "+i+" Highest Level of Degree: ");
someClass t = new someClass(fName,lName,level);
listOfName[i] = t;
}
for(int i = 0; i<numPerson; i++)
{
System.out.println(Arrays.toString(listOfName));
}
}
}
当我改变
时System.out.println(Arrays.toString(listOfName));
到
System.out.println(Arrays.toString(listOfName[i]));
我收到错误说明
java:29: error: no suitable method found for toString(someClass)
非常感谢任何和所有帮助。
答案 0 :(得分:2)
改变这个:
for(int i = 0; i<numTutor; i++)
{
System.out.println(Arrays.toString(listOfTutor));
}
到此:
for(int i = 0; i<numTutor; i++)
{
System.out.println(listOfTutor[i]);
}
换句话说,不是打印所有listOfTutor
numTutor
次,而是打印出每个元素一次。
或者,更好的是,使用增强的for循环:
for(final Tutor tutor : listOfTutor)
{
System.out.println(tutor);
}
(它做同样的事情,但不需要i
和numTutor
)。
答案 1 :(得分:0)
这样做......
for(int i=0; i<listOfTutor.length; i++) {
System.out.println(listOfTutor[i]);
}
答案 2 :(得分:0)
for(int i = 0; i<numTutor; i++)
{
System.out.println(listOfTutor[i]);
}
}
答案 3 :(得分:0)
你试过吗?
System.out.println(listOfTutor[i]);
或
System.out.println(listOfTutor[i].toString());
答案 4 :(得分:0)
使用以下代码
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class Stipend
{
public static void main (String[] args)
{
String fName = "";
String lName = "";
String level= "";
String ans;
String numStud;
ans = JOptionPane.showInputDialog(null,"Enter the number of Tutor's: ");
int numTutor = Integer.parseInt(ans);
Tutor [] listOfTutor = new Tutor [numTutor];
for (int i = 0; i<numTutor; i++)
{
fName = JOptionPane.showInputDialog(null,"Enter Tutor "+i+" First Name: ");
lName = JOptionPane.showInputDialog(null,"Enter Tutor "+i+" Last Name: ");
level = JOptionPane.showInputDialog(null,"Enter Tutor "+i+" Highest Level of Degree: ");
Tutor t = new Tutor(fName,lName,level);
listOfTutor[i] = t;
}
for(int i = 0; i<numTutor; i++)
{
System.out.println(listOfTutor[i].toString());
}
}
}
import java.util.Arrays;
public class Tutor
{
String firstName;
String lastName;
String degreeLevel;
String numStudents;
String FormalName;
public Tutor(String firstName, String lastName, String degreeLevel)
{
this.firstName = firstName;
this.lastName = lastName;
this.degreeLevel = degreeLevel;
this.numStudents = numStudents;
this.FormalName = lastName +", "+ firstName +" "+ degreeLevel;
}
public String toString()
{
return(FormalName);
}
}
我希望这有帮助。
答案 5 :(得分:0)
认为这应该可以正常工作。 :) 希望它有所帮助。
for(int i=0; i<listOfTutor.length; i++){
System.out.println(listOfTutor[i].FormalName);
}