| | |
Need help doing sorts
![]() |
•
•
Join Date: Apr 2008
Posts: 15
Reputation:
Solved Threads: 0
This is the main of my program all errors have been solved now I need help sorting my array list with a selection, bubble and insertion sort to print out in
alphabetic order using the name as key
numeric order using the income as key
order of nationality in alphabetic order
Each array must have its own class. Im clueless on how to do this and been sitting at the computer feeling stupid please help!!!!
// ***************************************************************************************
// Demographics.java Programmed by : [[Alana R. Lewis]]
//
//
//
//Implement a set of classes that define various types of demographic information
//about a set of people, such as age, nationality, occupation, income and so on.
//You will find appropriate classes defined below and illustrated in the UML diagrams.
// ****************************************************************************************
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
public class Demographics
{
public static void main(String[] args)throws IOException
{
System.out.println("I did not receive any assistance from anyone other than Mr. Willis or an assigned tutor.\n");
// create a fileScan Scanner for a file, StudentNames.txt
Scanner fileScan = new Scanner(new File("Demographics.txt"));
ArrayList people = new ArrayList();
// check whether the fileScan has the next item (i.e., next line)
while (fileScan.hasNext()) // // Read until file is exhausted
{
// create a String Tokenizer for a line
StringTokenizer st = new StringTokenizer( fileScan.nextLine());
// read name from tokenizer
String name = st.nextToken();
// read income from tokenizer
String ageString = st.nextToken();
// convert gpa into double
int age = Integer.parseInt( ageString );
// read gender from tokenizer
String gString = st.nextToken();
// read gender from tokenizer
Gender_Type gender = Gender_Type.valueOf( gString );
// read nationality from tokenizer
String nString = st.nextToken();
// read nationality from tokenizer
Nationality_Type nationality = Nationality_Type.valueOf( nString );
// read occupation from tokenizer
String oString = st.nextToken();
// read occupation from tokenizer
Occupation_Type occupation = Occupation_Type.valueOf( oString );
// read income from tokenizer
String incomeString = st.nextToken();
// convert gpa into double
double income = Double.parseDouble( incomeString );
//new person and insert into arraylist
Person person = new Person();
// do the sets
// print the person
person.setName(name);
person.setAge(age);
person.setGender(gender);
person.setNationality(nationality);
person.setOccupation(occupation);
person.setIncome(income);
people.add(person);
}
//print arraylist
System.out.println(people);
//do the sorts
//Print out List for Alphabetic Order(using the name as the key)
System.out.println("\nAlpabetic Order List");
System.out.println("----------------------");
//Print out list in numeric order (using income as key)
System.out.println("\nNumeric Order List");
System.out.println("--------------------");
//Print out list in order of nationality(Alphabetic Order)
System.out.println("\nNationality Order List");
System.out.println("------------------------");
}
}
alphabetic order using the name as key
numeric order using the income as key
order of nationality in alphabetic order
Each array must have its own class. Im clueless on how to do this and been sitting at the computer feeling stupid please help!!!!
// ***************************************************************************************
// Demographics.java Programmed by : [[Alana R. Lewis]]
//
//
//
//Implement a set of classes that define various types of demographic information
//about a set of people, such as age, nationality, occupation, income and so on.
//You will find appropriate classes defined below and illustrated in the UML diagrams.
// ****************************************************************************************
import java.util.StringTokenizer;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Arrays;
import java.io.*;
public class Demographics
{
public static void main(String[] args)throws IOException
{
System.out.println("I did not receive any assistance from anyone other than Mr. Willis or an assigned tutor.\n");
// create a fileScan Scanner for a file, StudentNames.txt
Scanner fileScan = new Scanner(new File("Demographics.txt"));
ArrayList people = new ArrayList();
// check whether the fileScan has the next item (i.e., next line)
while (fileScan.hasNext()) // // Read until file is exhausted
{
// create a String Tokenizer for a line
StringTokenizer st = new StringTokenizer( fileScan.nextLine());
// read name from tokenizer
String name = st.nextToken();
// read income from tokenizer
String ageString = st.nextToken();
// convert gpa into double
int age = Integer.parseInt( ageString );
// read gender from tokenizer
String gString = st.nextToken();
// read gender from tokenizer
Gender_Type gender = Gender_Type.valueOf( gString );
// read nationality from tokenizer
String nString = st.nextToken();
// read nationality from tokenizer
Nationality_Type nationality = Nationality_Type.valueOf( nString );
// read occupation from tokenizer
String oString = st.nextToken();
// read occupation from tokenizer
Occupation_Type occupation = Occupation_Type.valueOf( oString );
// read income from tokenizer
String incomeString = st.nextToken();
// convert gpa into double
double income = Double.parseDouble( incomeString );
//new person and insert into arraylist
Person person = new Person();
// do the sets
// print the person
person.setName(name);
person.setAge(age);
person.setGender(gender);
person.setNationality(nationality);
person.setOccupation(occupation);
person.setIncome(income);
people.add(person);
}
//print arraylist
System.out.println(people);
//do the sorts
//Print out List for Alphabetic Order(using the name as the key)
System.out.println("\nAlpabetic Order List");
System.out.println("----------------------");
//Print out list in numeric order (using income as key)
System.out.println("\nNumeric Order List");
System.out.println("--------------------");
//Print out list in order of nationality(Alphabetic Order)
System.out.println("\nNationality Order List");
System.out.println("------------------------");
}
}
Well, I guess you weren't paying attention at all in class when sorting was discussed. You haven't even started a skeletal implementation of a single algorithm.
I'd say you have some reading to do first: http://en.wikipedia.org/wiki/Sorting_algorithm
I'd say you have some reading to do first: http://en.wikipedia.org/wiki/Sorting_algorithm
![]() |
Similar Threads
- Quicksort sorts all but 1 number (C++)
- Should I major? (Computer Science)
- opinions on O(n log n) sorts (Java)
- copy constructor problem (C++)
- another newbie with alot of redhat and apache server Q'S (Linux Servers and Apache)
- internet explorer 5.5 problems (Web Browsers)
- no macs? (OS X)
- the books you use (Computer Science)
- C++ Academic (C++)
Other Threads in the Java Forum
- Previous Thread: Problem using java.util.Scanner
- Next Thread: Canvas, pain() and playing sound
| Thread Tools | Search this Thread |
add android api applet application applications array arrays automation bank binary bluetooth chat class clear client code codesnippet collections component converter database development dice digit ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list loop looping main map method methods mobile myregfun mysql netbeans newbie nonstatic openjavafx parameter pearl php problem program programming project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string superclass swing swt text-file thread threads tree windows






