Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

An object wont let me specified a particular variable – Java

I’m a new learner in java and created my own program to practice and upskill myself. But I have a problem that states [incompatible types: String cannot be converted to Friend] in which I’m having a hard time to figure out why the specific object wont let me specified variable like "friend.name[i]" Is my logic wrong? How can I fix this? Here’s my code:

Main:

package Friendprogram;
import Libraries.*;
import java.util.Scanner;

public class forfunportfolio {
    public static void main(String[] args) {
        
        Squad gang = new Squad ("Thunder", 3, 15, 18);
        Friend [] friend = new Friend [gang.maxSlot]; // declared an array of object,
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to the Thunder Squad\nPress 1 to Join us\nPress 2 to view current brothers\nPress 3 to exit");
        int answer = input.nextInt();

        while(answer < 4){
        switch(answer){
        case 1:
                   for(int i=0; i<gang.maxSlot; i++){ // Ideally I want this to input only one time and permanently stored in 
                                                      // array unless exited the program but I think it's impossible 
                       System.out.printf("Enter Name:");
                       String tmpName = input.next();
                       friend[i] = tmpName; // The problem is here, where "friend.name" doesnt work here    
                       System.out.println("Enter Age:");
                       int tmpAge = input.nextInt();
                       friend[i] = tmpAge; //The problem is here, "friend.age" doesnt work here           
                   }   
                   for(Friend f: friend){
                       gang.Agechecker(f); // Bollean to check if age is good or not
                       gang.newFriend(f); // to "permanently store" in an array unless exited the program
                   }
                break;
        case 2:
            System.out.println("Thunder brothers information");
            gang.viewSquad();
            break;
            
        case 3:
            System.out.println("Thanks for using the program brother");
            System.exit(0);        
        }         
         System.out.println("Welcome to the Thunder Squad\nPress 1 to Join\nPress 2 to view current brothers\nPress 3 to exit");
         answer = input.nextInt();        
        }
    }
}

Friend:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

package Libraries;
public class Friend {
    public String name;
    public int age;
    public Friend(String name, int age) { // Setted to string name and int age
        this.name = name;
        this.age = age;
    }
}

Squad:

package Libraries;
public class Squad {
    public String name;
    public Friend [] friend = new Friend[3]; 
    public int memCount;
    int count = 0;
    public int minAge;
    public int maxAge;
    public int maxSlot;
    public Squad(String name, int maxSlot, int minAge, int maxAge) {
        this.name = name;
        this.maxSlot = maxSlot;
        this.minAge = minAge;
        this.maxAge = maxAge;
    }

    public void newFriend(Friend foe){ // My logic for "permanently stored" in array
        friend[count] = foe;
        memCount++;
        count++;
    }
    public boolean Agechecker(Friend brother){ // To disregard the information if
                                               // doesnt not meet the age requirement
        if (brother.age > minAge && brother.age < maxAge){
            System.out.println("Age verified");
        return true;
        }
        else if(brother.age < minAge && brother.age > maxAge){ 
            System.out.println("Age did not meet requirements");
        }
           return false;
    }
    public void viewSquad (){ //View the "Permanently Stored" brothers
    System.out.println("The list of brothers in the Squad");
    for(int i=0; i<=memCount; i++){
            System.out.println(friend[i].name + ", " + friend[i].age); // Here I can specifically declared name and age
        }
    }
}

TD;LR: I do not want to manually initialize the information like in the example code below, instead I want the user to input and scanned it where it will be saved in array until exited. I also wanted it to only input once, not in loop but it seems impossible.

Friend [] friend= new Friend []{new Friend ("Joe", 18),
    new Friend ("Trump", 17),
    new Friend ("Hilary", 16),
};

>Solution :

Instead of writing friend[i] = tmpName; and friend[i] = tmpAge;, you need to use the new keyword and the constructor for Friend. But you can pass in the variables as arguments, instead of using constants. That is, you should replace the two problematic lines with this.

friend[i] = new Friend(tmpName, tmpAge);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading