Need urgent help in code!

Reply

Join Date: Oct 2008
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!

 
0
  #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
  1. e.printstacktrace();

  1. import java.io.*;
  2. import java.util.*;
  3. class search
  4. {
  5. private String str,sval;
  6. private String arr[]=new String[8];
  7. private int ch;
  8. private int pos,ctr;
  9. clear c1=new clear();
  10. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  11.  
  12. public void calc()throws IOException
  13. {
  14.  
  15. DataInputStream in=DataInputStream(new FileInputStream("adbk.txt"));
  16. System.out.println("How would you like to search?");
  17. System.out.println("1)by fn");
  18. System.out.println("2)by ln");
  19. System.out.println("3)by area code");
  20. System.out.println("4)by city");
  21. System.out.println("5)by state");
  22. System.out.println("enter choice:");
  23. ch=Integer.parseInt(br.readLine());
  24.  
  25.  
  26. switch(ch)
  27. {
  28. case 1:
  29. {
  30. System.out.println("enter fn");
  31. pos=0;
  32. break;
  33. }
  34. case 2:
  35. {
  36. System.out.println("enter ln");
  37. pos=2;
  38. break;
  39. }
  40. case 3:
  41. {
  42. System.out.println("enter arcd");
  43. pos=3;
  44. break;
  45. }
  46. case 4:
  47. {
  48. System.out.println("enter city");
  49. pos=5;
  50. break;
  51. }
  52. case 5:
  53. {
  54. System.out.println("enter state");
  55. pos=6;
  56. break;
  57. }
  58. default:
  59. {
  60. System.out.println("error");
  61. break;
  62. }
  63. }
  64. sval=br.readLine();
  65.  
  66.  
  67. //clr here
  68. while((str=in.readLine())!=null)
  69. {
  70.  
  71. StringTokenizer st=new StringTokenizer(str,"_");
  72. for(int i=0;i<=7;i++)
  73. {
  74. arr[i]=st.nextToken();
  75. }
  76.  
  77. if(sval==arr[pos])
  78. {
  79. ctr++;
  80. for(int i=0;i<=7;i++)
  81. {
  82. System.out.print(arr[i]+" ");
  83. }
  84. System.out.println();
  85. }
  86.  
  87. }
  88.  
  89. System.out.println("Total hits="+ctr);
  90.  
  91. }
  92. public static void main(String args[])
  93. {
  94. search s=new search();
  95.  
  96. try
  97. {
  98. s.calc();
  99. }
  100.  
  101. catch(IOException e)
  102. {
  103. System.out.println("error");
  104. }
  105. }
  106. }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,346
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: 498
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Need urgent help in code!

 
0
  #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
  1. 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 Quick reply to this message  
Join Date: Oct 2008
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!

 
0
  #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 Quick reply to this message  
Join Date: Feb 2006
Posts: 2,281
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 243
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Need urgent help in code!

 
0
  #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 Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Need urgent help in code!

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC