String JAVA-无法解决无效/字符串问题

String JAVA-无法解决无效/字符串问题,string,return,void,String,Return,Void,谢谢你关注我的问题和你的建议。我正在使用Getter Setter编写这个简单的程序,我的意图是当用户类型结束时,它将停止并打印输入 问题:当我运行这个程序时,它两次询问学生姓名,并接受最后一次输入。范例 输入(sp)名称: nn 输入(sp)名称: 纳米 输入(sp)ID: 11 输入(sp)名称: 结束 {nm=11} 我的想法是:我假设这部分代码导致了这种情况 公共字符串takeSPNAME(){ 但是,如果使用VOID而不是String,则无法使用.match选项[while(!(thi

谢谢你关注我的问题和你的建议。我正在使用Getter Setter编写这个简单的程序,我的意图是当用户类型结束时,它将停止并打印输入

问题:当我运行这个程序时,它两次询问学生姓名,并接受最后一次输入。范例

输入(sp)名称: nn 输入(sp)名称: 纳米 输入(sp)ID: 11 输入(sp)名称: 结束 {nm=11}

我的想法是:我假设这部分代码导致了这种情况

公共字符串takeSPNAME(){

但是,如果使用VOID而不是String,则无法使用.match选项[while(!(this.takeSPNAME().matches(“END”))]VOID是基本类型

有人能告诉我能做什么吗

代码:

导入java.util.*; 公共班级学生简介{

public String spName;
public int spID;

public HashMap<String, Integer> hlist= new HashMap<String, Integer>();

public String getSpName() {
    return spName;
}

public String setSpName(String spName) {
    return this.spName=spName ;
}
public int getSpID() {
    return spID;
}
public void setSpID(int spID) {
    this.spID = spID;
}

public void takeSPNAME(){

    Scanner userInput = new Scanner(System.in); 
    System.out.println("Enter (sp)a name: ");
      this.setSpName(userInput.next());
}

public void takeSPID(){
    Scanner userInput = new Scanner(System.in); 
    System.out.println("Enter (sp)ID: ");
    this.setSpID(userInput.nextInt());

}

public void printINFO(){

    while(!(this.takeSPNAME().matches("END"))){

        this.takeSPNAME();
        this.takeSPID();
        hlist.put(spName, spID);
    }
    System.out.println(hlist);
}
public String spName;
public int spID;


public HashMap<String, Integer> hList= new HashMap<String, Integer>();

public String takespName(){
    Scanner userInput = new Scanner(System.in);
    System.out.println("Enter (sp) student name: ");
    return spName= userInput.next();

}
public void printSPinfo1(){

    String myname= this.takespName();


    if((spName).matches("END")){

        System.out.println("EXIT");
    }
    while(!(spName).matches("END")){

        Scanner userInput = new Scanner(System.in);
        System.out.println("Enter (sp) student id: ");
        spID=userInput.nextInt();
        this.takespName();
        hList.put(spName, spID);
    }
    System.out.println(hList);
}
公共字符串spName;
公共int spID;
public HashMap hlist=new HashMap();
公共字符串getSpName(){
返回spName;
}
公共字符串setSpName(字符串spName){
返回此参数。spName=spName;
}
public int getSpID(){
返回spID;
}
公共无效设置PID(内部spID){
this.spID=spID;
}
public void takeSPNAME(){
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“输入(sp)名称:”;
this.setSpName(userInput.next());
}
公共无效takeSPID(){
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“输入(sp)ID:”;
this.setSpID(userInput.nextInt());
}
public void printINFO(){
而(!(this.takeSPNAME().matches(“END”)){
this.takeSPNAME();
这个。takeSPID();
hlist.put(spName,spID);
}
系统输出打印项次(hlist);
}
}


由于我没有看到任何回应,我试图自己解决这个问题。工作几天后,我更改了代码并删除了Getter Setter。下面是新代码

导入java.util.*

公共班级学生简介{

public String spName;
public int spID;

public HashMap<String, Integer> hlist= new HashMap<String, Integer>();

public String getSpName() {
    return spName;
}

public String setSpName(String spName) {
    return this.spName=spName ;
}
public int getSpID() {
    return spID;
}
public void setSpID(int spID) {
    this.spID = spID;
}

public void takeSPNAME(){

    Scanner userInput = new Scanner(System.in); 
    System.out.println("Enter (sp)a name: ");
      this.setSpName(userInput.next());
}

public void takeSPID(){
    Scanner userInput = new Scanner(System.in); 
    System.out.println("Enter (sp)ID: ");
    this.setSpID(userInput.nextInt());

}

public void printINFO(){

    while(!(this.takeSPNAME().matches("END"))){

        this.takeSPNAME();
        this.takeSPID();
        hlist.put(spName, spID);
    }
    System.out.println(hlist);
}
public String spName;
public int spID;


public HashMap<String, Integer> hList= new HashMap<String, Integer>();

public String takespName(){
    Scanner userInput = new Scanner(System.in);
    System.out.println("Enter (sp) student name: ");
    return spName= userInput.next();

}
public void printSPinfo1(){

    String myname= this.takespName();


    if((spName).matches("END")){

        System.out.println("EXIT");
    }
    while(!(spName).matches("END")){

        Scanner userInput = new Scanner(System.in);
        System.out.println("Enter (sp) student id: ");
        spID=userInput.nextInt();
        this.takespName();
        hList.put(spName, spID);
    }
    System.out.println(hList);
}
公共字符串spName;
公共int spID;
public HashMap hList=new HashMap();
公共字符串takespName(){
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“输入(sp)学生姓名:”;
返回spName=userInput.next();
}
public void printSPinfo1(){
字符串myname=this.takespName();
如果((spName).matches(“END”)){
System.out.println(“退出”);
}
while(!(spName).matches(“END”)){
扫描仪用户输入=新扫描仪(System.in);
System.out.println(“输入(sp)学生id:”;
spID=userInput.nextInt();
this.takespName();
hList.put(spName,spID);
}
系统输出打印项次(hList);
}
}

输出:(就像我想要的)

输入(sp)学生id: 15 输入(sp)学生姓名: 结束
{bob=14,END=15,aron=11}

任何人都可以…?感谢工作4天后,我终于自己解决了这个问题。我不得不稍微改变一下设计(不再使用Getter Setter)。这就是我所做的