943,923 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 604
  • Java RSS
Nov 14th, 2008
0

Need help in code

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
shubhang is offline Offline
25 posts
since Oct 2008
Nov 14th, 2008
-1

Re: Need help in code

What are the error messages? Post the whole stack.
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Nov 14th, 2008
0

Re: Need help in code

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).
Reputation Points: 10
Solved Threads: 0
Light Poster
shubhang is offline Offline
25 posts
since Oct 2008
Nov 14th, 2008
0

Re: Need help in code

You really should change
Java Syntax (Toggle Plain Text)
  1. System.out.println("error");
to
Java Syntax (Toggle Plain Text)
  1. e.printStackTrace();
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 15th, 2008
0

Re: Need help in code

Sir , but what does this do? e.printStackTrace();
Was this the main problem in the code?
Reputation Points: 10
Solved Threads: 0
Light Poster
shubhang is offline Offline
25 posts
since Oct 2008
Nov 15th, 2008
0

Re: Need help in code

Sir,
I tried the
Java Syntax (Toggle Plain Text)
  1. e.printStackTrace();
,instead of
Java Syntax (Toggle Plain Text)
  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();
Reputation Points: 10
Solved Threads: 0
Light Poster
shubhang is offline Offline
25 posts
since Oct 2008
Nov 15th, 2008
0

Re: Need help in code

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

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
shubhang is offline Offline
25 posts
since Oct 2008
Nov 15th, 2008
0

Re: Need help in code

Click to Expand / Collapse  Quote originally posted by shubhang ...
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.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Nov 15th, 2008
0

Re: Need help in code

The error i get while runtime is as follows:
Java Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 0
Light Poster
shubhang is offline Offline
25 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Collision Detection - Micro Java
Next Thread in Java Forum Timeline: printing an element in an array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC