• Log in or sign up for an account here: https://www.sythe.org/login

Need help with array

d great one

Apprentice
Joined
Nov 4, 2007
Sythe Gold
0
Messages
930
This is my main program. Everything is fine before calling the method.
//this is the main class

import java.io.*;
class mainko{
public static void main(String args[])throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int[] number=new int[5];

for(int x=0;x<5;x++){
System.out.println("Enter 5 integers: ");
number[x]=Integer.parseInt(br.readLine());
}

System.out.print("Number in array are: ");

for(int x=0;x<5;x++){
System.out.println(number[x]+" ");
}
sorting prog=new sorting();

prog.sort(number);//need help calling the method


}
}


This is the main problem. I don't know how to put mathematical operation in an array.
The method should sort the input integers.
In example, if I input 5, 8, 9, 4,6 the method will sort all the integers and display the lowest first then to the highest integer.

//problem lies in creating the method here
class sorting{

public void sort(){

int[] array=new int[5];

}
}


Need help please. Thanks!
 
Code:
public class Sorting
{
	public int[] Sorting(int sort)
	{
		Array.sort(sort)
	}
}

that should work, unless you need to make you're own method if so, i can try to work something out for you.
 
Code:
public class Sorting
{
	public int[] Sorting(int sort)
	{
		Array.sort(sort)
	}
}

that should work, unless you need to make you're own method if so, i can try to work something out for you.
You should have int[] sort as your parameter..? As you are taking an array of input and just returning it back sorted.
 
ya u rite mi bad i was in a rush

int[] sorted = new int[5]; int compare; for(int x = 0; x< 5; x++) { compare = number[x]; for(int y = 0; y<5; y++){if(compare > number[y]){compare=number[y];} sorted[x] = compare;} }

i thnk dat shuld sort ur number array into a new sorted array n inkase u kant tel its a bubble sort. it shuld work tho.
 
Runescape Gamblingrunescape gambling
Back
Top