Help needed for project

Thread Solved

Join Date: Aug 2005
Posts: 6
Reputation: Blazer is an unknown quantity at this point 
Solved Threads: 0
Blazer Blazer is offline Offline
Newbie Poster

Help needed for project

 
0
  #1
Aug 31st, 2005
Hi everyone,

I require some help for this project I am currently working on. Basically what I have to do is read from two text files, then calculate some numbers from both files (eg: add the numbers and average them) and after that, write the results to a another file. I have written a bit of code to read from the files but I am stuck at the moment.

import java.io.*;
import java.util.*;

public class Read
{
public static void main(String [] args)
{
BufferedReader input;
String line;
String [] subject = new String[3];
int i = 0;

try
{
FileReader fr = new FileReader("Description.txt");
input = new BufferedReader(fr);

while ((line = input.readLine()) != null)
{
StringTokenizer tokenizer = new StringTokenizer(line, ", \t");
String token;

while (tokenizer.hasMoreTokens())
{
token = tokenizer.nextToken();
System.out.println(token);
// subject[i] = token;
// System.out.println(subject[i]);
// i++;
}
}

input.close();
}

catch (IOException e)
{
System.err.println(e);
}
}
}

You see, I am trying to put each line of the text file into an array. Here is an example of the file:

Introduction to Software
Assignment, 100, 10
Tutorial, 100, 10

I know every line there are 3 strings. But what I am unsure of is how to put each line into a different array.

I hope someone can help. Thank you
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 55
Reputation: NPH is an unknown quantity at this point 
Solved Threads: 1
NPH NPH is offline Offline
Junior Poster in Training

Re: Help needed for project

 
0
  #2
Sep 2nd, 2005
Why do you need to store all the lines? Couldn't you just maintain the average of the numbers in a variable.

For example,


  1. create a total & amount variable (for the average... average = total/amount)
  2. double total =0;
  3. double amount=0;
  4.  
  5. //for each line in the file
  6. while ((line = input.readLine()) != null)
  7. {
  8.  
  9. //tokenize the line
  10. StringTokenizer tokenizer = new StringTokenizer(line, ",");
  11.  
  12. //if there are 3 tokens in the line
  13. if( tokenizer.countTokens() == 3)
  14. {
  15. //get 1st token in line
  16. String str = tokenizer.nextToken();
  17.  
  18. //get 2nd token in line
  19. int num1 = Integer.parseInt(tokenizer.nextToken().trim());
  20.  
  21. //get 3rd token in line
  22. int num2 = Integer.parseInt(tokenizer.nextToken().trim());
  23.  
  24. //add numbers to total
  25. total += num1 + num2;
  26.  
  27. //increment amount
  28. amount++;
  29.  
  30. }
  31.  
  32. }
  33.  
  34. //show average
  35. System.out.println("Average is: " + total/amount);
You should give more information about your project so a better answer can be given. Hope the above helps.

For more help, www.NeedProgrammingHelp.com
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 6
Reputation: Blazer is an unknown quantity at this point 
Solved Threads: 0
Blazer Blazer is offline Offline
Newbie Poster

Re: Help needed for project

 
0
  #3
Sep 2nd, 2005
NPH, thank you very much for the reply.

Sorry for not giving you more details. The question is something like this:

I am suppose to develop a simple reporting system to produce result files for students' performance in a certain subject. The information required to produce the results is stored on 2 files and the results is stored on another third file.

The first file is a subject description file, like I have shown earlier

Introduction to Software
Assignment, 100, 10
Attendance, 100, 10
Exam, 100, 70
Tutorial, 100, 10

(Item name, max score, scaled max score)

And the other is a assessment results file, like this

Introduction to Software
1234567, David, Assignment, 80, Attendance, 100, Exam, 75, Tutorial, 95

(Student ID, name, assessment item, score)

The final result file is suppose to be something like this

Introduction to Software
1234567, (final mark), (difference of final mark from average)

actualScore * scaledMaximumScore / maximumScore = formula to get final mark

I was planning to put the information into an array, but it seems to be quite complicated. There are a few problems that I have encountered. Such as not being able to choose which line to start reading the file from, or splitting each line into a different array.

Thanks again to anyone that helps
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 55
Reputation: NPH is an unknown quantity at this point 
Solved Threads: 1
NPH NPH is offline Offline
Junior Poster in Training

Re: Help needed for project

 
0
  #4
Sep 2nd, 2005
The hardest part is knowing how to store & structure the information once it is read.

Firstly, the program must be able to quickly answer the following question: Give me all info on a subject "Intro to Software". You should then be able to get the scale/non-scaled max scores for any assignment in the subject. You will need to do this when you read the students file and need to calculate the scores.

I recommend creating a class called Subject. Hashmaps can then be used to associate the name "Intro to Software" to an object of type Subject that holds all the info on Intro to Software. This lets you obtain all the info by just knowing the name.

This is not the entire solution but here is the idea. Instead of hard coding the data, you must read it from the file but it should help. Run it and read the comments.

  1. import java.util.*;
  2. //subject class
  3. class Subject
  4. {
  5. //name of subject
  6. private String name;
  7.  
  8. //all the assignments in the subject
  9. private ArrayList assignments = new ArrayList();
  10.  
  11. //constructor
  12. public Subject(String name)
  13. {
  14. this.name = name;
  15. }
  16.  
  17. //add an assignment to subject
  18. public void addAssignment(Assignment assign)
  19. {
  20. assignments.add(assign);
  21. }
  22.  
  23. //get an assignment from subject
  24. public Assignment getAssignment(String name)
  25. {
  26. for(int i =0; i < assignments.size(); i++)
  27. {
  28. Assignment assign = (Assignment)( assignments.get(i) );
  29.  
  30. if(assign.getName().equals(name))
  31. {
  32. return assign;
  33. }
  34. }
  35.  
  36. throw new RuntimeException("Assignment " + name + " not found");
  37. }
  38.  
  39. }
  40.  
  41. //assignment
  42. class Assignment
  43. {
  44. //data
  45. private String name;
  46. private int maxScore;
  47. private int maxScaledScore;
  48.  
  49. //constructor
  50. public Assignment(String name, int maxScore, int maxScaledScore)
  51. {
  52. this.name = name;
  53. this.maxScore = maxScore;
  54. this.maxScaledScore = maxScaledScore;
  55.  
  56. }
  57.  
  58. //get max score
  59. public int getMaxScore()
  60. {
  61. return maxScore;
  62. }
  63.  
  64. //get scaled max score
  65. public int getMaxScaledScore()
  66. {
  67. return maxScaledScore;
  68. }
  69.  
  70. //get name
  71. public String getName()
  72. {
  73. return name;
  74. }
  75.  
  76. //toString()
  77. public String toString()
  78. {
  79. return "The info is... Name: " + name + " Max Score: " + maxScore + " Scaled Max Score: " + maxScaledScore;
  80. }
  81. }
  82.  
  83. class Main
  84. {
  85. public static void main(String[] args)
  86. {
  87. //create hashmap from name to Subject objects
  88. HashMap subjects = new HashMap();
  89.  
  90.  
  91.  
  92.  
  93. //create subject object
  94. Subject sub1 = new Subject("Intro to Something");
  95.  
  96. //add assignments to object
  97. Assignment a11 = new Assignment("Assignment 1", 100, 15);
  98. sub1.addAssignment(a11);
  99.  
  100. //add assignments to object
  101. Assignment a12 = new Assignment("Assignment 2", 100, 70);
  102. sub1.addAssignment(a12);
  103.  
  104. //add assignments to object
  105. Assignment a13 = new Assignment("Assignment 3", 80, 20);
  106. sub1.addAssignment(a13);
  107.  
  108. //add to hashmap
  109. subjects.put("Intro to Something", sub1);
  110.  
  111.  
  112.  
  113.  
  114.  
  115. //create subject object
  116. Subject sub2 = new Subject("Intro to Typing");
  117.  
  118. //add assignments to object
  119. Assignment a21 = new Assignment("Assignment 1", 67, 15);
  120. sub2.addAssignment(a21);
  121.  
  122. //add assignments to object
  123. Assignment a22 = new Assignment("Assignment 2", 77, 15);
  124. sub2.addAssignment(a22);
  125.  
  126. //add assignments to object
  127. Assignment a23 = new Assignment("Assignment 3", 72, 45);
  128. sub2.addAssignment(a23);
  129.  
  130. //add to hashmap
  131. subjects.put("Intro to Typing", sub2);
  132.  
  133.  
  134.  
  135.  
  136. //get subject "Intro to Typing"
  137. Subject aSubjectWeWant = (Subject)( subjects.get("Intro to Typing"));
  138.  
  139. //get assignment 2 from the subject object
  140. Assignment assignWeWant = (Assignment) ( aSubjectWeWant.getAssignment("Assignment 2") );
  141.  
  142. //print out its max score
  143. System.out.println( "Max score of assignment 2 of subject 'Intro to Typing' is: " + assignWeWant.getMaxScore() );
  144.  
  145. }
  146. }
For more help, www.NeedProgrammingHelp.com
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 6
Reputation: Blazer is an unknown quantity at this point 
Solved Threads: 0
Blazer Blazer is offline Offline
Newbie Poster

Re: Help needed for project

 
0
  #5
Sep 2nd, 2005
Wow... very nice code NPH.

Yea, like you said, I will have to change the main class code to implement the string tokenizer so that I can extract each line from the files and create an object out of it.

Can you explain what a hash map is? I don't think I have heard of it before.

The code will definitely help me on writing my code. Thank you so much :cheesy:
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 6
Reputation: Blazer is an unknown quantity at this point 
Solved Threads: 0
Blazer Blazer is offline Offline
Newbie Poster

Re: Help needed for project

 
0
  #6
Sep 3rd, 2005
I have written some part of my main already but I have run into a problem. I get a null pointer exception from this code. Here is my main and my description class.

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Assign1
  5. {
  6. public static void main(String [] args)
  7. {
  8. BufferedReader input;
  9. String line;
  10. Description [] subject;
  11. int i = 0;
  12. int lineCount = 0;
  13.  
  14. try
  15. {
  16. FileReader fr = new FileReader("Description.txt");
  17. input = new BufferedReader(fr);
  18.  
  19. while ((line = input.readLine()) != null)
  20. {
  21. lineCount++;
  22. }
  23.  
  24. System.out.println(lineCount);
  25. subject = new Description[lineCount - 1];
  26.  
  27. while ((line = input.readLine()) != null)
  28. {
  29. StringTokenizer tokenizer = new StringTokenizer(line, ", \t");
  30. String token1, token2, token3;
  31.  
  32. while (tokenizer.hasMoreTokens())
  33. {
  34. token1 = tokenizer.nextToken();
  35. token2 = tokenizer.nextToken();
  36. token3 = tokenizer.nextToken();
  37. subject[i] = new Description(token1, token2, token3);
  38. i++;
  39. }
  40. }
  41.  
  42. input.close();
  43. }
  44.  
  45. catch (IOException e)
  46. {
  47. System.err.println(e);
  48. }
  49. }
  50. }
  51.  
  52. class Description
  53. {
  54. //variables
  55. private String name;
  56. private String maxScore;
  57. private String maxScaledScore;
  58.  
  59. //constructor
  60. public Description(String name, String maxScore, String maxScaledScore)
  61. {
  62. this.name = name;
  63. this.maxScore = maxScore;
  64. this.maxScaledScore = maxScaledScore;
  65.  
  66. }
  67.  
  68. //get scaled max score
  69. public String getMaxScaledScore()
  70. {
  71. return maxScaledScore;
  72. }
  73.  
  74. //get max score
  75. public String getMaxScore()
  76. {
  77. return maxScore;
  78. }
  79.  
  80. //get name
  81. public String getName()
  82. {
  83. return name;
  84. }
  85.  
  86. //toString()
  87. public String toString()
  88. {
  89. return "The info is... Name: " + name + " Max Score: " + maxScore + " Scaled Max Score: " + maxScaledScore;
  90. }
  91. }

In my main class, I am trying to create an array of objects. So, each variable of subject contains the name, maxScore and maxScaledScore of the items in the subject. I hope anyone can help. Thank you
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 6
Reputation: Blazer is an unknown quantity at this point 
Solved Threads: 0
Blazer Blazer is offline Offline
Newbie Poster

Re: Help needed for project

 
0
  #7
Sep 5th, 2005
I am almost complete with my project. Thank you NPH for the help.

Moderators, may I delete my thread?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Help needed for project

 
0
  #8
Sep 5th, 2005
No, you can not delete a thread.
Let others learn something from it if they can (and go to the trouble to search for an answer before asking a question), that's what a public forum is for.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 6
Reputation: Blazer is an unknown quantity at this point 
Solved Threads: 0
Blazer Blazer is offline Offline
Newbie Poster

Re: Help needed for project

 
0
  #9
Sep 5th, 2005
Alright.. thank you :-)
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 12
Reputation: boyzz is an unknown quantity at this point 
Solved Threads: 0
boyzz boyzz is offline Offline
Newbie Poster

Re: Help needed for project

 
0
  #10
Sep 19th, 2005
hmm... wat if there is an alphabet in the ID given? and i want to ignore that student marks and continue from it?

tried some ways, but it usually catch by the exception, and the program exit... :eek:
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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