Solved

sorting arrays

Posted on 2003-10-25
3
269 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 51
Basic Java Case or If-Else statement... 3 50
Arrays.asList  VS  ArrayList 4 56
eclipse console opening separately 2 18
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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