Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 109,153 Java Programmers for FREE! Ask your question and get quick answers from experts. There are 1,137 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Problem calling program with parameters

 
Reply to this topicStart new topic

Problem calling program with parameters

freshoreo
post 1 Jul, 2008 - 02:59 PM
Post #1


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 11

I'm as tired of asking for help as you people are giving it. My professor gave us a brief overview of calling methods in the same file, but never went into depth on calling from another file. Then of course he gives us an assignment where we are to use 2 files and I can't get in contact with him. here is the code from my 1st (main) file:

CODE

import java.util.*;

public class NumberConvert
{
    public static void main(String args[])
    {
        System.out.println("Welcome to the Number Converter");
        System.out.println("");
        
        Scanner sc = new Scanner(System.in);
        int userNumber = 0;
        int choice = 0;
        
            System.out.println("Please enter a number between 1 and 100: ");
            userNumber = sc.nextInt();
            
            System.out.println("What would you like to convert your number to?");
            System.out.println("Type \"1\" for Binary \rType \"2\" for Octal \rType \"3\" for Hexadecimal");
            System.out.print("Your choice: ");
            choice = sc.nextInt();
            
            if (choice == 1)
            {
                Calculate.getBinary(userNumber);
            }
            else if (choice == 2)
            {
                Calculate.getOctal(userNumber);
            }
            else if (choice == 3)
            {
                Calculate.getHexadecimal(userNumber);
            }
            else
            {
                System.out.println("Invalid choice! Try again.");
            }
    }

}


And here is the file I am trying to call from:

CODE

public class Calculate
{
    public void getBinary(int userNumber)
    {
        System.out.print("The original number was " + userNumber + " and the binary converted number is " + Integer.toBinaryString(userNumber));
    }
    public void getOctal(int userNumber)
    {
        System.out.print("The original number was " + userNumber + " and the octal converted number is " + Integer.toOctalString(userNumber));
    }

    public void getHexadecimal(int userNumber)
    {
        System.out.print("The original number was " + userNumber + " and the hexadecimal converted number is " + Integer.toHexString(userNumber));
    }
}


Any help is useful. Thank you!!

-Spencer
User is offlineProfile CardPM

Go to the top of the page


Martyr2
post 1 Jul, 2008 - 03:29 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 4,318



Thanked 80 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Ok first make sure both files are in the same directory. Then for each of your methods in the Calculate class add the word "static"

java

public class Calculate
{
// Notice the word "static" used before "void"
public static void getBinary(int userNumber)
{
System.out.print("The original number was " + userNumber + " and the binary converted number is " + Integer.toBinaryString(userNumber));
}

// Again we use "static"
public static void getOctal(int userNumber)
{
System.out.print("The original number was " + userNumber + " and the octal converted number is " + Integer.toOctalString(userNumber));
}

// Last static again
public static void getHexadecimal(int userNumber)
{
System.out.print("The original number was " + userNumber + " and the hexadecimal converted number is " + Integer.toHexString(userNumber));
}
}


So we added this word because you are attempting to call the methods of this class without first creating an instance of the "Calculate" class. Normally if you wanted to call this as an instance of the class, you would use the new keyword to create an instance and then call the methods of that instance like so...

java

// Create an instance
Calculate myCalculateClass = new Calculate();

// Call the method of that new instance variable
myConvertClass.getHexadecimal(5);


But since your code doesn't need to have an instance related with it, you can make your methods static and call them using the class name, like you are.

Just add the "static" to your methods and you are good to go. For more information look up static methods and it will tell you more about how they are used.

Good luck!

"At DIC we be static code ninjas... so static sometimes we don't even move. Sucks for dodging bullets." decap.gif

This post has been edited by Martyr2: 1 Jul, 2008 - 03:31 PM
User is offlineProfile CardPM

Go to the top of the page

pbl
post 1 Jul, 2008 - 04:26 PM
Post #3


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 1,909



Thanked 113 times

Dream Kudos: 75
My Contributions


QUOTE(Martyr2 @ 1 Jul, 2008 - 03:29 PM) *

"At DIC we be static code ninjas... so static sometimes we don't even move. Sucks for dodging bullets." decap.gif

Lol biggrin.gif biggrin.gif biggrin.gif
User is offlineProfile CardPM

Go to the top of the page

freshoreo
post 2 Jul, 2008 - 09:34 AM
Post #4


New D.I.C Head

*
Joined: 12 Jun, 2008
Posts: 11

Thank you!!!
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/5/08 05:01PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month