Grimmjow91 Grimmjow91 - 7 months ago 11
Java Question

Method from another file not being seen

I am having an issue with a java program that I have to make for class. I am to take two letters and convert those to a location. I am to then take a message and encrypt it using a basic character swap. I seem to be having an issue with the fact that a call in my Driver file can't see the method I am trying to call in my Actions file. I sure I am missing something simple and doing it wrong but the more I read the documentation the more confused I become. Could someone please explain to me what I am doing wrong. I am trying to call my getCountry method which is in my Actions file from my Driver file. The error is at the line

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


I know that this could be done in one file but my teacher wants it in two. I know I am missing something simple but I do not understand the documentation that I have been reading in regards to using objects. Could you please point out what I have done wrong? I would be quite grateful. Thank you.

Answer

Change your main so that you pass the important data into Driver:

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

Then use those Actions inside of the main constructor:

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
}

And similarly then use the loc reference inside of the yourmom() method

Also, don't compare Strings using == or !=. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two object references are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if(locHA == "FR")
    locHA = "France";

do

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

or

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