sort array of integers

arrays

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3375 Views - Last Post: 29 September 2009 - 08:22 AM Rate Topic: -----

#1 mil1234  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 109
  • Joined: 01-February 09

sort array of integers

Post icon  Posted 10 August 2009 - 04:47 AM

hi guys.i'm putting 10 integers in an array of integers[] and then i want to sort them in ascending order.and therfore i used the sort method of java.util.Arrays. The problem is that the sorting isn't taken place and instead the resut is ten garbage letters or numbers.tnx fo any help guys:here's the code i have:

import javax.swing.JOptionPane;
import java.util.*;

public class InputIntegersAndSortInAscendingOrder
{
	public static void main (String[]args)
	{
		int [] Nos = new int[10];
		
		for(int iNos=0; iNos<Nos.length;iNos++)
		{
			Nos[iNos]=Integer.parseInt(JOptionPane.showInputDialog("Enter number:"));
			

		}
	   
		Arrays.sort(Nos);
		JOptionPane.showMessageDialog(null, Nos);
		
	}


Is This A Good Question/Topic? 0
  • +

Replies To: sort array of integers

#2 Gasper  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 66
  • Joined: 29-May 09

Re: sort array of integers

Posted 10 August 2009 - 05:00 AM

You can not output whole array at one.
Try printing numbers one by one like:
import javax.swing.JOptionPane;
import java.util.*;

public class InputIntegersAndSortInAscendingOrder
{
	public static void main (String[]args)
	{
		int [] Nos = new int[10];
		
		for(int iNos=0; iNos<Nos.length;iNos++)
		{
			Nos[iNos]=Integer.parseInt(JOptionPane.showInputDialog("Enter number:"));
			

		}
	   
		Arrays.sort(Nos);
		for(int i = 0; i < 10; i++)
			JOptionPane.showMessageDialog(null, "Number " + (i + 1) + " = " + Nos[i]);
		
	}
}



or, concat integers from array

import javax.swing.JOptionPane;
import java.util.*;

public class InputIntegersAndSortInAscendingOrder
{
	public static void main (String[]args)
	{
		int [] Nos = new int[10];
		
		for(int iNos=0; iNos<Nos.length;iNos++)
		{
			Nos[iNos]=Integer.parseInt(JOptionPane.showInputDialog("Enter number:"));
			

		}
	   
		Arrays.sort(Nos);
		
		String output = "";
		for(int i = 0; i < 9; i++)
			output += Nos[i] + ", ";
		output += Nos[9];
		
		JOptionPane.showMessageDialog(null, output);
		
	}
}



Btw, cool class name.

This post has been edited by Gasper: 10 August 2009 - 05:03 AM

Was This Post Helpful? 1
  • +
  • -

#3 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2796
  • View blog
  • Posts: 15,911
  • Joined: 24-September 08

Re: sort array of integers

Posted 10 August 2009 - 08:39 AM

Quote

You can not output whole array at one.



Funny, I did it the other day....


Bubble sort?
Was This Post Helpful? 0
  • +
  • -

#4 j_lauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-September 09

Re: sort array of integers

Posted 25 September 2009 - 07:14 AM

Is there a way to sort the numbers in the reverse order they were entered?

Thanks
Jeremy
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: sort array of integers

Posted 25 September 2009 - 10:01 PM

View Postj_lauer, on 25 Sep, 2009 - 06:14 AM, said:

Is there a way to sort the numbers in the reverse order they were entered?

Thanks
Jeremy

Yes but it is easier to take Gasper suggestion and just invert the loop

		String output = "";
		for(int i = 9; i > 0; i--)
			output += Nos[i] + ", ";
		output += Nos[0];



Sorry Gasper hope you didn't have a copyright on that code :^:
Was This Post Helpful? 0
  • +
  • -

#6 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: sort array of integers

Posted 25 September 2009 - 11:03 PM

You can use foreach loop to loop through the array and display all the elements in it.
for(int i : Nos){
   System.out.println(i + " ");
}



Good luck! :)
Was This Post Helpful? 1

#7 j_lauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-September 09

Re: sort array of integers

Posted 28 September 2009 - 02:57 PM

I don't mean to hijack someones post, but it sort of relates to my issue. I am trying to input a set of 10 numbers(4,5.3,7.2,1.8,99) and have an output in the reverse order they were entered. Here is what I have already.

import javax.swing.JOptionPane;
import java.util.*;

public class sorting
{
	public static void main (String[]args)
	{
		int [] Nos = new int[10];
	   
		for(int iNos=0; iNos<Nos.length;iNos++)
		{
			Nos[iNos]=Integer.parseInt(JOptionPane.showInputDialog("Enter number:"));
		   

		}
	   
		Arrays.sort(Nos);
	   
		String output = "";
		for(int i = 0; i < 9; i++)
			output += Nos[i] + ", ";
		output += Nos[9];
	   
		JOptionPane.showMessageDialog(null, output);
		  
	}
}



any help would be greatly appreciated
Jeremy
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: sort array of integers

Posted 28 September 2009 - 04:45 PM

View Postj_lauer, on 28 Sep, 2009 - 01:57 PM, said:

I don't mean to hijack someones post, but it sort of relates to my issue. I am trying to input a set of 10 numbers(4,5.3,7.2,1.8,99) and have an output in the reverse order they were entered. Here is what I have already.

import javax.swing.JOptionPane;
import java.util.*;

public class sorting
{
	public static void main (String[]args)
	{
		int [] Nos = new int[10];
	   
		for(int iNos=0; iNos<Nos.length;iNos++)
		{
			Nos[iNos]=Integer.parseInt(JOptionPane.showInputDialog("Enter number:"));
		   

		}
	   
		Arrays.sort(Nos);
	   
		String output = "";
		for(int i = 0; i < 9; i++)
			output += Nos[i] + ", ";
		output += Nos[9];
	   
		JOptionPane.showMessageDialog(null, output);
		  
	}
}



any help would be greatly appreciated
Jeremy

If you want to output them in reverse order they were entered DON'T sort them
And just the reverse loop for(int i = 0; i >= 0; i--) that was displayed a few posts ago
Was This Post Helpful? 0
  • +
  • -

#9 j_lauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-September 09

Re: sort array of integers

Posted 28 September 2009 - 05:14 PM

View Postpbl, on 28 Sep, 2009 - 03:45 PM, said:

View Postj_lauer, on 28 Sep, 2009 - 01:57 PM, said:

I don't mean to hijack someones post, but it sort of relates to my issue. I am trying to input a set of 10 numbers(4,5.3,7.2,1.8,99) and have an output in the reverse order they were entered. Here is what I have already.

import javax.swing.JOptionPane;
import java.util.*;

public class sorting
{
	public static void main (String[]args)
	{
		int [] Nos = new int[10];
	   
		for(int iNos=0; iNos<Nos.length;iNos++)
		{
			Nos[iNos]=Integer.parseInt(JOptionPane.showInputDialog("Enter number:"));
		   

		}
	   
		Arrays.sort(Nos);
	   
		String output = "";
		for(int i = 0; i < 9; i++)
			output += Nos[i] + ", ";
		output += Nos[9];
	   
		JOptionPane.showMessageDialog(null, output);
		  
	}
}



any help would be greatly appreciated
Jeremy

If you want to output them in reverse order they were entered DON'T sort them
And just the reverse loop for(int i = 0; i >= 0; i--) that was displayed a few posts ago



i tried that, it reverse sorts them from highest to lowest. I am looking to reverse the order they were entered without sorting. An example is if I enter these numbers 5.0 4.4 1.9 2.9 3.4 3.5 the output should be 3.5 3.4 2.9 1.9 4.4 5.0 along with listing the lowest number entered. I understand the sorting, but i don't think i need to sort it to get the output.

Thanks
Jeremy

sorry if my post was confusing
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10825
  • View blog
  • Posts: 40,363
  • Joined: 27-December 08

Re: sort array of integers

Posted 28 September 2009 - 05:17 PM

Also, if you have values like 3.8, you should consider using doubles, which support decimal places, instead of ints, which truncate the decimal places. Let me illustrate:

Your input: 3.5
Your input as an int- 3
Your input as a double- 3.5
Was This Post Helpful? 0
  • +
  • -

#11 j_lauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-September 09

Re: sort array of integers

Posted 28 September 2009 - 05:37 PM

View Postmacosxnerd101, on 28 Sep, 2009 - 04:17 PM, said:

Also, if you have values like 3.8, you should consider using doubles, which support decimal places, instead of ints, which truncate the decimal places. Let me illustrate:

Your input: 3.5
Your input as an int- 3
Your input as a double- 3.5


The problem is when i shift every int to double int I start to get compiler errors and they don't make any sense to me.

Jeremy
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: sort array of integers

Posted 28 September 2009 - 05:58 PM

View Postj_lauer, on 28 Sep, 2009 - 04:14 PM, said:

i tried that, it reverse sorts them from highest to lowest. I am looking to reverse the order they were entered without sorting. An example is if I enter these numbers 5.0 4.4 1.9 2.9 3.4 3.5 the output should be 3.5 3.4 2.9 1.9 4.4 5.0 along with listing the lowest number entered. I understand the sorting, but i don't think i need to sort it to get the output.

Thanks
Jeremy

sorry if my post was confusing

That's exactly what I said
DO NOT SORT THEM

Arrays.sort(Nos);
Was This Post Helpful? 0
  • +
  • -

#13 j_lauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-September 09

Re: sort array of integers

Posted 28 September 2009 - 07:05 PM

View Postpbl, on 28 Sep, 2009 - 04:58 PM, said:

View Postj_lauer, on 28 Sep, 2009 - 04:14 PM, said:

i tried that, it reverse sorts them from highest to lowest. I am looking to reverse the order they were entered without sorting. An example is if I enter these numbers 5.0 4.4 1.9 2.9 3.4 3.5 the output should be 3.5 3.4 2.9 1.9 4.4 5.0 along with listing the lowest number entered. I understand the sorting, but i don't think i need to sort it to get the output.

Thanks
Jeremy

sorry if my post was confusing

That's exactly what I said
DO NOT SORT THEM

Arrays.sort(Nos);


Got it, I must have missed that on you last post. Now I need to tackle the "double int" part of the problem so I can use decimal point with the entries. Any idea how I could pick out the lowest entered number?

Thanks for all your help so far.
Jeremy
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: sort array of integers

Posted 28 September 2009 - 07:25 PM

May be something like that ?
int lowest = iNos[0];
		for(int iNos=1; iNos<Nos.length;iNos++)
		{
			if(iNos[i] < lowest)
			   lowest = iNos[i];
		}


Was This Post Helpful? 0
  • +
  • -

#15 j_lauer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 06-September 09

Re: sort array of integers

Posted 28 September 2009 - 07:43 PM

View Postpbl, on 28 Sep, 2009 - 06:25 PM, said:

May be something like that ?
int lowest = iNos[0];
		for(int iNos=1; iNos<Nos.length;iNos++)
		{
			if(iNos[i] < lowest)
			   lowest = iNos[i];
		}




Not sure where to add it. I added it to the end after the output message and created many new compiler errors.....does it have to go before the output statement?
//Jeremy Lauer Assignment 5.2
import javax.swing.JOptionPane;	//used for java boxes
//import java.util.*;

public class sorting
{
	public static void main (String[]args)
	{
	   int [] Nos = new int[10];	//sets up the array quantity
	   
		for( int iNos=0; iNos<Nos.length;iNos++)	//establish the array length
		{
			Nos[iNos]=Integer.parseInt(JOptionPane.showInputDialog("Enter number:"));	//user entered data
		   

		}
	   
		//Arrays.sort(Nos);	//not used for this problem

		String output = "";	//setting up the output
		for( int i = 9; i > 0; i--)
			output += Nos[i] + ", ";
		output += Nos[0];	//output the order
	   
		JOptionPane.showMessageDialog(null, output);	//displaying the output
		  
	int lowest = iNos[0];
		for(int iNos=1; iNos<Nos.length;iNos++)
		{
			if(iNos[i] < lowest)
			   lowest = iNos[i];
		}
	JOptionPane.showMessageDialog(null, lowest);	//displaying the output
	}
}


Thanks
Jeremy
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2