Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basic Java program: coding error

Status
Not open for further replies.

tamkas

Programmer
May 18, 2007
5
0
0
CA
Hey guys,

I am very new in Java Program. I was trying to sort numbers in the program.

import java.io.*;
/*
* Main.java
*
* Created on May 19, 2007, 10:18 PM
*
/


public class SortNumbers{
public static void sort(double[] nums){
for (int i=0; i<nums.length; i++){
int min =i; // holds the index of the smallest element
for(int j=i; j<nums.length; j++)
{ if (num[j]<num[min])min = j;
}
// Now swap the smallest one with element i.
//This leaves all elements between 0 and i sorted.
double tmp;
tmp =nums;
nums=nums[min];
nums[min]=tmp;
}
}



public class Main {
/** Creates a new instance of Main */
public static void Main(String[] args) {
double[] nums = new double[10];
for(int i=0; i<nums.length; i++)
nums = Math.random() * 100;
sort(nums);
for (int i = 0; i<nums.length; i++)
System.out.println(nums);
}
}
}



I have got a following error:

class, interface or enum expected.

Thanks in advance.
 
Hi

Sorry to say, but your code looks bad. First of all, use an editor with syntax highlighting feature. That way you would avoid the first error : the comment was not closed, so extended over the first class. Second, indent your code consistently and post it between [tt][ignore]
Code:
[/ignore][/tt] and [tt][ignore]
[/ignore][/tt] tags to help us to read it.
Code:
import java.io.*;
/*
 * Main.java
 *
 * Created on May 19, 2007, 10:18 PM
 *
 [red]*[/red]/


class SortNumbers {
    public static void sort(double[] nums) {
        for (int i=0; i<nums.length; i++) {
            int min =i; // holds the index of the smallest element
            for(int j=i; j<nums.length; j++) {
                if (num[red]s[/red][j]<num[red]s[/red][min])min = j;
            }
            // Now swap the smallest one with element i.
            //This leaves all elements between 0 and i sorted.
            double tmp;
            tmp =nums[i];
            nums[i]=nums[min];
            nums[min]=tmp;
        }
    }
[red]}[/red]

public class Main {
    /** Creates a new instance of Main */
    public static void [red]m[/red]ain(String[] args) {
        double[] nums = new double[10];
        for(int i=0; i<nums.length; i++)
            nums[i] = Math.random() * 100;
        [red]SortNumbers.[/red]sort(nums);
        for (int i = 0; i<nums.length; i++)
            System.out.println(nums[i]);
    }
}

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top