Solved

sorting arrays

Posted on 2003-10-25
3
252 Views
Last Modified: 2010-03-31
here is a simple one, I need to sort these:
80 IsmatK
38 DavidW
58 DavidS
42 DavidR
where the int is the mark and the string is the login name, they are seperated into 2 arrays.
so-far this is what I have.

import java.io.*;
import java.awt.FileDialog;
import java.lang.*;

class three
/*Class to import text file of marks into an array, then sort it, then print it out
 to a screen sorted*/
{
      static public void main(String args[]) throws IOException
      {

            FileReader file = new FileReader("marks.txt");
                  
            //tokenize the input stream so that we can add it to an array
            StreamTokenizer inputStream = new StreamTokenizer(file);
            inputStream.wordChars(0x20,0x7F);
            
            //making my new arrays for the stuff
            String[] names = new String[4];
            int[] marks = new int[4];
            
            //heading test
            System.out.println("This is a heading \n");
            
            //initiate value i
                  
            //start adding the file into my arrays
            float tokenType = inputStream.nextToken();            
            for (int i = 0; tokenType != inputStream.TT_EOF; i++)
            {
                        
            marks[i] = (int)inputStream.nval;
            inputStream.nextToken();
            names[i] = inputStream.sval;
            tokenType = inputStream.nextToken();
            
            }
            file.close();
            
            System.out.println(marks.length);
      }

}

0
Comment
Question by:Pyro-San
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 9619329
You'd be better off doing the following:

class Student {
      private String name;
      private int mark;
      
      public Sudent(String name, int mark) {
            this.name = name;
            this.mark = mark;
      }
      
      public void setMark(int mark){
        this.mark = mark;
      }

      public int getMark(){
        return this.mark;
      }


      public void setName(String name){
        this.name = name;
      }

      public String getName(){
        return this.name;
      }
}

Then you can create a list of Students and sort them easily
0
 
LVL 1

Expert Comment

by:CyTG
ID: 9637507
as CEHJ said, make a comparator and do collections.sort on the list..
0
 
LVL 92

Expert Comment

by:objects
ID: 10224848
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept CEHJ's comment as answer

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

objects
EE Cleanup Volunteer
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 72
solarwind tftp server 2 32
Strange loading of website behaviour 3 23
recursion example 16 70
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now