RSS Forums RSS

Need urgent help in code!

Please support our Java advertiser: Programming Forums
Reply
Posts: 25
Reputation: shubhang is an unknown quantity at this point 
Solved Threads: 0
shubhang shubhang is offline Offline
Light Poster

Need urgent help in code!

  #1  
Nov 19th, 2008
Hi,
I am new at java.
I have made the search class of an address book program
with the functions of add, modify and delete.

Please have a look into the code.

Please also explain the function of
 e.printstacktrace();

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

	public void calc()throws IOException
	{
			
                DataInputStream in=DataInputStream(new FileInputStream("adbk.txt"));
		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:");
		ch=Integer.parseInt(br.readLine());
																							

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


																					//clr here
		while((str=in.readLine())!=null)
		{

			StringTokenizer st=new StringTokenizer(str,"_");
			for(int i=0;i<=7;i++)
			{
				arr[i]=st.nextToken();
			}

			if(sval==arr[pos])
			{
				ctr++;
				for(int i=0;i<=7;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");
			}
		}
}
AddThis Social Bookmark Button
Reply With Quote  
Posts: 4,111
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 458
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is online now Online
Industrious Poster

Re: Need urgent help in code!

  #2  
Nov 19th, 2008
Originally Posted by shubhang View Post
Please have a look into the code.
Why? Is there something wrong with it? If so, what specifically?

Originally Posted by shubhang View Post
Please also explain the function of
 e.printstacktrace();
There is no such function as you have written it. There is, however, a printStackTrace() method on Throwable. Read that API link to see what it does.
Reply With Quote  
Posts: 25
Reputation: shubhang is an unknown quantity at this point 
Solved Threads: 0
shubhang shubhang is offline Offline
Light Poster

Re: Need urgent help in code!

  #3  
Nov 20th, 2008
When I run the program,the search part(HIGHLIGHTED) does not work.
Please tell me where I am going wrong.

import java.io.*;
import java.util.*;
class search
{
	private String str,sval;
	private String arr[]=new String[8];
	private int ch;
	private int pos,ctr;

	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

	public void calc()throws IOException
	{
		BufferedReader in=new BufferedReader(new FileReader("adbk.txt"));			//file read
		//DataInputStream in=DataInputStream(new FileInputStream("adbk.txt"));
		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:");
		ch=Integer.parseInt(br.readLine());
																							

		switch(ch)
		{
			case 1:
			{
				System.out.println("enter fn");
				pos=0;
				break;
			}
			case 2:
			{
				System.out.println("enter ln");
				pos=2;
				break;
			}
			case 3:
			{
				System.out.println("enter arcd");
				pos=3;
				break;
			}
			case 4:
			{
				System.out.println("enter city");
				pos=5;
				break;
			}
			case 5:
			{
				System.out.println("enter state");
				pos=6;
				break;
			}
			default:
			{
				System.out.println("error");
				break;
			}
		}
		sval=br.readLine();
																							//clr here
		while((str=in.readLine())!=null)
		{

			StringTokenizer st=new StringTokenizer(str,"_");
			for(int i=0;i<=7;i++)
			{
				arr[i]=st.nextToken();


			}
			System.out.println();

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

		}

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

	}

}
Thanks.
Reply With Quote  
Posts: 1,957
Reputation: masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold masijade is a splendid one to behold 
Solved Threads: 198
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Need urgent help in code!

  #4  
Nov 20th, 2008
Define "does not work".
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Posts: 173
Reputation: Antenka will become famous soon enough Antenka will become famous soon enough 
Solved Threads: 38
Antenka's Avatar
Antenka Antenka is offline Offline
Junior Poster

Re: Need urgent help in code!

  #5  
Nov 20th, 2008
It doesn't work because you compare strings using "==". Try this:
  1. if(sval.equals(arr[pos]))
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Views: 396 | Replies: 4 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 3:09 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC