By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,506 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

Getting "Null"on print out

P: 8
hey, im doing this program and im reading the information from a file, i cant figure out how to get the print out right. Ive looked at all my for loops and i thought the error would be in the "substrings" that im doing..mabye the errror is in lines 25-39? this might be alost cause cause you cant run this file without the data file, but the scores and averages work, but the the peoples names dont print out right

it says

Null Null M 58 68 73 33 F

insted of Null Null it should be a first and last name

thanks

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.io.*;
  3. import java.awt.*;
  4. import java.lang.*;
  5.  
  6. public class Prgrm1
  7. {
  8. private FileInputStream file_in;
  9. private DataInputStream data_in;
  10. public Prgrm1()
  11. {
  12. int a = 0, b=0;
  13. String temp = "";
  14. String[] first = new String[10];
  15. String[] last = new String[10];
  16. char[] gender = new char[10];
  17. int[][] grades = new int[10][5];
  18. float[] grd_avg;
  19. String[] ltr_grd;
  20. float[] t;
  21.  
  22.  
  23. try
  24. {
  25. file_in = new FileInputStream("data314.dat");
  26. data_in = new DataInputStream(file_in);
  27. for (a=0; a<=10; a++)
  28. {
  29. temp = data_in.readUTF();
  30. int n1 = temp.indexOf(";")+1;
  31. int n2 = temp.indexOf(",");
  32. String out ="";
  33. first[a] = temp.substring(0,temp.indexOf(" ") );
  34. first[a] = first[a].toLowerCase();
  35. first[a] = capname(first[a]);
  36. last[a] = temp.substring(temp.indexOf(" ")+1,temp.indexOf(":") );
  37. last[a] = last[a].toLowerCase();
  38. last[a] = capname(last[a]);
  39. gender[a] = temp.charAt(temp.indexOf(":")+1);
  40.  
  41. for(b=0; b<4; b++)
  42. {
  43. grades[a][b] = Integer.parseInt(temp.substring(n1,n2));
  44. n1= n2+1;
  45. n2= temp.indexOf(",",n1);
  46. out+=" "+grades[a][b];
  47. }
  48. grades[a][4] = Integer.parseInt(temp.substring(n1));
  49. out +=" "+ grades[a][4];
  50. grd_avg=grade_avg(grades);
  51. out +=" "+ grd_avg[a];
  52. ltr_grd=grd_ltr(grd_avg);
  53. out +=" "+ltr_grd[a];
  54. sort(first,last,gender,grades,grd_avg,ltr_grd);
  55.  
  56. System.out.println(first[a]+" "+last[a]+" "+gender[a]+" "+out);
  57.  
  58.  
  59. }
  60.  
  61.  
  62. }
  63. catch(IOException e)
  64. {
  65.  
  66. JOptionPane.showMessageDialog(null,"Hello");
  67. }
  68.  
  69.  
  70. }
  71.  
  72. public static void sort(String[]first, String[]last, char[]gender, int[][]grades, float[]grd_avg, String[] ltr_grd)
  73. {
  74. int a,b,c,delta;
  75. float temp;
  76. String name_temp;
  77. char gend_temp;
  78.  
  79.  
  80. for(a=0;a<9;a++)//start sort
  81. {
  82. for(b=a+1;b<10;b++)
  83. {
  84. if(grd_avg[a]>grd_avg[b])
  85. {
  86.  
  87. temp=grd_avg[a];
  88. grd_avg[a]=grd_avg[b];
  89. grd_avg[b]=temp;
  90.  
  91. name_temp=first[a];
  92. first[a]=first[b];
  93. first[b]=name_temp;
  94.  
  95. name_temp=last[a];
  96. last[a]=last[b];
  97. last[b]=name_temp;
  98.  
  99. gend_temp=gender[a];
  100. gender[a]=gender[b];
  101. gender[b]=gend_temp;
  102.  
  103. name_temp=ltr_grd[a];
  104. ltr_grd[a]=ltr_grd[b];
  105. ltr_grd[b]=name_temp;
  106.  
  107. for(c=0;c<5;c++)
  108. {
  109. delta=grades[a][c];
  110. grades[a][c]=grades[b][c];
  111. grades[b][c]=delta;
  112. }
  113. }
  114. }
  115. }
  116. }
  117.  
  118.  
  119.  
  120. public String capname(String name)
  121. {
  122. String a = name.substring(0,1);
  123. a = a.toUpperCase();
  124. name = name.substring(1);
  125. a += name;
  126. name = a;
  127. return name;
  128. }
  129.  
  130. public float[] grade_avg (int[][]grd)
  131. {
  132. float avg_tot =0;
  133. float[] grd_avg = new float[10];
  134. int a,b;
  135. for(a=0;a<10;a++)
  136. {
  137. avg_tot=0;
  138. for(b=0;b<5;b++)
  139. {
  140. avg_tot+= grd[a][b];
  141. }
  142. grd_avg[a]= avg_tot/(float)5;
  143. }
  144.  
  145.  
  146.  
  147. return grd_avg;
  148. }
  149.  
  150. public String[] grd_ltr(float[] avg)
  151. {
  152. String[] grd_ltr=new String [10];
  153. int a;
  154. for(a=0;a<10;a++)
  155. {
  156. if(avg[a]>90)
  157. grd_ltr[a]="A Excellent";
  158. if(avg[a]<90)
  159. grd_ltr[a]="B";
  160. if(avg[a]<80)
  161. grd_ltr[a]="C";
  162. if(avg[a]<70)
  163. grd_ltr[a]="D";
  164. if(avg[a]<60)
  165. grd_ltr[a]="F";
  166. }
  167.  
  168. return grd_ltr;
  169. }
  170. public static void main(String[] args)
  171. {
  172. Prgrm1 app = new Prgrm1();
  173. System.exit(0);
  174. }
  175. }
  176.  
