2014-04-19 4 views
0

임 오류 메시지 Cannot invoke getAmountFood() on the primitive type int를 판독호출 생성자

public static String averageFood(){ 
    int x = 0; 
    int y = 0; 
    int percent = 0; 
    String average = null; 
      for(int a = 0; a < population; a++){ 
        gerbil[a].getfoodeaten(); 
        for (int b=0; b < foodeats.length; b++){ 
          x += foodeats[b+1]; 
        } 
        for(int c = 0; c < maxfood.length; c++){ 
          y = totalfood[c].getAmountFood() + y; 
        } 
      percent = (int)((x/y*100)); 
      average = gerbil[a].getId()+" ("+gerbil[a].getName()+" "+percent+"%"; 
      System.out.println(average); 
      } 
      return average; 
} 

아래 방법 y = totalfood[c].getAmountFood() + y;

코드

의 특정 라인에서 에러를 수신 int로. 이것이 의미하는 바는 무엇이며 어떻게 해결할 수 있습니까? 당신이 int 배열로 정의 totalfood을 가지고 있기 때문에 그 이유는

Class gerbillab 

package gerbillab; 

import java.util.Scanner; 

import gerbillab.Gerbil; 

public class gerbillab{ 
public static int population; 
public static int[] maxfood; 
public static int[] foodeats; 
public static int types; 
public static String[] idnumber; 
public static String g; 
public static String gerbilId; 
public static Gerbil[] gerbil; 
public static String amountoffoodeaten; 
public static String gerbilsearch; 
public static String thisgerbil; 
public static void main(String args[]){ 
     Scanner keyboard = new Scanner(System.in); 

    System.out.println("How many types of food do the gerbils eat?");  
    String f = keyboard.nextLine(); 
    int totalF = Integer.parseInt(f); 

    String[] food = new String[totalF]; 
    maxfood = new int[totalF]; 
    for 
     (int a = 0; a<food.length; a++){ 
     System.out.println("Name of food number " + (a+1)); 
     String foodname = keyboard.nextLine(); 
     food[a] = foodname; 

     System.out.println("Max amount of food " + (a+1)); 
     String m = keyboard.nextLine(); 
     int maximum = Integer.parseInt(m); 
     maxfood[a] = maximum; 
     } 

    System.out.println("How many gerbils are in the lab?"); 
    String numberofGerbils = keyboard.nextLine(); 
    population = Integer.parseInt(numberofGerbils); 

    idnumber = new String[population]; 
    String[] nickname = new String[population]; 
    boolean[] bite = new boolean[population]; 
    boolean[] escape = new boolean[population]; 

    gerbil = new Gerbil[population]; 
     for 
      (int b = 0; b<idnumber.length; b++){ 
      System.out.println("What is the id number of gerbil " + (b+1)); 
      String idnumberx = keyboard.nextLine(); 
      idnumber[b] = idnumberx; 

      System.out.println("What is the name for gerbil " + (b+1)); 
      String nicknamex = keyboard.nextLine(); 
      nickname[b] = nicknamex; 

      int[] foodeats = new int[totalF]; 
       for 
        (int c = 0; c<foodeats.length; c++){ 
        System.out.println("how much " + food[c] + " did this gerbil eat"); 
         String amountoffoodeaten = keyboard.nextLine(); 
         foodeats[c] = Integer.parseInt(amountoffoodeaten); 
         } 


      System.out.println("Does this Gerbil bite? Please enter True or False"); 
      String doesitbite = keyboard.nextLine(); 
      if (doesitbite.equalsIgnoreCase("true")) 
       bite[b] = true; 
      else{ 
       bite[b] = false; 
      } 


      System.out.println("Does this Gerbil escape? Enter True or False"); 
      String doesitescape = keyboard.nextLine(); 
      if (doesitescape.equalsIgnoreCase("true")) 
       escape[b] = true; 
      else{ 
       escape[b] = false; 

      } 

      gerbil[b] = new Gerbil(idnumberx, nicknamex, foodeats, escape[b], bite[b], maxfood); 
     } 

     while (true){ 
      System.out.println("What would you like to know?"); 
      String question = keyboard.nextLine(); 
      String search = "search"; 
      String average = "average"; 
      String end = "end"; 
      String restart = "restart"; 


      if (question.equalsIgnoreCase(search)){ 
       new gerbillab().searchForGerbil(); 
       } 

      else 
       if (question.equalsIgnoreCase(average)){ 
        for(int i = 0; i < idnumber.length; i++){ 
         System.out.println(idnumber[i]); 
         } 
        for(int i = 0; i < nickname.length; i++){ 
         System.out.println(nickname[i]); 
         } 
        for(int i = 0; i < bite.length; i++){ 
         System.out.println(bite[i]); 
         } 
        for(int i = 0; i < escape.length; i++){ 
         System.out.println(escape[i]); 
         } 
       } 
      else 
       if (question.equalsIgnoreCase(end)){ 
        System.exit(0); 
       } 
      else 
       if (question.equalsIgnoreCase(restart)){ 
        new gerbillab().main(args); 
        } 
      else 
       System.out.println("Try again"); 
       } 
} 

public static void searchForGerbil() 
{ 
    System.out.println("Please type in a gerbil lab ID"); 
    Scanner keyboard = new Scanner(System.in); 
    String searchgerbil = keyboard.next(); 
    for (int i = 0; i <gerbil.length; i++){ 
      if (searchgerbil.equals(gerbil[i].getId())){ 
       System.out.println(gerbil); 
     } 
     else{ 
      System.out.println("Gerbil " + searchgerbil + " doesnt exist"); 
     } 
} 

} 



} 

