Solved

sorting arrays

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 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 about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

734 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