Solved

sorting arrays

Posted on 2003-10-25
3
289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

627 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