Can any help me with this code, im trying to get the program to return to a function. I have no idea how this is done. Give a lil explanation so I know for next time. Thanks here the code :

// The "Calculator" class.
import java.awt.*;
import hsa.Console;


public class Test
{
static Console c;


static public void main (String [] args)
{


c = new Console ();
int choice;


c.println ("choose one of the following");
c.println ("add ...1");
choice = c.readInt ();


if (choice == 1)
{
add ();  // trying to go down to add function



}



static void int add (int a1, int a2, int total) // add function
{
c.println ("enter a number ");
a1 = c.readInt ();
c.println ("enter another number ");
a2 = c.readInt ();
total = a1 + a2;
c.println (total);
main (); // trying to go back to main
}
}

you can return primitive data types and objects... not functions

i think i see what u mean... the focus will automatically return to main, so there is no need to call main(); it will continue from where it left off.

Alright then how would I do focus or wutever . ? Because im trying to make a calculator that will add , multiply etc . I new at java and every code ive written never I never called any function , my program was just continual , like as th program was run , the program would just be read line after line. Now that im written bigger programs i need to know how to seperate my methods or functions etc.

The old method of doing this is as follows:

static int getMenuChoice(Console c)
{
	c.println("Choose an option");
	c.println("");
	c.println("1. Add");
	c.println("2. Subtract");
	c.println("3. Multiply");
	//and so on
	c.println("0. Exit");
	
	return c.readInt();
}

public static void main(String args[])
{
	Console c = new Console();
	
	int choice = getMenuChoice(c);
	
	while(choice != 0)
	{
		if(choice == 1)
		{
			add();
		}
		else if(choice == 2)
		{
			subtract();
		}
		//and so on
		else
		{
			c.println("Invalid selection");
		}
		
		choice = getMenuChoice(c);
	}
}

There are newer methods such as using event handlers, but I don't think you are ready to get into all of that.

Ok this is how far i got can anyone finish it , i give up.

import java.awt.*;
import hsa.Console;


public class Calculator
{
//    static Console c;


static public int getMenuChoice(Console c)
{
c.println("Choose an option");
c.println("");
c.println("1. Add");
c.println("2. Subtract");
c.println("3. Multiply");
//and so on
c.println("0. Exit");


return c.readInt();
}


public static void main(String args[])
{
Console c = new Console();


int choice = getMenuChoice(c);


while(choice != 0)
{
if(choice == 1)
{
return add ();
}
else if(choice == 2)
{
//subtract();
}
//and so on
else
{
c.println("Invalid selection");
}


choice = getMenuChoice(c);
}
}
static public int add (int a1 , int a2)
{
Console c = new Console();  // dont know if this is required
c.println ("hello"); // just testing to see if it works.
return  a1 * a1;



}
}

Why are you doing this:

return add ();

If you execute a return while in the main subroutine, you stop the execution of the program.

And don't forget that returning a value from a void method will result in a compiler error in Java (unlike C).

Ya guys im not sure why am using the things im using like return a1 * a1 or wutever , that 's why im asking others that have more knowledge than me. I think I have a liltle understanding of it now. All i need to know now is how to (return to public static main ) function

Alright guys i fugured it out for those who are also having problems.

import java.awt.*;
import hsa.Console;




public class Calculator
{
static Console a = new Console ();     
static Console c = new Console () ; 


     public static int number ; 
     public static int number_2;
     public static int total ;
     static public int getMenuChoice(Console c) 

{
         a.setSize (200 , 200);
         a.setLocation ( 400 , 0);
         c.setSize (225 , 600 );
         c.setLocation (0,0) ;
         c.println ("enter a number");
         number = c.readInt ();   
         c.println("Choose an option");
         c.println("");
         c.println("1. Add");
         c.println("2. Subtract");
         c.println("3. Multiply");
         c.println("4. Divide ");
         c.println("0. Exit");

      return c.readInt();
}

         public static void add () 
        {
        c.println ("Enter number");
        number_2 = c.readInt ();
        total = number + number_2 ;
        a.println ( number + " + " + number_2 + " = " + total);
        c.clear (); 
        getMenuChoice (c);
        }

     public static void subtract ()
     {
        c.println ("Enter number");
        number_2 = c.readInt ();
        total = number - number_2 ;
        a.println ( number + " - " + number_2 + " = " + total);
        c.clear (); 
        getMenuChoice (c);
        } 
          public static void multiply ()
       {
        c.println ("Enter number");
        number_2 = c.readInt ();
        total = number * number_2 ;
        a.println ( number + " * " + number_2 + " = " + total);
        c.clear (); 
        getMenuChoice (c);
}

     public static void divide ()
       {
        c.println ("Enter number");
        number_2 = c.readInt ();
        total = number * number_2 ;
        a.println ( number + " / " + number_2 + " = " + total);
        c.clear (); 
        getMenuChoice (c);
}




public static void main(String args[])
{


        int choice = getMenuChoice(c);
        choice = c.readInt ();

         while(choice != 0)
        {
                if(choice == 1)
                {
                add ();    
                c.clear ();

                }
                else if(choice == 2)
                {
                        subtract();
                }

                else if (choice == 3)
                {
                    multiply ();
                }
                else if (choice == 4)
                {
                  divide();
                }

                //else if (choice == 5)
                //{



                else
                {
                 c.println("Invalid selection");
                }


                }


               }
            }

my program was just continual , like as th program was run , the program would just be read line after line. Now that im written bigger programs i need to know how to seperate my methods or functions etc.

Functions (or methods as they're called in Java) alone make no difference between OO(object-oriented) and procedural (your "continual") languages.

Focus is just saying which component can recieve user events. When typing keys in your web browser, they won't do anything unless the address bar has focus. By having the current focus, it can accept those keystrokes and then display them in the text field(address bar).

I'm just trying to help you understand what you're programming.

Ok i have another problem with calling these methods. Now im trying to do somewhat the same thing , trying to call on a method or function but now im using throws IOException to inpu and output files. How would I do this .
Here the code:

import java.awt.*;
import java.io.*;
import hsa.Console;

public class Chapter7 
{
    static Console c = new Console ();           // The output console


    static public int getMenuChoice(Console c)
{
        c.println("Choose an option");
        c.println("");
        c.println("1.Exercise..1");
        c.println("Exit........0");

        return c.readInt();
}
public static void main (String[] args) 
    {
     int choice = getMenuChoice(c);


        while(choice != 0)
        {
                if(choice == 1)
                {
                  exercise1();  // this is were i need to               call on exercise 1 not sure on how to do this.
                 }
                  else
                 {
                c.println("Invalid selection");
                }
                choice = getMenuChoice(c);
        }
            }

        public static void exercise1 () throws IOException 
        {
        String fileName = "a:\\ages.txt" ;
        String line ;
        BufferedReader input ;
        input = new BufferedReader (new FileReader (fileName)); 
        line = c.readLine ();

        while (line != null)
        {
         line = input.readLine();
         getMenuChoice(c);

    } 

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.