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

Print a specific cell from a string array

I’ve created a class that includes people data named "Datiutente" and an object based on that class named "du". Every person has a name and a surname (with the set/get methods).

I want to create a system that can provide the user information on a specific person based on the position which they are stored in the array.

I tried using a variable named vd to ask the user which person wanted to visualize based on the position that a person gained in the array (inserted in the for cycle), but when I try to print with vd it just prints "Name: null". Same if I change "vd" to "1". It always prints "Null".

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

Yes, I tested this when I already inserted some data.

do {
    System.out.println("what do you want to do?");
    System.out.println("1 - check for a person");
    System.out.println("2 - Add person");
//    System.out.println("3 - Delete person");
    int choice = Integer.parseInt(keyb.readLine());
    Datiutente du[] = new Datiutente[10];
    
    if (choice == 2){
        System.out.println("How many people?");
        int hm = Integer.parseInt(keyb.readLine());
        
        for (int i=0;i<hm;i++){
            du[i] = new Datiutente();
            System.out.println("insert name:");
            du[i].setName(keyb.readLine());
            System.out.println("insert surname");
            du[i].setSurname(keyb.readLine());
        }  
    }
    
    if (choice == 1){
        System.out.println("which person are you searching?");
        int vd = Integer.parseInt(keyb.readLine());
        System.out.println("position: " + vd);
        System.out.println("Name: "+  du[vd]);
        System.out.println("Surname: " + du[vd]);
    }

there’s the full code:


package appartamento;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Appartamento {

    public static void main(String[] args) throws IOException {
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader keyb = new BufferedReader(input);
        boolean attiva = true;
        
        do {
        System.out.println("what do you want to do?");
        System.out.println("1 - check for a person");
        System.out.println("2 - Add person");
    //    System.out.println("3 - Delete person");
        int choice = Integer.parseInt(keyb.readLine());
        Datiutente du[] = new Datiutente[10];
        
        if (choice == 2){
            System.out.println("How many people?");
            int hm = Integer.parseInt(keyb.readLine());
            
            for (int i=0;i<hm;i++){
                du[i] = new Datiutente();
                System.out.println("insert name:");
                du[i].setName(keyb.readLine());
                System.out.println("insert surname");
                du[i].setSurname(keyb.readLine());
                        }  
        }
        
        if (choice == 1){
            System.out.println("which person are you searching?");
            int vd = Integer.parseInt(keyb.readLine());
            System.out.println("position: " + i);
            System.out.println("Name: "+  du[i]);
            System.out.println("Surname: " + du[i]);
            
        }
        
        
        
        
    } while (attiva = true); 
    
    }
}

and there’s the class "Datiutente":


package appartamento;

public class Datiutente {
    private String name;
    private String surname;
    private String codfis;
    
    public void setName(String name){
        this.name = name;
    }
    
    public String getName(){
        return name;
    }
    
    public void setSurname(String surname){
        this.surname = surname;
    }
    
    public String getSurname(){
        return surname;
    }
    
    
}

>Solution :

In every iteration you define the Datiutente du[] = new Datiutente[10];, so du is reset to {null,...,null} and the data saved in the previous iteration are replaced;
Try to define the array before the loop statement.

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