vendredi 17 juillet 2020

Why cant the if-else-function work with the scanner input? [duplicate]

So i tried to make a Corona-App for school. My plan is to search every Array in the Arraylist for the ID (which is in the second line of every Array (1;2;3;4)) and check if the value is already existent. The issue is I always activate the else-statement even though the value is in the Array.

import java.util.*;
public class java_prrr {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); 
        String[] device1 = {"Samsung","1","true", "Dennis", "0", "8"};
        String[] device2 = {"Apple","2","false", "Angela", "6", "4"};
        String[] device3 = {"Huawei","3","false", "Fabian", "12", "3"};
        String[] device4 = {"Blackberry","4","false", "Chris","3", "16"};
        
        ArrayList<String[]> devices = new ArrayList<String[]>();
        devices.add (device1);  //0
        devices.add (device2);  //1
        devices.add (device3);  //2
        devices.add (device4);  //3
        
        System.out.println("ID wird abgerufen (manuelle Eingabe)");
        String id = sc.nextLine();
        
        for(int i = 0; i < devices.size(); i++){
            if(id == devices.get(i)[1]){
                System.out.println("Sie sind bereits in der Array aufgelistet.");
            }
        else{
                System.out.println("Geben Sie bitte Ihren Gerätenamen ein:");
                String name = sc.nextLine();
                System.out.println("Haben Sie Corona? true/false:");
                String corona = sc.nextLine();
                System.out.println("Wie lautet Ihr Name?");
                String besitzer = sc.nextLine();
                System.out.println("Koordinaten werden abgerufen (manuelle Eingabe)");
                System.out.println("x-Koordinate");
                String entfernungx = sc.nextLine();
                System.out.println("y-Koordinate");
                String entfernungy = sc.nextLine();
                String[] Jonas= {name, id, corona,besitzer, entfernungx, entfernungy};
                devices.add(Jonas);
                System.out.println(devices.get(4));             
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire