473,490 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Getting "Null"on print out

8 New Member
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
4 5414
r035198x
13,262 MVP
Throw away those arrays and create a Person class with those fields as properties of a person.
Sep 12 '08 #2
chaarmann
785 Recognized Expert Contributor
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
kaka123
8 New Member
thanks, i figured it all out for the most part, and im just trying to get the sort to work
Sep 15 '08 #4
r035198x
13,262 MVP
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

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

Similar topics

13
3061
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
10
25978
by: dcrespo | last post by:
Hi all, How can I replace all None values with the string 'Null' in a dictionary? For example: convert this: a = {'item1': 45, 'item2': None} into this:
13
3075
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
14
3123
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
9
2001
by: D. Shane Fowlkes | last post by:
I'm using SQL Server 2000 and on my page, I'm simply creating a SQLDataReader and filling in Labels with the retrieved (single) record. However, how can I prevent from getting errors when a field...
5
1902
by: rengeek33 | last post by:
I am building a SQL statement for Oracle and need one part of it to read: , myvar = null, myvar2 = "1", myvar3 = "0", myvar4 = null, etc. I am constructing this string using the String...
1
17185
by: alain MONTMORY | last post by:
Hello everybody, I am a newbie to python so I hope I am at the right place to expose my problem..... :-http://www.python.org/doc/2.4.2/ext/pure-embedding.html 5.3 Pure Embedding I download the...
5
2270
RMWChaos
by: RMWChaos | last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function...
0
2198
by: tgkprog | last post by:
hi i'm trying to use Win32::IEAutomation to automate clickbank.com navigation and page saving i can login and go to the https://www.clickbank.com/account/showTransactions.htm page but cannot...
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.