DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Help!

  1. #1
    Join Date
    May 2004


    Hey, I'm fairly new to java. I took a year of comp-sci last year, but thanks to a teacher who didn't himself know java, I can't say I learned much. This year, I have to write a program using "methods", something I never learned last year. I've gotten sorta far by looking at other programs, but I still need help.

    /*Computer Science II ProgramOne

    Write a program with ten names.
    Write two methods to sort the names.
    One is a bubble sort and the other a selection sort.
    The information on the selection sort is on page 324 of the
    text. Analyze the two sort to determine which is more efficient.

    Write the analysis in terms of time and or operations.

    import TerminalIO.KeyboardReader;
    public class cs5470497p1{
    public static void main (String [] args){
    KeyboardReader reader = new KeyboardReader ();

    System.out.println("\t\t\tJordan Jones \n\t\t\t5470497\n\t\t\tsorting program #1\n\t\t\t2004/2005\n\n");

    String [] name = new String [10+1];
    int x;

    //~~GET DATA~~//
    for(int j=1; j <= 10;j++){
    name[j] = reader.readLine("Enter Your Name for user "+j+": ");

    x=reader.readInt("Sort with bubble(1) or selection(2)? ");

    if(x == 1)
    if(x == 2)

    public static void bubble(String [] name) {

    String low = " ";
    //sort by names
    for(int j=1;j <= 10-1; j++)
    for(int i=1;i <= 10-j; i++){
    //sort strings
    if(name[i].compareTo(name[i+1]) > 0 ){
    //switch strings
    low = name[i];
    name[i] = name[i+1];
    name[i+1] = low;


    public static void selection(String [] name) {
    for(int i = 0; i< names.length-1; i++){
    int minIndex = findMinimum(names, i);
    if (minIndex != i)
    swap(names, i, minIndex);

    for(int j=1; j <= 10;j++)
    System.out.println( name[j]);


  2. #2
    Join Date
    Feb 2004
    Well what is it that you don't understand about methods?

  3. #3
    Join Date
    Feb 2004
    Oh, you can't ever have written a usable program without at least one method. You can't run any program without a method with the signature
    public static void main(String[] args)
    so you probably know more about methods than you think.

  4. #4
    Join Date
    May 2004
    Yea, I have been learning, but for some reason, I still don't understand. Something with the methods doesn't work. Am I doing the right "starter" for the method? I don't really really know what I'm doing, I'm just scraping by with copy>paste. I'm getting errors (sorry I can't be specific, I'm on diff comp w/o codewarrior), and I'm not even getting to the point where I see any output. Does anybody know what I am doing wrong? (yes, I set the java target).

    Oh and you can make programs without methods it's just no practical. Last summer I made a working text version of blackjack using no methods. 500 lines of code and too many hours of work though

  5. #5
    Join Date
    Mar 2004
    A method is the java term for "function" basically.

    //this makes a new class with the "Classname" you give it

    public class Classname


    //with the following code contained within the brackets from the above example:

    public int getX()
    return 42;

    public void showHello()
    System.out.prinln("Hello World");

    All those above are methods.
    And object is an instance of that class and can call any of those methods.

    Classname objectName = new Classname();

    int Y = objectName.getX();


    A special method having the same name as the class and has no return type defined for it, is called a constructor. That method is executed as soon as an object of that class is created.
    I hope that clarified some things for ya.

  6. #6
    Join Date
    Feb 2004
    I assure you that you can't write a runnable java program without at least one method. When the JVM loads your class it calls a method to run it, it calls the main method that I mentioned above. That is a method just like any other, there's nothing at all special about it

    Ok, after reading your code I see that your problem is that you're attempting to define one method inside of another. Methods cannot be defined in other methods, they must be defined at the class level. The means that they the only brackets that surround them are the one at the start of the class, and the one at the end of the class.

    Ps, it helps to read it if you format the code properly. Use the code tags [ code ] and [ /code ] (without the spaces inside the square brakets).

    It will then look something like this.
        You can indent things
                 easily :)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.