2 Replies - 4078 Views - Last Post: 10 May 2008 - 08:14 AM Rate Topic: -----

#1 arkafizz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-May 08

error: non-static variable this cannot be referenced from a static con

Post icon  Posted 09 May 2008 - 11:27 PM

well this is a keygen that takes a string "name" from keyboard,and generates a key
by changing each alphabet in name to its second successor.eg a becomes c,b becomes d etc.
this key is then compared to a string key that will be entered by keyboard...

now i get an error when i compile this code:


Keygen.java:21: non-static variable this cannot be referenced from a static cont
ext
UserData user=new UserData(); //USERDATA OBJECT CREATED.

^
1 error







import java.util.Scanner;


public class Keygen{

public static void main(String[]args)
{

System.out.println("welcome to my program");

Scanner input=new Scanner(System.in);

int Slength=0;
int Result=0;

UserData user=new UserData(); //USERDATA OBJECT CREATED.

System.out.println("enter name");

String Name=input.nextLine();
Slength=Name.length();

user.data(Name,Slength); //changed....

System.out.println("enter key");

String uKey=input.nextLine();

Result=user.keyCheck(uKey);

if(Result==0)
{
System.out.println("bad key buddy!");
}

else
{
System.out.println("have fun!");
}


}//main ends




//now class Userdata begins


public class UserData{

char[] Key=new char[20];

public void data(String Name,int Slength){

int temp=0;
int x=0;

for(x=0;x<Slength;x++){

if(Name.charAt(x)=='Z'||Name.charAt(x)=='z'){

Key[x]=Name.charAt(x);
}

else
{
temp=(int) (Name.charAt(x));
temp=temp+2;
Key[x]=(char) (Name.charAt(x));
}





}//for of data ends
}//public void data ends

//keycheck method begins


public int keyCheck(String uKey){

int flag=1;

for(int x=0;x<uKey.length();x++){

if(uKey.charAt(x)!=Key[x])
{
flag=0;
break;
}

}//for of keycheck ends

if(flag==1)
{
return(1);
}

}//public int keycheck ends

}// class USERDATA ends

}//class KEYGEN ends



please help me rectify this error...
thank you for reading!!!

Is This A Good Question/Topic? 0
  • +

Replies To: error: non-static variable this cannot be referenced from a static con

#2 eddieboy665  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 113
  • Joined: 09-April 07

Re: error: non-static variable this cannot be referenced from a static con

Posted 10 May 2008 - 03:55 AM

Hi, not completely sure I understand how the program is supposed to work or what you want it to do but it now compiles:

import java.util.Scanner;


public class Keygen {

	public static void main(String[] args)
	{
		System.out.println("welcome to my program");

		Scanner input = new Scanner(System.in);

		int sLength=0;
		int result=0;

		UserData user = new UserData(); //USERDATA OBJECT CREATED.

		System.out.println("enter name");

		String name = input.nextLine();
		sLength = name.length();

		user.data(name, sLength); //changed....

		System.out.println("enter key");

		String uKey = input.nextLine();

		result = user.keyCheck(uKey);

		if(result == 0) System.out.println("bad key buddy!");
		else System.out.println("have fun!");
	}
}

//now class Userdata begins
class UserData 
{
	char[] key = new char[20];

	public void data(String name, int sLength)
	{
		int temp = 0;
		int x = 0;

		for (x=0; x<sLength; x++){
			if(name.charAt(x) == 'Z'|| name.charAt(x) == 'z')
				key[x] = name.charAt(x);
			else {
				temp = (int) (name.charAt(x));
				temp = temp+2;
				key[x] = (char) (name.charAt(x));
			}
		}
	}

	//keycheck method begins
	public int keyCheck(String uKey)
	{
		// To understand what the key looks like for debugging
		printKey();	
				
		int flag = 1;

		for(int x=0; x<uKey.length(); x++) {
			if(uKey.charAt(x) != key[x]) {
				flag = 0;
				break;
			}
		}

		if(flag==1) return(1);
		else return 0;
	}
	
	private void printKey()
	{
		String theKey = "";
		for (int i=0; i<key.length; i++){
			theKey += key[i];
		}
		System.out.println("THE KEY IS: "+theKey);
	}

}



All I did was move the UserData outside of the main class for it to compile. I also reformatted it and added a method that may be useful for debugging.



Possibly this is what you meant:

	   for (x=0; x<sLength; x++){
			if(name.charAt(x) == 'Z'|| name.charAt(x) == 'z')
				key[x] = name.charAt(x);
			else {
				temp = (int) (name.charAt(x));
				temp = temp+2;
				key[x] = (char) (temp);
			}
		}



key[x] = (char) (temp);
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5831
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: error: non-static variable this cannot be referenced from a static con

Posted 10 May 2008 - 08:14 AM

You have this:
public class Keygen{
	public class UserData {
	}
	public static void main(String[]args) {
		System.out.println("welcome to my program");
		Scanner input = new Scanner(System.in);
		UserData user=new UserData();
	}
}



UserData is an inner class, called from you static method. However, it's unavailable because you don't have a Keygen object, you're calling from the class method. There are two possible solutions.

One is simple:
public class Keygen{
	static class UserData {
	}
}



That will get it to work. However, statics are not a good sign in a java program. I prefer this way.

public class Keygen {
	public class UserData {
	}
	
	public void run() {
		System.out.println("welcome to my program");
		Scanner input = new Scanner(System.in);
		UserData user=new UserData();
	}
	
	public static void main(String[]args) {
		Keygen pgm = new Keygen();
		pgm.run();
	}
}



It looks a little odd at first. However, the less static methods you have ( you only need one ) and the less you have in main, the more versitile your program can be. Now, another class can call your run method, extend the class, etc. I know, doesn't make so much sense in this example, but it's a good habit.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1