I have a couple of classes in my app.

So in one of them i use same int to make my life easier on the other one

import java.util.*;

public class ObtainKey 
{
	public static void KeyAnalyzer()
	{
		int idx = 0;
		int tokenCount;
		String input;
		String inputArr[] = new String [10];
		System.out.println("Enter Search Key :");
		Scanner getKey = new Scanner(System.in);
		input = getKey.nextLine();
		StringTokenizer st = new StringTokenizer(input);
		tokenCount = st.countTokens();
		while (st.hasMoreTokens())
		{
			inputArr[idx]=st.nextToken(); idx++;
		}
		
		
		
		int SearchWord = 0;
		int Search2Words = 0;
		int SearchAnd = 0;
		int SearchOr = 0;
		int SearchProx = 0;
		int position = 0;
		
		if (tokenCount == 0)
		{
			System.out.println("Please provide a searchKeyword");
		}
		if (tokenCount == 1) // search for 1 word
		{
			SearchWord = 1;
			System.out.println("Looking for : " + inputArr[0]);

and if statements go on.
On my second class i would like to call these int again

import java.io.*;
import java.util.Scanner;



public class Search 
{
	public static void ScanSearch(String fileslist[]) 
	{
		try
		{
			for (int x=0; x<fileslist.length; x++)
			{
				
				//open file
				FileInputStream fstream = new FileInputStream(fileslist[x]);
				DataInputStream in = new DataInputStream(fstream);
				BufferedReader br = new BufferedReader(new InputStreamReader(in));
				String strLine;
				while ((strLine = br.readLine()) !=null )
				{
					if(SearchWord = 0)
					{
					
					}
				}
			}
		}
		catch (Exception e)
		{
			System.err.println("Error: " + e.getMessage());
		}
	}
}

Error pops out while typing down the int's name.
Where am i wrong ?

Recommended Answers

All 6 Replies

Actually is something like this possible ?
If not what alternative means do i have ?

If your methods etc are all static then you can declare your variables as
public static
outside the methods which (a) makes them available to all methods in that class and (b) makes them available to other classes by referring to them using the class name, eg ObtainKey.SearchWord
This will work, but is a poor way to design a real program - uncontrolled sharing of variables like this tends to collapse in chaos as the program gets bigger, so don't get in the habit of doing this.
For info on the best way to do this look at this:
http://docstore.mik.ua/orelly/java-ent/jnut/ch03_05.htm

If your methods etc are all static then you can declare your variables as
public static
outside the methods which (a) makes them available to all methods in that class and (b) makes them available to other classes by referring to them using the class name, eg ObtainKey.SearchWord
This will work, but is a poor way to design a real program - uncontrolled sharing of variables like this tends to collapse in chaos as the program gets bigger, so don't get in the habit of doing this.
For info on the best way to do this look at this:
http://docstore.mik.ua/orelly/java-ent/jnut/ch03_05.htm

Do you mean the integer from SearchWord, Search2Words, etc.? How do you associate these 2 classes together? You may need to acquire the values from your first class (ObtainKey) and then pass them to the second class (Search). Your class definition looks weird by the way...

@Taywin

1. Yes i mean these integers (SearchWord, Search2Words etc)
2. I haven't Associated them. I Just Call them from the main :

public class main 
{
	public static void main(String args[])
	{
		new Folder();
		ObtainKey findkey = new ObtainKey();
		findkey.KeyAnalyzer();
	}

}

@Taywin

1. Yes i mean these integers (SearchWord, Search2Words etc)
2. I haven't Associated them. I Just Call them from the main :

public class main 
{
	public static void main(String args[])
	{
		new Folder();
		ObtainKey findkey = new ObtainKey();
		findkey.KeyAnalyzer();
	}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.