这只是我整个项目的一个类文件。我检查过每一个班级,我发现这是罪魁祸首。它应该包含在另一个类中解析的城市和州(因此你留下了city = Dallas和state = TX)但是当这些参数被传递时,它们不会更新私有String状态和私人String城市。
我一直在??作为toString方法的输出
public class Address
{
private String city;
private String state;
public Address()
{
city="?";
state="?";
}
public String getCity()
{
return city;
}
public String getState()
{
return state;
}
public void setState(String s)
{
//System.out.println(s);
state=s;
//System.out.println(state);
}
public void setCity(String c)
{
city=c;
}
public String toString()
{
String cityState=city+","+state;
return cityState;
}
}
Tester Class
import java.io.*;
import java.util.*;
public class Assignment4
{
public static void main (String[] args)
{ // local variables, can be accessed anywhere from the main method
char input1;
String inputInfo = new String();
String line = new String(), line2;
//instantiate a Bank array
Bank[] accounts = new Bank[10];
Bank bank1 = null;
Scanner scan = new Scanner(System.in);
int index = 0;
// print the menu
printMenu();
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = scan.nextLine().trim();
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
if (line.length() == 1)
{// matches one of the case statements
switch (input1)
{
case 'A': //Add Bank
System.out.print("Please enter the bank information:\n");
inputInfo = scan.nextLine();
bank1 = BankParser.bankParser(inputInfo);
accounts[index] = bank1;
index++;
break;
case 'B': //Display banks
for (int i=0; i< index; i++)
System.out.print(accounts[i].toString());
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown action\n");
break;
}
}
else
{
System.out.print("Unknown action\n");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user**/
public static void printMenu()
{
System.out.print("Choice\t\tAction\n" +
"------\t\t------\n" +
"A\t\tAdd Bank\n" +
"B\t\tDisplay Banks\n" +
"Q\t\tQuit\n" +
"?\t\tDisplay Help\n\n");
}
}
Bank.java类
public class Bank
{
Address Address=new Address();
private String bankName;
private String bankID;
private Address bankAddress= new Address();
public Bank()
{
bankName="?";
bankID="?";
}
public String getBankName()
{
return bankName;
}
public String getBankID()
{
return bankID;
}
public Address getBankAddress()
{
return bankAddress;
}
public void setBankName(String bName)
{
bankName=bName;
}
public void setBankID(String bID)
{
bankID=bID;
}
public void setBankAddress(String city, String state)
{
Address.setCity(city);
Address.setState(state);
}
public String toString()
{
String bankInfo="\nBank Name:\t\t"+bankName+"\nBank ID: \t\t"+bankID+"\nBank address:\t\t"+bankAddress+"\n\n";
return bankInfo;
}
}
BankParser.Java类
public class BankParser
{
public static Bank bankParser(String lineToParse)
{
Bank bank=new Bank();
String delims="[/]+";
String[] primaryParse=lineToParse.split(delims);
bank.setBankName(primaryParse[0]);
bank.setBankID(primaryParse[1]);
String[] secondaryParse=primaryParse[2].split("[,]");
//String city=secondaryParse[0];
//String state=secondaryParse[1];
bank.setBankAddress(secondaryParse[0], secondaryParse[1]);
//System.out.println(secondaryParse[1]);
return bank;
}
}
答案 0 :(得分:1)
您的代码没问题。测试以下内容以说服自己:
public static void main(String[] args) {
Address address = new Address();
address.setCity("Albany");
address.setState("NY");
System.out.println(address.toString());
}
答案 1 :(得分:1)
您有两个Address对象,一个名为Address,另一个名为bankAddress。您正在Address对象上设置city和state值,但返回bankAddress对象。您只需要有一个Address对象,您可以设置值并返回。
答案 2 :(得分:0)
public void setBankAddress(String city, String state)
{
Address.setCity(city);
Address.setState(state);
}
这是你的问题,你可能想做
public void setBankAddress(String city, String state)
{
bankAddress.setCity(city);
bankAddress.setState(state);
}
代替。