2016-04-16 18 views
-1

Ich habe ein Problem mit einem Java-Programm, das ich für den Unterricht machen muss. Ich soll zwei Buchstaben nehmen und diese an einen Ort umwandeln. Ich soll dann eine Nachricht aufnehmen und mit einem einfachen Zeichenaustausch verschlüsseln. Ich habe anscheinend ein Problem mit der Tatsache, dass ein Aufruf in meiner Treiberdatei die Methode, die ich in meiner Actions-Datei aufrufen möchte, nicht sehen kann. Ich bin sicher, dass ich etwas Einfaches vermisse und es falsch mache, aber je mehr ich die Dokumentation lese, desto verwirrter werde ich. Könnte mir bitte jemand erklären, was ich falsch mache. Ich versuche, meine getCountry-Methode, die sich in meiner Actions-Datei befindet, aus meiner Treiberdatei aufzurufen. Der Fehler ist auf der LinieMethode von einer anderen Datei nicht gesehen

locH = loc.getCountry(locationLetters); 

Driver.java

/** 
Program Name: Action 
Date:4/14/2016 

Program Description: This program is going to handle the window where the user enters data. 
It is also going to be what is going to call the methods of the Actions class 
Methods: Driver(), 
*/ 
import javax.swing.*; // For the Swing classes 
import java.awt.event.*; // For the ActionListener Interface 

import java.util.Scanner; //for the keyboard 


public class Driver extends JFrame 
{ 
//delcare 
    private String locationLetters; //this is going hold the users letter selection 
    private String message; //this is going to hold the users message 

    private String locH; //for the holder of location selection 
    private String messH; //to hold the message before change it to an array 

    Scanner keyboard = new Scanner (System.in);//to make the keyboard 
    /** 
    This method is the constuctor that is going to make the window for the program to use. 
    */ 
public Driver() 
{ 
    System.out.println("sup nerd"); 

    yourmom(); 
}//end of Driver() 

public String yourmom() 
{ 
    System.out.println("Please entner the two charater key for the location you want to message"); 
    locationLetters = keyboard.next(); 

    System.out.println("Please enter the message you would like to have encrypted."); 
    message = keyboard.next(); 


locH = loc.getCountry(locationLetters); 
return locH; 

}//end of yourmom() 


public static void main(String[] arg) 
{ 
    Actions loc = new Actions(); 
    Actions mess = new Actions(); 
    new Driver(); 


}//end of main 

}//end of Driver class 

Actions.java

/** 
Program Name: Action 
Date:4/14/2016 
Program Description: This program is going to handle all the encryption actions as well 
loction where the message is being sent. 
Methods:Location(), 
*/ 


public class Actions 
{ 
//decare 

public String messE; //this is for the message that is going to be ecrypted 

public String getCountry(String locHA) 
{ 
    locHA.toUpperCase(); 
    if(locHA == "FR") 
    locHA = "France"; 
    if(locHA == "GB") 
    locHA = "Great Britain"; 
    if(locHA == "CA") 
    locHA = "Canada"; 
    if(locHA == "JA") 
    locHA = "Japan"; 
    if(locHA == "RU") 
    locHA = "Russia"; 
    if(locHA == "GE") 
    locHA = "Germany"; 
    if(locHA == "AU") 
    locHA = "Australia"; 
    if(locHA == "MX") 
    locHA = "Mexico"; 
    return locHA;  
} 
}//end of action class 

Ich weiß, dass diese in einer Datei getan werden könnte, aber mein Lehrer will es in zwei . Ich weiß, dass mir etwas Einfaches fehlt, aber ich verstehe die Dokumentation, die ich in Bezug auf die Verwendung von Objekten gelesen habe, nicht. Könnten Sie bitte darauf hinweisen, was ich falsch gemacht habe? Ich wäre sehr dankbar. Vielen Dank.

+2

Das liegt daran, dass Sie 'loc' in der Methode' main() 'deklariert haben und versuchen, von der Methode' yourmom() 'darauf zuzugreifen. Veröffentlichen Sie auch keine Links zum Code. Fügen Sie Ihren Code in die Frage selbst ein. – user2004685

+3

Der Code muss ** in der Frage selbst ** sein. Nicht bei Pastebin. Wenn Sie nach einem Fehler fragen, ** posten Sie, was der Fehler ist **. –

+0

@ Grimmjow91: Wenn Sie jemanden in einem Kommentar benachrichtigen möchten, pred ''' 'vor ihrem Namen. Wenn du also user2004685 benachrichtigen willst, platziere '' user2004685'' in deinem Kommentar zu ihm. –

Antwort

0

Ihre Haupt ändern, damit Sie die wichtigen Daten in Fahrer passieren:

public static void main(String[] arg) { 
    Actions loc = new Actions(); 
    Actions mess = new Actions(); 
    new Driver(loc, mess); 
} 

dann innerhalb des Haupt Konstruktor diese Aktionen verwenden:

public Driver(Actions loc, Actions mess) { 
    // use parameters to set fields so that the parameter references can be 
    // used elsewhere in the class 
    this.loc = loc; 
    this.mess = mess; 

    System.out.println("sup nerd"); 
    yourmom(loc); // and pass references where needed 
} 

Und verwenden in ähnlicher Weise dann die Lok Referenz innerhalb von Die Methode yourmom()

Vergleichen Sie auch keine Strings mit == oder !=. Verwenden Sie stattdessen die Methode equals(...) oder equalsIgnoreCase(...). Verstehen Sie, dass == überprüft, ob die beiden Objektreferenzen die gleichen sind, was nicht das ist, was Sie interessiert. Die Methoden auf der anderen Seite überprüfen, ob die beiden Zeichenfolgen die gleichen Zeichen in der gleichen Reihenfolge haben, und das ist was hier zählt. Also statt

tun

// always use curly braces too! 
if("FR".equals(locHA)) { 
    locHA = "France"; 
} 

oder

// if you want to allow more liberal capitalization 
if("FR".equalsIgnoreCase(locHA)) { 
    locHA = "France"; 
} 
+0

Ich habe eine Frage dazu. Wann immer ich equals benutze funktioniert es nie und ich weiß nicht warum. – Grimmjow91

0

Put

Actions.getCountry(); 

in der Hauptklasse. Und Sie können die Öffentlichkeit aus der zweiten Klasse nehmen, da es nicht so leicht zugänglich ist.

Verwandte Themen