Sep 11 '08 #1
Share this Question
Share on Google+
4 Replies


10K+
P: 13,264
Throw away those arrays and create a Person class with those fields as properties of a person.
Sep 12 '08 #2

Expert 100+
P: 785
It can have many causes that you get null back as first name.
1.) print out the string that you read from file right away after reading it, so you can verify if the names is there. Maybe the name was not saved at all? Or wrong delimiters?
2.) you should always check if a String.indexOf() function returns -1 or not, that means if it was found or not. for example in line 36 <<temp.indexOf(":") >> could return -1 if there is no colon ":" in the returned string. But your code doesn't care and continues to use the returned value for a substring as if it always would return a value != -1 !!! What happens if you assign firstname = temp.substring(0,-1) or lastname=temp.substring(-1+1, 12) as a result of that?

Remember: always write code to validate the values before using them and check if they are really meeting your assumptions. The time spent for this validation code is worth gold later on. If you still think it's wasted time (what all newbies think) then you will be forced later on to do it anyway if you work in a company and experts from QA, fellow programmers and testing team will convince you that it is much better to do. So just get used to "proper" coding, the earlier the better.
A function like capname() would be never accepted to go to production.
What is if the passed String name is null or empty? Then
Expand|Select|Wrap|Line Numbers
  1. String a = name.substring(0,1);
would crash!
Try to write functions in a way that they can deal with any possible data, not just meeting your assumptions. (read about assertions)
And if you cannot avoid assumptions in rare cases (because of performance reasons or whatever), then you have to comment them well !

And a last tip: use sun java coding standards and long, meaningful names.
instead "capname()" you should have used "capitalizeName()" (camel case, no abbreviations). Or better just "capitalize()", because you could use it not only for names. That makes it easier for others to understand.
Sep 12 '08 #3

P: 8
thanks, i figured it all out for the most part, and im just trying to get the sort to work
Sep 15 '08 #4

10K+
P: 13,264
thanks, i figured it all out for the most part, and im just trying to get the sort to work
You will find it far much easier to program in Java if you are always thinking objects. Those arrays all over the place are evil.
Sep 16 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.