2017-02-16 4 views
-1

Ich bin mit einem Projekt beauftragt, ein Programm eines mehrdimensionalen Arrays zu erstellen, das Vorname, Nachname, Telefonnummer und Alter enthält. Ich bin zur Zeit der Störung zu erhalten:Methoden hinzufügen, entfernen und anzeigen

Exception in thread "main" java.lang.Error: Ungelöste Kompilation Problem: Eingang kann nicht

at Lab2.<init>(Lab2.java:19) 
at Lab2.main(Lab2.java:7) 

** Editx2 gelöst werden, ich glaube, ich es heraus . Ich möchte im Idealfall Kontakte nach Vor- und Nachnamen entfernen, aber ich kann es nicht herausfinden. Ich habe versucht, eine neue Sysout-Eingabeaufforderung für den Nachnamen wie ich für den Vornamen hinzugefügt und dann eine weitere if-Anweisung hinzugefügt, aber es endete damit etwas funky statt.

import java.util.Scanner; 

public class Lab2 { 
    static String[][] contacts = new String[10][4]; 

    public static void main (String [] args) { 
     new Lab2(); 
    } 

    public Lab2() { 
     String[][] contacts = new String[10][4]; 
     Scanner input = new Scanner(System.in); 
     System.out.println("Welcome to my Contacts Directory. Enter a selection below: "); 

     while(true) { 
      System.out.println("1: Add a contact"); 
      System.out.println("2: Remove a contact"); 
      System.out.println("3: Display your contacts"); 
      System.out.println("0: Exit the Contacts Directory"); 

      int userChoice = input.nextInt(); 

      switch(userChoice) { 
     case 1:   
      addContacts(contacts); 
      break; 

     case 2: 
      removeContacts(contacts); 
      break; 

     case 3: 
      displayContacts(contacts); 
      break; 

     case 0: 
      System.out.println("You are leaving the directory! Goodbye!"); 
      System.exit(0); 
      } 
     } 
    } 

    private static void addContacts(String[][] contacts) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter First Name"); 
     String fName = input.nextLine(); 

     System.out.println("Enter Last Name"); 
     String lName = input.nextLine(); 

     System.out.println("Enter Phone Number"); 
     String num = input.nextLine(); 

     System.out.println("Enter Age"); 
     String age = input.nextLine(); 

      for (int i = 0; i < 10; i++) { 
       if (contacts[i][0] == null || contacts[i][0].equals(null)) { 
        contacts[i][0] = fName; 
        contacts[i][1] = lName; 
        contacts[i][2] = num; 
        contacts[i][3] = age; 
        boolean Inserted = true; 
        break; 


      } 
     } 
    } 

    private static void removeContacts(String[][] contacts) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter the first name of the contact you want to remove: "); 
     String removeContact = input.nextLine(); 
     if (removeContact != null) { 
      for (int i = 0; i < contacts.length; i++) { 
       for (int j = 0; j < contacts[i].length; j++) { 
        if (removeContact.equals(contacts[i][j])) { 
         contacts[i][0] = null; 
         contacts[i][1] = null; 
         contacts[i][2] = null; 
         contacts[i][3] = null; 
         break; 
        } 
       } 
      } 
     } 
     } 


    private static void displayContacts(String[][] contacts) { 
     for(int i = 0; i < contacts.length; i++) { 
      for(int j = 0; j < contacts[i].length; j++) { 
       System.out.println(contacts[i][j] + " "); 
      } 
      System.out.println(); 
     } 
    } 
} 
+0

Es gibt eine ähnliche Antwort hier, aber ich konnte es nicht aus ihrem Code herausfinden. Es ist ziemlich identisch. – JWags

+0

Die removeContacts-Methode funktioniert nicht – JWags

+0

Die Rolle des Konstruktors besteht darin, Instanzvariablen zu initialisieren, Sie haben viele unnötige Dinge. Ich würde vorschlagen, dass Sie die while-Schleife daraus entfernen. –

Antwort

-1

Ihre "Eingabe" Variable [Objekt] ist keine globale Variable, sondern eine Bereichsvariable in einer anderen Funktion.

Verwandte Themen