Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: calling class in other class not working

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default calling class in other class not working

    hi there
    i have this class here:
    public class UsernameArrayHolder {
    	public String[] UsernameArrayHolder()  { //need identifier
    		  String[]		UsernameArray	=	{"user1","user2","user3"};				//variable til at holde login navn
     
    		  return UsernameArray;
     
    	}
    }
    and i have one like that but just for password.
    when i try to call it in my other class like this:
     
    import java.util.*;
    public class Login2Darray
     
    {
        public static void main(String [] args){
     
    		  String[] usernameInfo=UsernameArrayHolder.UsernameArray; // i call the other class
     
    		  int i = 0;
     
     
     
    		  Scanner sc=new Scanner(System.in);
     
            System.out.println("Username: ");
     
               String UsernameInput = sc.next();
     
    		  System.out.println("Password: ");
     
               String PasswordInput = sc.next();
     
     
     
    				for (i=0; i<UsernameArray.length; i++){
     
    					if((UsernameInput.equals(UsernameArray[i]) && PasswordInput.equals(PasswordArray[i]))){
     
    								System.out.println("You are logged in");
    								//skriv i værdi til tekst fil
    								i = UsernameArray.length;
    					}	
    				}//end of for loop
     
        }
    }

    the problem is it doesn't seem to work. am i calling it right? and if not how am i supposed do?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: calling class in other class not working

    Rather than "it doesn't seem to work" it's better to say what you did and what happened when you did that. Eg that you compiled a program and recieved some particular compiler message.

    Remember that you don't call classes, you call methods. And methods are, typically, methods of a particular instance of a class. So, in your main() method first create an instance of the UsernameArrayHolder class using "new", then call the method you want as a method of that particular instance. It is possible to have methods that "belong" in some sense to a class rather than to a specific instance: socalled static methods, but these are best avoided to begin with.

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: calling class in other class not working

    so i should make it like this: when i call my method?

    UsernameArrayHolder username = new UsernameArrayHolder();
    PasswordArrayHolder password = new PasswordArrayHolder();
    username.UsernameArrayHolder();
    password.PasswordArrayHolder();

    but when i do that it still says it can't find my UsernameArray :s

  4. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: calling class in other class not working

    What is the code, and what is the compiler message? You have introduced the PasswordArrayHolder class right out of the blue. Work one step at a time.

    Also I notice you have a UsernameArrayHolder() method in the UsernameArrayHolder class. This is confusing at best. If it supposed to be a method call it something else and start it with a lower case letter. If it is supposed to be a constructor then remember that it doesn't return anything.

  5. #5
    Member
    Join Date
    Sep 2012
    Posts
    30
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: calling class in other class not working

    it is suppose to be a constructer call because I want to be able to use the entire array. but i have no idea how to make that call :S

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: calling class in other class not working

    OK. But if it is a constructor then it does not return anything. Some other method of the class will be used to return whatever it is you want to return.

    Are you using a textbook? Because defining classes and using the constructors and methods they define should be explained in detail with examples.

Similar Threads

  1. Java, calling another public class from within the main class giving problems.
    By RandomGaisha in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 26th, 2012, 02:30 PM
  2. calling to another class from another class
    By Diesel298 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 13th, 2012, 10:17 PM
  3. Calling / Updating class?
    By JuLiAnc in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 14th, 2012, 07:56 AM
  4. about calling sub class
    By pokuri in forum Object Oriented Programming
    Replies: 3
    Last Post: January 11th, 2011, 03:30 PM
  5. Help Calling Method From Another Class
    By CheekySpoon in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2010, 10:24 AM