Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > General Coding
You last visited: Today at 08:34

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



JAVA problem array sorting

Discussion on JAVA problem array sorting within the General Coding forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Apr 2012
Posts: 28
Received Thanks: 5
JAVA problem array sorting

i use three arrays where in
System.out.println("Enter number of process: ");
int process = in.nextInt();
String name[] = new String[process];
int age[] = new int[process];
double income[] = new double[process];

bla bla bla so basically i used three arrays and a for loop and the outcome is this.
(note: this is a user input)

so here's my inputs
================
|| Name || Age || Income ||
================
|| chuck || 40 || 40,000 ||
|| norris || 65 || 32,000 ||
|| nemo || 14 || 37,200 ||
================

what should i do if i want it to sort like this?

================
|| Name || Age || Income ||
================
|| nemo || 14 || 37,200 ||
|| chuck || 40 || 40,000 ||
|| norris || 65 || 32,000 ||
================

i want to sort it by age from the youngest to the oldest
and still the name and income is still associated with the age.

can somebody help me ?
morphling18 is offline  
Old 09/28/2012, 09:08   #2

 
Obilee's Avatar
 
elite*gold: 144
Join Date: May 2007
Posts: 62,469
Received Thanks: 29,541
Code:
import java.util.Arrays;
import java.util.Scanner;


public class Main {

	private static class Person implements Comparable<Person> {
		
		private String m_name;
		private int m_age;
		private double m_income;
		
		public Person(String name, int age, double income) {
			this.m_name = name;
			this.m_age = age;
			this.m_income = income;
		}
		
		public String getName() {
			return m_name;
		}
		
		public int getAge() {
			return m_age;
		}
		
		public double getIncome() {
			return m_income;
		}

		//compares this object with a other object for the sorting
		@Override
		public int compareTo(Person p) {
			if( m_age < p.getAge() )
	            return -1;
	        if( m_age > p.getAge() )
	            return 1;
	            
	        return 0;
		}
		//to allow a "nice" output string
		@Override
		public String toString() {
			return m_name + " " + m_age + " " + m_income;
		}
	}
	
	public static void main(String[] args) {
		
		//Array with all Persons
		Person[] persons = new Person[3];
		//Input Scanner
		Scanner input = new Scanner(System.in);
		//User Input ( do it in your Loop)
		for (int i = 0; i < 3; i++) {
			String name = input.next();
			int age = input.nextInt();
			double income = input.nextDouble();
			//Create new Person and save him
			Person person = new Person(name, age, income);
			persons[i] = person;
		}
		//output unsorted array
		System.out.println(Arrays.toString(persons));        
		//sort
		Arrays.sort(persons);		
		//output sorted array
		System.out.println(Arrays.toString(persons));

		
	}
}
I don't know how far your level is but this is a quick solution. You need to put your information about the persons in a Person object. With this you have them always "together" and your problem that you cant refer the age with the right name is gone.

Arrays offers a method to sort all object in them, for this you need the compareTo method in your Person object which says if this person is younger or older than the other person.

try it and ask questions.

Code:
int[] array = new int[]{ 4, 6, 2, 7, 5, 8, 9, 3, 1 };
System.out.println(Arrays.toString( array ));
java.util.Arrays.sort(array);        
System.out.println(Arrays.toString( array ));
Would be also possible if you dont want to use objects, but here you have to asign the names and incomes to the ages what is rly a ugly thing to do.

if you dont want to do it with the arrays.sort u need to read about sort algorithms and implement them yourself: http://en.wikipedia.org/wiki/Sorting_algorithm
Obilee is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
[Java] Wie erzeuge ich aus einem Jsoup Element ein Array?
08/22/2012 - General Coding - 2 Replies
Hi bin gerade dabei eines meiner ersten Java Programme zu "programmieren". Das ganze soll eine EinsatzSMS Anwendung für eine Feuerwehr werden. Hier mal mein Code: EinsatzSmS - Pastebin.com Verwendete Library: jsoup Java HTML Parser, with best of DOM, CSS, and jquery
Java - Byte Array in BufferedImage funktioniert nicht
02/08/2012 - General Coding - 0 Replies
hallo ich versuche schon seit einiger zeit ein byte in ein BufferedImage zu konvertieren aber ich bekomme immer eine IllegalArgumentException weil ImageIO.read(new ByteArrayInputStream(b) null wiedergibt das byte array wird über das internet via socket erhalten und in das byte gespeichert. hier nochmal der ganze code der betroffenen stellen: hier ist der server der die anfrage sendet und das bild empfängt:
[Java] Problem mit Array
10/17/2011 - General Coding - 5 Replies
Hallo lieber User ! Ich habe hier ein Programm das ein Array mit Zufallszaheln befüllt, und es dann mit einer anderen Methode durch Beistriche trennt. Mein Problem liegt in der Testklasse, da er mir da zwar die Ziffern durch Beistriche trennt, diese aber nicht mehr zufällig sondern alle gleich sind. z.b eingabe : 5 ausgabe : 1,1,1,1,1, public class ArrayMethoden { public static int zufallsArray (int anzahl) { int a = new int;
java - switch -> case frage [array]
12/29/2010 - General Coding - 6 Replies
hey hey... hab ein kleines problem, mit der switch - case abfrage mit nutzen von arrays... switch (zuf){ case wetten:uebereinstimmungen = 1;break; } zeigt der mir nen fehler an , mit ner einfachen if-abfrage klappt es zwar, aber sieht mit switch-case sauberer aus, da es mehrere sind...
Array-Problem
05/13/2010 - AutoIt - 0 Replies
Also ich habe die System-Zeit und das System-Datum in einen Array geladen: #include <Date.au3> Global $dat, $tim $dat = _NowCalcDate() If @error Then Return $tim = _NowTime(5)



All times are GMT +1. The time now is 08:34.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.