Start New Discussion within our Software Development Community

this is a far stretch but i found this fourm online and i thought i'd give it a shot since i dont know where else to turn to...

I tried my best to implement the array into ascending order but i'm not sure if it's correct. I needed it to read in the values from the user but also i need the values that were read to be ascending order from low to high values. Can someone please help and let me know if i've done something wrong?


Create a list of temperatures (highest) of each day, arranged in an ascending order. The list contains maximum 10 records.
This list must be implemented using an array.
The output consists of: the original order in which the items were read in and the sorted output.

import java.util.*;

public class Temperature()
{
public static void Sort()
{
int[] a;
int temp;
java.util.Arrays.sort(a); 

for (int i=0; i<(a.length; i++)
{
temp = a[i];
a[i] = a[a.length - i – 1];
a[a.length – i – 1] = temp;
}

public static void main(String[] args) 
{
Scanner in = new Scanner(System.in);
Int[] temps = new int[10];
for (int i=0; i<temps.length; i++)
{
System.out.println(“Enter Temperature For The Day: “);
temps[i] = scan.nextint();
}
System.out.println(“The Temperature For The Start:” + temps[i] + “”);
Sort();
}

First problem:
In main you create an array "temps" and put the data into it, in sort you have a different array "a" that you (try to) sort, but there's no connection between the two arrays!
One solution would be to declare and initialise a single array that both methods can share - ie just declare it inside the class but outside any method.
That's not the only problem, but this will get you moving in the right direction.

ps: fix your compiler errors before posting, unless you really don't understand the message, in which case post the code & message so we can help.

This article has been dead for over six months. Start a new discussion instead.