0

当我运行这段代码时,我只会得到一长串这样的名字:

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)

非常感谢任何和所有帮助。

4

6 回答 6

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);
    }

(它做同样的事情,但不需要iand numTutor)。

于 2013-03-21T20:36:01.280 回答
0

这样做...

for(int i=0; i<listOfTutor.length; i++) {
    System.out.println(listOfTutor[i]);
}
于 2013-03-21T20:36:10.200 回答
0
 for(int i = 0; i<numTutor; i++)
        {
        System.out.println(listOfTutor[i]);
        }
    }     
于 2013-03-21T20:36:48.947 回答
0

你试过吗?

System.out.println(listOfTutor[i]);

或者

System.out.println(listOfTutor[i].toString());
于 2013-03-21T20:38:33.573 回答
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);
    }   
}

我希望这有帮助。

于 2013-03-21T20:40:02.323 回答
0

认为这应该可以正常工作。:) 希望能帮助到你。

for(int i=0; i<listOfTutor.length; i++){ 
   System.out.println(listOfTutor[i].FormalName);
}
于 2013-03-21T20:47:01.813 回答