# goto statement in JAVA



## Shloeb (Jun 21, 2008)

I have just started java classes 4 days ago. And i have been  given an assignment. To make that assignment i have to use a command similar to goto statement. 
*Assignment is:*

Design a class to represent a bank account
*Data types*
-Name of the depositor
-Account number
-Type of account
-Balance amount in the account

*Methods*
-To assign initial values
-To deposit an amount
-To withdraw an amount after checking balance
-To display the name & balance

I have made this program

```
import java.io.*;
class bank
  {
      String name,actype;
      int acno,bal,amt;
bank()
  {
      StringBuffer name=new StringBuffer("Satinder Pal Singh");
      acno=101023;
      StringBuffer actype=new StringBuffer("Savings");
      bal=10000;
  }
void dep()throws IOException
  {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.print("What amount you want to deposit:");
      amt=Integer.parseInt(br.readLine());
      bal=bal+amt;
  }
void wdraw()throws IOException
  {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Your Balance="+bal);
      System.out.print("What amount you want to withdraw:");
      amt=Integer.parseInt(br.readLine());
      bal=bal-amt;
      if(bal<amt)
  {
      System.out.println("Not sufficient balance");
      //xyz;
  }
  }
void disp()
  {
      System.out.println("Name:"+name);
      System.out.println("Balance:"+bal);
  }
void dbal()
   {
      System.out.println("Balance:"+bal);
   }
   }
class mgmt
  {
      public static void main(String ar[])throws IOException
  {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  
      int n;
      bank b1=new bank();
      b1.disp();
      xyz: System.out.println("To withdraw press 1");
           System.out.println("To deposit press 2");
           n=Integer.parseInt(br.readLine());
      if(n==1)
  {
      b1.wdraw();
      b1.dbal();
  }
      else if(n==2)
  {
      b1.dep();
      b1.dbal();
  }
      else
  {
      System.out.println("Please enter a valid choice");
      //xyz;
  }
  }
  }
```
The main problem here is that i need a command to jump to label xyz. As i am new to JAVA so i don't know. goto was supported by earlier java compilers but not know. So can anyone give me an alternative?


----------



## Faun (Jun 21, 2008)

use conditional constructs, goto is ditched as its non conforming to the standards

Think a little in a logical way, u should be done with it in any language (c, c++ or xyz).


----------



## QwertyManiac (Jun 21, 2008)

Always remember to use an infinite loop and return statements for continuous service applications. No, its not like all infinite loops are bad, your telephone network works all the time doesn't it? Thats a great application of the "infinity". 

Here's a modified version that doesn't necessitate the use of goto, and also returns statuses of the operations that occurred (1 if there was an error in deposit() or withdraw(), else 0 if all went fine)


```
import java.io.*;
class bank
{
	String name,actype;
	int acno,bal,amt;
	
	bank()
	{
		StringBuffer name=new StringBuffer("Satinder Pal Singh");
		acno=101023;
		StringBuffer actype=new StringBuffer("Savings");
		bal=10000;
	}
	
	int dep()throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.print("What amount you want to deposit:");
		amt=Integer.parseInt(br.readLine());
		if (amt<0)
		{
			System.out.println("Amount can't be negative.");
			return 1;
		}
		bal=bal+amt;
		return 0;
	}
	
	int wdraw()throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Your Balance="+bal);
		System.out.print("What amount you want to withdraw:");
		amt=Integer.parseInt(br.readLine());
		if(bal<amt)
		{
			System.out.println("Not sufficient balance.");
			return 1;
		}
		if(amt<0)
		{
			System.out.println("Amount can't be negative.");
			return 1;
		}
		bal=bal-amt;
		return 0;
	}
	
	void disp()
	{
		System.out.println("Name:"+name);
		System.out.println("Balance:"+bal);
	}
	
	void dbal()
	{
		System.out.println("Balance:"+bal);
	}
}

class mgmt
{
	public static void main(String args[])throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

		int n;
		bank b1=new bank();
		b1.disp();
		while(true)
		{
			System.out.println("To withdraw, press 1");
			System.out.println("To deposit, press 2");
			System.out.println("To exit, press 3");
			n=Integer.parseInt(br.readLine());
			if(n==1)
			{
				b1.wdraw();
				b1.dbal();
			}
			else if(n==2)
			{
				b1.dep();
				b1.dbal();
			}
			else if(n==3)
			{
				break;
			}
			else
			{
				System.out.println("Please enter a valid choice.");
			}
		}
	}
}
```

P.s. There are other bugs in there you might want to check, like it doesn't display the name string properly, but that's for you to fix.


----------



## Shloeb (Jun 21, 2008)

