Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Default Problem with java classes

    I am working on a project of me.
    I have the one problem with one class :/

    The Compiler find an error in " username.username_check(username); ". I can't find the solution,could anyone help me? Thanx :)

    Java Code:
    import java.util.* ;
    
    class  username 
    {  
       Scanner input = new Scanner(System.in);
       private String Usernames[]={"alexandros","alex","ronis","ronis23"};
       private String Passwords[]={"A12#4","dGf23","ronis23","ronis23"};
       public  String Confirmation[]={"@23fg?","Aw224"};
       boolean username_exist;
       public String username;
       int i;
       int k;
    
       public void username_check(String user)
       {   
           String username=user;
           for (i=0;i<=Usernames.length;i++)
    	   {
    	      if (username.equals(Usernames[i]))
    		   {
                 System.out.print("Username accepted.");
    		     i=100;
                }
              else
               {
                 System.out.print("Give the right username: ");
                 username=input.next();
                 
    		     int counter=0;
    		     while (username!=Usernames[i] && counter<=2);
    		     {
    		        System.out.print("Sorry,you gave again wrong username. Please give the right one: ");
    				username=input.next();
    				counter=counter+1;
    				
    		      }
    			 i=100;
                }    
            }
        }	 
    }
    
    
    class diko
    {
       public static void main(String args[])
       {
          Scanner input = new Scanner(System.in);
          
    	  String username;
    	  String password;
    	  System.out.println("Give username: ");
    	  username=input.next();
    	  username.username_check(username);
    	  System.out.println("Give password: ");
    	  password=input.next();
    	  
    	}
    }
    Last edited by alexandros_tselios; 04-08-2013 at 08:59 PM.

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: Problem with java classes

    First of all in line 54 you make a big mistake..
    if we want to use a method from a class we must first create an object of this class and then to use his methods
    so the code should be :
    Java Code:
    import java.util.* ;
     
    class  UserAccess
    { 
       Scanner input = new Scanner(System.in);
       private String Usernames[]={"alexandros","alex","ronis","ronis23"};
       private String Passwords[]={"A12#4","dGf23","ronis23","ronis23"};
       public  String Confirmation[]={"@23fg?","Aw224"};
       boolean username_exist;
       public String username;
       int i;
       int k;
     
       public void username_check(String user)
    	{  
    		username=user;
    		for (i=0;i<=Usernames.length;i++)
    		{
    			if (username.equals(Usernames[i]))
    			{
    				System.out.print("Username accepted.");
    				i=100;
    				}
    			else
    			{
    				System.out.print("Give the right username: ");
    				username=input.next();
    				k=0;
    				int counter=0;
    				while (username!=Usernames[i] && counter<=2);
    				{
    					System.out.print("Sorry,you gave again wrong username. Please give the right one: ");
    					username=input.next();
    					counter=counter+1;
    					System.out.println()
    					k=k+1;
    				}
    				i=100;
    			}   
    		}
    	}   
    }
     
     
    class diko
    {
    	public static void main(String args[])
    	{
    		Scanner input = new Scanner(System.in);
           
    		String username;
    		String password;
    		System.out.println("Give username: ");
    		username=input.next();
    		UserAccess UserAccessObj = new UserAccess();
    		UserAccessObj.username_check(username);
    		System.out.println("Give password: ");
    		password=input.next();
           
    	}	
    }
    but you have a lot of mistakes so i make an onother program :

    Java Code:
    import java.util.* ;
     
    class  UserAccess{
    	private String [] CorrectUserNames = {"UserName1","UserName2","UserName3","UserName4","UserName5"};
    	private String [] CorrectPasswords = {"PassWord1","PassWord2","PassWord3","PassWord4","PassWord5"};
    	private String UserName ;
    	private String UserPassword ;
    	private Scanner input = new Scanner(System.in);
    	private int count = 0 ;
    	
    	public boolean UserNameCheck(){
    		boolean access = false ;
    		System.out.print("Give Username :");
    		UserName = input.next();
    		while(!access && count < 3){
    			for(int i = 0 ; i < CorrectUserNames.length ; i++ ){
    				if(CorrectUserNames[i].equalsIgnoreCase(UserName)){
    					System.out.println("Username Accepted!");
    					access = true;
    				}
    			}
    			if(!access){
    				count = count + 1;
    				System.out.print("Wrong Username. Try again :");
    				UserName = input.next(); 
    			}
    		}
    		return access;
    	}
    	
    	public boolean PassWordCheck(){
    		boolean access = false ;
    		count = 0;
    		System.out.print("Give Password :");
    		UserPassword = input.next();
    		while(!access && count < 3){
    			for(int i = 0 ; i < CorrectPasswords.length ; i++ ){
    				if(CorrectPasswords[i].equalsIgnoreCase(UserPassword)){
    					System.out.println("Password Accepted!");
    					access = true;
    				}
    			}
    			if(!access){
    				count = count + 1;
    				System.out.print("Wrong password. Try again :");
    				UserPassword = input.next(); 
    			}
    		}
    		return access;
    	}
    }
     
     
    class User{
    	public static void main(String args[]){
    		UserAccess UserAccessObj = new UserAccess();
    		if(UserAccessObj.UserNameCheck()){
    			if(UserAccessObj.PassWordCheck()){
    				System.out.println("Welcome User!");
    			}else{
    				System.out.println("Try Again next Time ");
    			}
    		}else{
    			System.out.println("Try Again next Time ");
    		}
    	}
    }

Similar Threads

  1. Problem when Overloading Classes
    By Army in forum New To Java
    Replies: 3
    Last Post: 04-12-2012, 09:07 AM
  2. Problem with method and classes
    By picasso88 in forum New To Java
    Replies: 3
    Last Post: 01-29-2012, 09:46 PM
  3. Classes Problem
    By FreeQy in forum New To Java
    Replies: 5
    Last Post: 11-05-2011, 03:37 PM
  4. Problem with classes
    By liakos in forum NetBeans
    Replies: 14
    Last Post: 03-10-2011, 07:34 PM
  5. inner classes problem
    By smallmos1 in forum New To Java
    Replies: 7
    Last Post: 11-18-2010, 03:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •