hi please can some1 help me out with this project,this is the project
write a program to process a set of student marks. Each line of the input file follows this format: a name (which may be considered as a unique key for the purpose of this assignment) followed by a mark. An example is given below:

Simon 4
Anna 10
Simon 4
Anna 9
Anna 5
Edward 10
Once the input has been processed, the next step is to create the output. This should be listed in two ways and written to a single output file. The first list should be in alphabetic order, the second list should be in merit order[1]. For each student in each list print the number of marks and the average. Each list should be preceded by a title.

After both lists have been printed, print the number of students, the average mark of all the students (this is not the same as the average of all the marks processed), and the standard deviation of the student averages (i.e. in the example below, this would be the s.d. of 10.0, 8.0, 4.0). Processing the above input data should produce the following output. All floating point numbers should be printed to one decimal place. When printing the merit order each line should begin with the rank in the merit order, as shown below.

Alpha order
Anna 3 8.0
Edward 1 10.0
Simon 2 4.0

Merit order
1 Edward 1 10.0
2 Anna 3 8.0
3 Simon 2 4.0

Number of students: 3
Average student mark: 7.3
Standard deviation: 2.5
The filenames should be passed in as command-line arguments. If the output file already exists it should be overwritten. Your code should make appropriate use of Java Collection classes.

I was able tp write the first part but am stuck now cus i dont know how to rearrange the names to come out in an alphabetical order
please if any1 could help i ll be gratefull thanks

Recommended Answers

All 9 Replies

Please provide code as you have it. Forum rules are explicit like this one

this is the code,am able to print out the student name but am stuck on print them out in alphabetical order now check the code below
text file for student

Simon 4
Anna 10
Simon 4
Anna 9
Anna 5
Edward 10

code

public class LineReader {
    public static void main(String[] args) throws Exception {
        String file ="E:/java/src/file/Student";
       Scanner sc = new Scanner(new FileReader(file));
         Map<String, List<Integer>> map =  new TreeMap<String,List<Integer>>();
                new TreeMap<String,List<Integer>>();

        while(sc.hasNextLine()){

        System.out.println(sc.nextLine());
        }
        sc.close();
      }
}

Now dear how do you expect the output to be in a sorted order unless

  1. The contents in the file are themselves in a sorted order --OR--.
  2. You manually sort the contents.

But you have done neither.

Also you have declared the "TreeMap" object and also initialized it, but never used it. I assume you have a faint Idea that a TreeMap can be used to sort and store you objects, So I will recommend you first visit the javadocs of the TreeMap class here and then check out this example of using the TreeMap.

Also the next time please use code tags whenever you are pasting code inside your posts, its as simple as putting the [code=java] and [/code] around your code. It keeps the indentations intact and also provides syntax highlighting for you code thereby making it more readable, you can read more about them here.

with treemap i can sort the student names in alphabetical order thats why i used it but the problem now is how do i make the tree map to function? or used the tree for the output

with treemap i can sort the student names in alphabetical order thats why i used it but the problem now is how do i make the tree map to function? or used the tree for the output

I have already linked you to the Javdocs of the TreeMap class in my previous post and also linked to an example on how to use TreeMaps, what more do you wish for ?

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

public class LineReader 
{
	static class myComparator implements Comparator
	{
		public int compare(Object thisVal, Object otherVal)
		{
			if((String)otherVal == null)
				return 1;
			else
				return ((String)thisVal).compareTo((String)otherVal);
		}
	}
	
	public static void main(String[] args) throws Exception 
	{
		String file ="<path>";
		Scanner sc = new Scanner(new FileReader(file));
		Map<String, Integer> map = new TreeMap<String, Integer>(new myComparator());
		StringTokenizer stok = null;
		while(sc.hasNextLine()){	
			stok = new StringTokenizer(sc.nextLine(), " ");
			map.put(stok.nextToken(), Integer.parseInt(stok.nextToken()));
		}
		sc.close();
		Set s = map.entrySet();
		Iterator itr = s.iterator();
		while(itr.hasNext())
			System.out.println(itr.next());
	}	
}

Anna=5
Edward=10
Simon=4

commented: failure to learn -2

@blackcompe

Teach a person to fish, do not give him the fish, thats the purpose of this forum, You should have at least let the O.P. attempt to use and understand the TreeMap collection on his own, and then help him with his solution, instead you have just given him the complete code to his assignment, and all he has to do is cut paste it without any need for understanding a thing !!!

Also I recall jasimp discouraging you from this practice, so why do you persist with it.

commented: Thanks for remembering :) +9

Thanks

It’s traditional to start learning a new programming language by writing a program called "Hello World". You can think of it as a very simple initiation into the ranks of Java programmers. All the program does is write the text "Hello World!" to your computer screen.

The basic steps we will follow for our Hello World program are:

1. Write the program in Java
2. Compile the source code
3. Run the program

SNIP

commented: Useless and irrelevant post. -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.