클래스 햄스터

package gerbillab; 
public class Gerbil { 

private String idnumber; 
private String nickname; 
private int[] totalfood; 
private String[] foodname; 
private boolean escape; 
private boolean bite; 
private int[] foodeats; 
public String gerbilsearch; 

public Gerbil(String idnumberx, String gerbilName, int[] gerbilfoodeats, boolean gerbilEscape, boolean gerbilBite, int[] maxfood) { 
idnumber = idnumberx; 
nickname = gerbilName; 
foodeats = gerbilfoodeats; 
escape = gerbilEscape; 
bite = gerbilBite; 
totalfood = maxfood; 
} 

public Gerbil(String[] typefood) { 
foodname = typefood; 
} 
public int[] getfoodeaten() { 
return foodeats; 
} 
public Gerbil(int[] numOfFood) { 
totalfood = numOfFood; 
} 
public int[] getAmountFood() { 
return totalfood; 
} 
public boolean getBite() { 
return bite; 
} 
public boolean getEscape() { 
return escape; 
} 
public String getId() { 
    return idnumber; 
} 
public String getName() { 
    return nickname; 
} 
public void setId(String newId) { 
idnumber = newId; 
} 
public void setName(String newName) { 
nickname = newName; 
} 
public String[] gettypesofFood() { 
return foodname; 
} 
} 
+0

Totalfood은 INT의 배열입니다; 당신은 int에서 메소드를 호출 할 수 없습니다. 아주 간단합니다. –

+1

나는 바보입니다. 감사합니다 – user3550505

+0

@ user3550505 내 대답이 도움이 되었습니까? – Jeeter

답변

2

: 여기

참조에 대한 내 코드의 나머지 부분입니다. 당신이 할 필요가 :

private Gerbil[] totalfood; 

이 당신에 erroring하는 라인에 대한 감각을 만들 것, getAmountFoodGerbil의 방법이기 때문에, 그래서 totalfood[c]Gerbil를 반환해야합니다. 문맥으로 판단

, 내 생각 엔 입력하기위한 것입니다 :

y = gerbil[c].getAmountFood() + y;