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