import java.util.Arrays;
import java.util.Scanner;
class MixNumberWithUnit_AM{
String unit;
Mix_AM mixNumber;
public MixNumberWithUnit_AM (String str) {
String[] abc= parseUnit(str);
mixNumber= new Mix_AM(abc[0]);
unit = abc[1];
}
public static MixNumberWithUnit_AM position(String str){
String userInput= str;
MixNumberWithUnit_AM ft;
MixNumberWithUnit_AM in;
int ftpos = userInput.indexOf("ft");
int inpos = userInput.indexOf("in");
int appos = userInput.indexOf("'");
int quotepos = userInput.indexOf("\"");
if(ftpos !=-1 && inpos !=1){ // This is if both exist
String ftString= userInput.substring(0,ftpos+2);
String inString= userInput.substring(ftpos+2);
ft= new MixNumberWithUnit_AM(ftString);
in= new MixNumberWithUnit_AM(inString);
}
else if(ftpos !=-1 && inpos ==-1){ //if "feet" exist (only have feet)
ft= new MixNumberWithUnit_AM(userInput);
in= new MixNumberWithUnit_AM(" ");// intialize to zero inches
}
else if(ftpos ==-1 && inpos !=-1){
ft= new MixNumberWithUnit_AM(" ");
in= new MixNumberWithUnit_AM(userInput);
}
else if(appos !=-1 && quotepos !=-1){
String ftString= userInput.substring(0,ftpos+1);
String inString= userInput.substring(ftpos+1);
ft= new MixNumberWithUnit_AM(ftString);
in= new MixNumberWithUnit_AM(inString);
}
else if(appos !=-1 && quotepos ==-1){
ft= new MixNumberWithUnit_AM(userInput);
in= new MixNumberWithUnit_AM(" ");
}
else if (appos ==-1 && quotepos ==-1){
ft= new MixNumberWithUnit_AM(" ");
in= new MixNumberWithUnit_AM(userInput);
}
}
public static String[] parseUnit (String userInput){
String str= userInput.trim();
int len = str.length();
String s2=str.substring(len-2); //get the last 2 characters
String p1="",p2="";
if(s2.equals("in") || s2.equals("ft") ){
p1=str.substring(0,len-2).trim();
p2=s2;
}
else{
s2=str.substring(len-1); //get the last 2 characters
if (s2.equals("'") || s2.equals("\"") ){
p1=str.substring(0,len-1).trim();
p2= s2;
}
else{
p1=""; p2=""; System.out.println("error:input w/o units");}
}//outer if
String[] rtn={p1,p2};
return rtn;
}//parse
public String displayMixNumberWithUnit() {
String str=mixNumber.displayMix();
if(str.equals("0")){
str="";
}
else{
str= str+unit;
}
return str;
}//display
public static String get(){
Scanner scan = new Scanner (System.in);
String userInput = scan.nextLine();
userInput = userInput.trim();
return (userInput);
} //get
public static void main(String[] args, String userInput) {
String userinput=get();
MixNumberWithUnit_AM m1=position(userInput);
System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
userInput = get();
MixNumberWithUnit_AM m2=position(userInput);
System.out.println(m2[0].displayMixNumberWithUnit()+m2[1].displayMixNumberWithUnit());
}//main
}//class
当我使用jGrasp编译此代码时,我得到了数组错误。我相信我还没有正确地声明我的阵列。我对数组如何工作了解不多。有人可以帮我解决一下。
MixNumberWithUnit_AM.java:105: error: array required, but MixNumberWithUnit_AM found
System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
^
MixNumberWithUnit_AM.java:105: error: array required, but MixNumberWithUnit_AM found
System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
^
MixNumberWithUnit_AM.java:109: error: array required, but MixNumberWithUnit_AM found
System.out.println(m2[0].displayMixNumberWithUnit()+m2[1].displayMixNumberWithUnit());
^
MixNumberWithUnit_AM.java:109: error: array required, but MixNumberWithUnit_AM found
System.out.println(m2[0].displayMixNumberWithUnit()+m2[1].displayMixNumberWithUnit());
^
4 errors
答案 0 :(得分:3)
MixNumberWithUnit_AM m1=position(userInput);
System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
变量m1
不是数组。这是一个对象参考。
答案 1 :(得分:0)
您的position()
方法的返回类型为MixNumberWithUnit_AM
。如此行所示
MixNumberWithUnit_AM m1=position(userInput);
然后尝试访问变量m1
时,如果数组不是数组,则以数组的形式访问它,它是此行中MixNumberWithUnit_AM
类型的对象
System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
如果您的对象MixNumberWithUnit_AM
有一个数组字段,那么您必须像
m1.arrayField[0]....
答案 2 :(得分:0)
您已声明MixNumberWithUnit_AM position(String str)
函数以返回类MixNumberWithUnit_AM
的对象。但是在下面的代码片段中:
MixNumberWithUnit_AM m1=position(userInput); // m1 is returned as object,
//you are even declaring m1 as object
System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());
// you are accessing it as array