If a function returns zero then it means true is returned in while loop? Another thing is that i knew about that string bug but i forgot to mention it. Can u tell me how to initialize a string?


----------



## mehulved (Jun 21, 2008)

Qwerty, why not different error codes for different errors? that's much better too.


----------



## QwertyManiac (Jun 21, 2008)

Shloeb said:


> If a function returns zero then it means true is returned in while loop? Another thing is that i knew about that string bug but i forgot to mention it. Can u tell me how to initialize a string?



No no, it does not have anything to do with the infinite loop *while (true)*. Its just a good approach to functions, that it returns a status code. 

When you scale this application to higher grounds, you will find it easy to get statuses of function calls after they terminate and based on their return code you can handle the errors/progresses.

It is not directly related to your program as such, just added in for learning.

If the function call would look like:

*status = object.deposit();*

Then when the function finally returns, the status integer variable can hold the return code and you could decide what to do with the number returned right? Hope you get what I wish to imply. Like if its returned 0, all if fine, lets proceed, else show some help and ask user to re-input, or so. For now those messages like "Amount is less than 0" are printed in the function, but this is not the usual way to do it, you can set a return code and debug it outside the function call this way.

When you progress through Java, you will find that handling these kind of exceptions are better via try-catch blocks, but thats an advanced topic for now (Assuming you have just started learning of course, maybe you have heard of it)

Also, you notice the *break;* statement in the 3'rd option? This one is used to terminate the infinite loop. Unconditional loops ( *while (true)* does not have a clear condition as you noticed ) can't be broken by any other method than returns or breaks.

@mehulved - Yes one could do that but I'd then have to build a mapping for the debug "messages" of each different return code so that its easy to read them than to read code to find out what the number means. For a small program as this, a simple binary form *return Good or Bad*, works. You scale it now


----------



## Shloeb (Jun 22, 2008)

Can u help me on how to initialize a string?


----------



## mehulved (Jun 22, 2008)

QwertyManiac said:


> @mehulved - Yes one could do that but I'd then have to build a mapping for the debug "messages" of each different return code so that its easy to read them than to read code to find out what the number means. For a small program as this, a simple binary form *return Good or Bad*, works. You scale it now


But wouldn't that be a better approach in most cases? And if you wanted a boolean for you could do 0 and non-zero values na?


----------



## QwertyManiac (Jun 22, 2008)

Isn't 1 non-zero?

@Sholeb

StringBuffer is a mutable string class if I remember right, why would you want to use a mutable string for storing names, which are usually constant?

But anyway what your mistake here is that you have declared name and actype as String class in the beginning and then have created another variable name as StringBuffer class, which thus gets assigned as local to the constructor (Wtf?). Fixing either one of the types to the same, fixes the issue, there's nothing wrong with the declaratory syntax.


```
import java.io.*;
class bank
{
	[B]StringBuffer[/B] name,actype;
	int acno,bal,amt;
	
	bank()
	{
		[B]name[/B]=new StringBuffer("Satinder Pal Singh"); //No need to specify class again
		acno=101023;
		[B]actype[/B]=new StringBuffer("Savings"); //No need to specify class again
		bal=10000;
	}
	
	int dep()throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.print("What amount you want to deposit:");
		amt=Integer.parseInt(br.readLine());
		if (amt<0)
		{
			System.out.println("Amount can't be negative.");
			return 1;
		}
		bal=bal+amt;
		return 0;
	}
	
	int wdraw()throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Your Balance="+bal);
		System.out.print("What amount you want to withdraw:");
		amt=Integer.parseInt(br.readLine());
		if(bal<amt)
		{
			System.out.println("Not sufficient balance.");
			return 1;
		}
		if(amt<0)
		{
			System.out.println("Amount can't be negative.");
			return 1;
		}
		bal=bal-amt;
		return 0;
	}
	
	void disp()
	{
		System.out.println("Name:"+name);
		System.out.println("Balance:"+bal);
	}
	
	void dbal()
	{
		System.out.println("Balance:"+bal);
	}
}

class mgmt
{
	public static void main(String args[])throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

		int n;
		bank b1=new bank();
		b1.disp();
		while(true)
		{
			System.out.println("To withdraw, press 1");
			System.out.println("To deposit, press 2");
			System.out.println("To exit, press 3");
			n=Integer.parseInt(br.readLine());
			if(n==1)
			{
				b1.wdraw();
				b1.dbal();
			}
			else if(n==2)
			{
				b1.dep();
				b1.dbal();
			}
			else if(n==3)
			{
				break;
			}
			else
			{
				System.out.println("Please enter a valid choice.");
			}
		}
	}
}
```


----------



## Desi-Tek.com (Jun 23, 2008)

