• Post Reply Bookmark Topic Watch Topic
  • New Topic

Struggle with two classes  RSS feed

 
Sebastian Laursen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, as you may have guessed I'm quite new to Java. I have some trouble with understanding how to get two classes to get objects from each other (if that is the correct term).

Lets say I have a login class, in which I have a method checking what the user has entered into the console (I have not displayed the whole code, but this class works as it should and give the user an option to enter username and password and return it true if entered correct).

public static boolean validateUserPass(String userName, String password) {
String[] user = {"admin"};
String[] passwords = {"firkanten"};
boolean check = false;

for (int i = 0; i < user.length; i++) {
if (userName.equals(user[i])) {

if (password.equals(passwords[i])) {
check = true;

Now, in another class I want a display box to appear on the screen and give the user three options: Click yes, no or cancel. I get this to run perfectly alone, this is not the hard part for me. I want the display box only to appear when the correct username and password is given, but I can't seem to figure out how to do this probably.

Can anyone guide me in the right direction? If you can understand what I want, and if you don't please let me know what I can give of information to let you understand it better.
 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like you want to know how to access the validateUserPass() function from the first class.
Let's name that class "Login.class" and the dialog box one "DialogBox.class"
So in order to connect the second with the first one, all you need to do is type:

This creates a new object of the first class, and the reference to it called 'log', which doesn't matter, you can call it however you want.
So now when you have the object ready, you can use it's methods using the reference name, following with a dot (.) and then the method name:

The code above will run the method validateUserPass() by creating an object of the first class, and use it in the second.

I hope this helped
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch
 
Sebastian Laursen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the answers, much appreciated!!.

It got me a little further, but I think my problem is that when I create the new object in the new class doing it like this:



Then, when I write log.validateUserPass(user,pass), I think I have to write the String names I have given(userName, password), but it says it cannot be resolved to a variable. Is that because it is an input and not an assigned value as such? Or is it because I need to make a local variable within the new class aswell?

Further I will only run the method display() if valiedateUserPass turns out true. Can this be done with a simple if statement or will you recommend any other option?

Not sure I have the logic correct here. Will it help to post the code or can you follow me so far?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post your code remembering to UseCodeTags (← click)
 
Marko Matosevic
Greenhorn
Posts: 12
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, the user and pass arguments in the function are just, well, arguments. You need to pass the correct value inside, either by your variables that hold the strings for username and password, or by doing it manually:

But you probably want to keep user and pass Strings in two variables and just pass those variables as the function arguments.
 
Sebastian Laursen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Display box class:



The LoginSystem class:

 
Sebastian Laursen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay so I got the login to work now by using this:


However, I don't think I use it as I am supposed to use it. And again, I want the display box to appear after the login.
 
Sebastian Laursen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Please post your code remembering to UseCodeTags (← click)


Am I completely missing the logic here?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your classes only contain static methods so you don't need to create an instance of the class to call the methods. Having said that the methods other than 'main' shouldn't really be static.

The LoginSystem class is a little odd because it's providing the UI and also checking the validity of the entered values, normally you separate the UI from the business logic. If you want to do it in one class for simplicity then the methods should be instance methods (ie not static) and the entered values could then be stored in instance variables. The validate method would not then need to accept any arguments as the instance would already have the entered values stored.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!