I am new at java.
I am making an address book program.
Below is a code of the Search class.
Could anyone please look into it?
The program is giving a problem while runtime.

import java.io.*;
import java.util.*;
class search
{
	private String str,sval;
	private String arr[]=new String[8];
	char ch1='y',ch2;
	private int n,ctr;
	clear c1=new clear();
	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

	public void calc()throws IOException
	{
		DataInputStream in=new DataInputStream(new FileInputStream("adbk.txt"));			//file read
		System.out.println("How would you like to search?");
		System.out.println("1)by fn");
		System.out.println("2)by ln");
		System.out.println("3)by area code");
		System.out.println("4)by city");
		System.out.println("5)by state");
		System.out.println("enter choice:");
		n=Integer.parseInt(br.readLine())-1;
		c1.clr();																			//clr here

		switch(n)
		{
			case 1:
			{
				System.out.println("enter fn");
			}
			case 2:
			{
				System.out.println("enter ln");
			}
			case 3:
			{
				System.out.println("enter arcd");
			}
			case 4:
			{
				System.out.println("enter city");
			}
			case 5:
			{
				System.out.println("enter state");
			}
			default:
			{
				System.out.println("error");
				break;
			}
		}
		sval=br.readLine();


				c1.clr();																			//clr here


		while(in.available()!=0)
		{
			str=in.readLine();
			StringTokenizer st=new StringTokenizer(str," ");
			for(int i=0;i<8;i++)
			{
				arr[i]=st.nextToken();
			}

			if(sval==arr[n])
			{
				ctr++;
				for(int i=0;i<8;i++)
				{
					System.out.print(arr[i]+" ");
					System.out.println();
				}
			}
		}

		System.out.println("Total hits="+ctr);

	}
	public static void main(String args[])
		{
			search s=new search();

			try
			{
				s.calc();
			}

			catch(IOException e)
			{
				System.out.println("error");
			}
		}
}

Help will be highly appreciated.:)

Recommended Answers

All 8 Replies

What are the error messages? Post the whole stack.

Please try running the program.
It is a part of my address book program.
The class searches records by first name,surname,area code,city and state.
The clear class just prints 100 blank lines to make it look as if the screen has cleared(I did not know of any other method).

You really should change

System.out.println("error");

to

e.printStackTrace();

Sir , but what does this do? e.printStackTrace();
Was this the main problem in the code?

Sir,
I tried the

e.printStackTrace();

,instead of

System.out.println("error");

But now I get a compile time error as follows:

C:\Documents and Settings\comp1\My Documents\search.java:49: cannot resolve symbol
symbol : variable e
location: class search
e.printStackTrace();

This is the modified code.
It is not working properly.
Please look into it.

import java.io.*;
import java.util.*;
class search
{
	private String str,sval;
	private String arr[]=new String[8];
	char ch1='y',ch2;
	private int n,ctr;
	clear c1=new clear();
	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

	public void calc()throws IOException
	{
		DataInputStream in=new DataInputStream(new FileInputStream("adbk.txt"));			//file read
		System.out.println("How would you like to search?");
		System.out.println("1)by fn");
		System.out.println("2)by ln");
		System.out.println("3)by area code");
		System.out.println("4)by city");
		System.out.println("5)by state");
		System.out.println("enter choice:");
		n=Integer.parseInt(br.readLine());
																							//clr here

		switch(n)
		{
			case 1:
			{
				System.out.println("enter fn");
				break;
			}
			case 2:
			{
				System.out.println("enter ln");
				break;
			}
			case 3:
			{
				System.out.println("enter arcd");
				break;
			}
			case 4:
			{
				System.out.println("enter city");
				break;
			}
			case 5:
			{
				System.out.println("enter state");
				break;
			}
			default:
			{
				System.out.println("error");
				break;
			}
		}
		sval=br.readLine();
		
												
		while(in.available()!=0)
		{
			str=in.readLine();
			StringTokenizer st=new StringTokenizer(str," ");
			for(int i=0;i<8;i++)
			{
				arr[i]=st.nextToken();
			}

			if(sval==arr[n])
			{
				ctr++;
				for(int i=0;i<8;i++)
				{
					System.out.print(arr[i]+" ");
					System.out.println();
				}
			}
		}

		System.out.println("Total hits="+ctr);

	}
	public static void main(String args[])
		{
			search s=new search();

			try
			{
				s.calc();
			}

			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
}

This is the modified code.
It is not working properly.
Please look into it.

That's really not enough information to go by. What specifically isn't working? I'm not seeing any compilation issues so whatever isn't working is most likely a logic error.

The error i get while runtime is as follows:

Exception in thread "main" java.util.NoSuchElementException
                                   at java.util.StringTokenizer.nextToken(StringTokenizer.java:232)
                                   at search.calc(search.java:70)
                                   at search.main(search.java:93)

What does this indicate?

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.