first letter  of the class name should start with capital letters
using small letter is not an error but not a best practice 

read this *java.sun.com/docs/codeconv/html/CodeConventions.doc8.html


----------



## mehulved (Jun 23, 2008)

QwertyManiac said:


> Isn't 1 non-zero?


It's a value, not values


----------



## Shloeb (Jun 23, 2008)

I didn't knew abt this srting buffer concept. I was searching for initializing a string an found this. But it was not actually for initializing a string. But i got confused so i just used this one thats all.


----------



## QwertyManiac (Jun 23, 2008)

mehulved said:


> It's a value, not values


Boolean is either this or that, not this or those 

@Sholeb - String a = "string"; should do in most cases. StringBuffer objects are used when the string has to be mutated at run time.


----------



## Shloeb (Jun 23, 2008)

I have made this program. 

```
import java.io.*;
class bank
{
	String s,actype;
	int acno,bal,amt;
	
	bank()
	{
		String s="Satinder";
		acno=101023;
		String actype="Savings";
		bal=10000;
	}
	
	int dep()throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.print("What amount you want to deposit:");
		amt=Integer.parseInt(br.readLine());
		if (amt<0)
		{
			System.out.println("Amount can't be negative.");
			return 1;
		}
		bal=bal+amt;
		return 0;
	}
	
	int wdraw()throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		System.out.println("Your Balance="+bal);
		System.out.print("What amount you want to withdraw:");
		amt=Integer.parseInt(br.readLine());
		if(bal<amt)
		{
			System.out.println("Not sufficient balance.");
			return 1;
		}
		if(amt<0)
		{
			System.out.println("Amount can't be negative.");
			return 1;
		}
		bal=bal-amt;
		return 0;
	}
	
	void disp()
	{
		System.out.println("Name:"+s);
		System.out.println("Balance:"+bal);
	}
	
	void dbal()
	{
		System.out.println("Balance:"+bal);
	}
}

class mgmt
{
	public static void main(String args[])throws IOException
	{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

		int n;
		bank b1=new bank();
		b1.disp();
		while(true)
		{
			System.out.println("To withdraw, press 1");
			System.out.println("To deposit, press 2");
			System.out.println("To exit, press 3");
			n=Integer.parseInt(br.readLine());
			if(n==1)
			{
				b1.wdraw();
				b1.dbal();
			}
			else if(n==2)
			{
				b1.dep();
				b1.dbal();
			}
			else if(n==3)
			{
				break;
			}
			else
			{
				System.out.println("Please enter a valid choice.");
			}
		}
	}
}
```
This is not working. Its still displaying null string.


----------



## QwertyManiac (Jun 23, 2008)

Why are you redeclaring the s and actype variables? Didn't I already say its not correct to do that?


```
import java.io.*;
class bank
{
    String s,actype;
    int acno,bal,amt;
    
    bank()
    {
        [B]// You have already declared them, you just have to assign.
        // DONT REDECLARE AS STRING. It makes it local.[/B]
        [B]s="Satinder";[/B]
        acno=101023;
        [B]actype="Savings";[/B]
        bal=10000;
    }
    
    int dep()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.print("What amount you want to deposit:");
        amt=Integer.parseInt(br.readLine());
        if (amt<0)
        {
            System.out.println("Amount can't be negative.");
            return 1;
        }
        bal=bal+amt;
        return 0;
    }
    
    int wdraw()throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Your Balance="+bal);
        System.out.print("What amount you want to withdraw:");
        amt=Integer.parseInt(br.readLine());
        if(bal<amt)
        {
            System.out.println("Not sufficient balance.");
            return 1;
        }
        if(amt<0)
        {
            System.out.println("Amount can't be negative.");
            return 1;
        }
        bal=bal-amt;
        return 0;
    }
    
    void disp()
    {
        System.out.println("Name:"+s);
        System.out.println("Balance:"+bal);
    }
    
    void dbal()
    {
        System.out.println("Balance:"+bal);
    }
}

class mgmt
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        int n;
        bank b1=new bank();
        b1.disp();
        while(true)
        {
            System.out.println("To withdraw, press 1");
            System.out.println("To deposit, press 2");
            System.out.println("To exit, press 3");
            n=Integer.parseInt(br.readLine());
            if(n==1)
            {
                b1.wdraw();
                b1.dbal();
            }
            else if(n==2)
            {
                b1.dep();
                b1.dbal();
            }
            else if(n==3)
            {
                break;
            }
            else
            {
                System.out.println("Please enter a valid choice.");
            }
        }
    }
}
```


----------



## Shloeb (Jun 23, 2008)

Oops!! I am really sorry. I didn't notice that. U have been of great help. Thanks.


----------

