Array required but (class name) is found

user2868770 picture user2868770 · Oct 14, 2013 · Viewed 7.5k times · Source
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

When ever I compile this code using jGrasp, I get an error with the arrays. I believe I haven't declared my arrays correctly. I don't understand much about how arrays work. Can someone please help me figure it out.

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

Answer

Jim Garrison picture Jim Garrison · Oct 14, 2013
   MixNumberWithUnit_AM m1=position(userInput);
   System.out.println(m1[0].displayMixNumberWithUnit()+m1[1].displayMixNumberWithUnit());

Variable m1 is not an array. It is an object reference.