Need help in code

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
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 help in code

 
0
  #1
Nov 14th, 2008
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.

  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. char ch1='y',ch2;
  8. private int n,ctr;
  9. clear c1=new clear();
  10. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  11.  
  12. public void calc()throws IOException
  13. {
  14. DataInputStream in=new DataInputStream(new FileInputStream("adbk.txt")); //file read
  15. System.out.println("How would you like to search?");
  16. System.out.println("1)by fn");
  17. System.out.println("2)by ln");
  18. System.out.println("3)by area code");
  19. System.out.println("4)by city");
  20. System.out.println("5)by state");
  21. System.out.println("enter choice:");
  22. n=Integer.parseInt(br.readLine())-1;
  23. c1.clr(); //clr here
  24.  
  25. switch(n)
  26. {
  27. case 1:
  28. {
  29. System.out.println("enter fn");
  30. }
  31. case 2:
  32. {
  33. System.out.println("enter ln");
  34. }
  35. case 3:
  36. {
  37. System.out.println("enter arcd");
  38. }
  39. case 4:
  40. {
  41. System.out.println("enter city");
  42. }
  43. case 5:
  44. {
  45. System.out.println("enter state");
  46. }
  47. default:
  48. {
  49. System.out.println("error");
  50. break;
  51. }
  52. }
  53. sval=br.readLine();
  54.  
  55.  
  56. c1.clr(); //clr here
  57.  
  58.  
  59. while(in.available()!=0)
  60. {
  61. str=in.readLine();
  62. StringTokenizer st=new StringTokenizer(str," ");
  63. for(int i=0;i<8;i++)
  64. {
  65. arr[i]=st.nextToken();
  66. }
  67.  
  68. if(sval==arr[n])
  69. {
  70. ctr++;
  71. for(int i=0;i<8;i++)
  72. {
  73. System.out.print(arr[i]+" ");
  74. System.out.println();
  75. }
  76. }
  77. }
  78.  
  79. System.out.println("Total hits="+ctr);
  80.  
  81. }
  82. public static void main(String args[])
  83. {
  84. search s=new search();
  85.  
  86. try
  87. {
  88. s.calc();
  89. }
  90.  
  91. catch(IOException e)
  92. {
  93. System.out.println("error");
  94. }
  95. }
  96. }
Help will be highly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,208
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Need help in code

 
-1
  #2
Nov 14th, 2008
What are the error messages? Post the whole stack.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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 help in code

 
0
  #3
Nov 14th, 2008
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).
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,483
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: 515
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Need help in code

 
0
  #4
Nov 14th, 2008
You really should change
  1. System.out.println("error");
to
  1. e.printStackTrace();
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 help in code

 
0
  #5
Nov 15th, 2008
Sir , but what does this do? e.printStackTrace();
Was this the main problem in the code?
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 help in code

 
0
  #6
Nov 15th, 2008
Sir,
I tried the
  1. e.printStackTrace();
,instead of
  1. 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();
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 help in code

 
0
  #7
Nov 15th, 2008
This is the modified code.
It is not working properly.
Please look into it.

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

Re: Need help in code

 
0
  #8
Nov 15th, 2008
Originally Posted by shubhang View Post
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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
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 help in code

 
0
  #9
Nov 15th, 2008
The error i get while runtime is as follows:
  1. Exception in thread "main" java.util.NoSuchElementException
  2. at java.util.StringTokenizer.nextToken(StringTokenizer.java:232)
  3. at search.calc(search.java:70)
  4. at search.main(search.java:93)

What does this indicate?
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC