Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 21-06-2008, 08:01 PM   #1 (permalink)
G.A.M.E.R
 
Shloeb's Avatar
 
Join Date: Jan 2007
Location: Bangalore
Posts: 403
Default goto statement in JAVA


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
Code:
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?
__________________
Core2Duo E7200 @ 3.16 Ghz| ABIT IP35-E | 2GB Transcend 800Mhz | GeCube ATI HD 4850 | 250 GB Western Digital | Corsair VX450W

Dell Studio 1558
Shloeb is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 21-06-2008, 08:14 PM   #2 (permalink)
Wahahaha~!
 
Faun's Avatar
 
Join Date: Dec 2006
Location: Pune/there
Posts: 7,676
Default Re: goto statement in JAVA

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).
__________________
Blog | Flickr | Battlelog
Spoiler:
Asus Z68 V-Pro|i5 2500k|TRUE Black|Ripjaws X|U2311H|N560GTX|D7000|XONAR STX|RE272|RE0|CC51|XE200PRO Walnut| TD II V2| Ultraphile|N5800

Mono
Faun is offline  
Old 21-06-2008, 08:33 PM   #3 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: goto statement in JAVA

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)

Code:
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.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 21-06-2008, 08:45 PM   #4 (permalink)
G.A.M.E.R
 
Shloeb's Avatar
 
Join Date: Jan 2007
Location: Bangalore
Posts: 403
Default Re: goto statement in JAVA

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?
__________________
Core2Duo E7200 @ 3.16 Ghz| ABIT IP35-E | 2GB Transcend 800Mhz | GeCube ATI HD 4850 | 250 GB Western Digital | Corsair VX450W

Dell Studio 1558
Shloeb is offline  
Old 21-06-2008, 09:03 PM   #5 (permalink)
18 Till I Die............
 
Join Date: Jul 2004
Location: India, Mumbai, Marine Lines
Posts: 5,792
Default Re: goto statement in JAVA

Qwerty, why not different error codes for different errors? that's much better too.
__________________
http://www.bash.org/?258908
mehulved is offline  
Old 21-06-2008, 09:28 PM   #6 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: goto statement in JAVA

Quote:
Originally Posted by Shloeb View Post
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
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 22-06-2008, 06:37 PM   #7 (permalink)
G.A.M.E.R
 
Shloeb's Avatar
 
Join Date: Jan 2007
Location: Bangalore
Posts: 403
Default Re: goto statement in JAVA

Can u help me on how to initialize a string?
__________________
Core2Duo E7200 @ 3.16 Ghz| ABIT IP35-E | 2GB Transcend 800Mhz | GeCube ATI HD 4850 | 250 GB Western Digital | Corsair VX450W

Dell Studio 1558
Shloeb is offline  
Old 22-06-2008, 07:15 PM   #8 (permalink)
18 Till I Die............
 
Join Date: Jul 2004
Location: India, Mumbai, Marine Lines
Posts: 5,792
Default Re: goto statement in JAVA

Quote:
Originally Posted by QwertyManiac View Post
@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?
__________________
http://www.bash.org/?258908
mehulved is offline  
Old 22-06-2008, 11:50 PM   #9 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: goto statement in JAVA

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.

Code:
import java.io.*;
class bank
{
	StringBuffer name,actype;
	int acno,bal,amt;
	
	bank()
	{
		name=new StringBuffer("Satinder Pal Singh"); //No need to specify class again
		acno=101023;
		actype=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.");
			}
		}
	}
}
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 23-06-2008, 12:52 AM   #10 (permalink)
In The Zone
 
Join Date: Oct 2006
Location: Mumbai
Posts: 365
Default Re: goto statement in JAVA

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 http://java.sun.com/docs/codeconv/ht...ions.doc8.html
__________________
Dhiraj Thakur
thakur.dheeraj(@)gmail.com
Desi-Tek.com is offline  
Old 23-06-2008, 02:03 AM   #11 (permalink)
18 Till I Die............
 
Join Date: Jul 2004
Location: India, Mumbai, Marine Lines
Posts: 5,792
Default Re: goto statement in JAVA

Quote:
Originally Posted by QwertyManiac View Post
Isn't 1 non-zero?
It's a value, not values
__________________
http://www.bash.org/?258908
mehulved is offline  
Old 23-06-2008, 09:31 AM   #12 (permalink)
G.A.M.E.R
 
Shloeb's Avatar
 
Join Date: Jan 2007
Location: Bangalore
Posts: 403
Default Re: goto statement in JAVA

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.
__________________
Core2Duo E7200 @ 3.16 Ghz| ABIT IP35-E | 2GB Transcend 800Mhz | GeCube ATI HD 4850 | 250 GB Western Digital | Corsair VX450W

Dell Studio 1558
Shloeb is offline  
Old 23-06-2008, 04:40 PM   #13 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: goto statement in JAVA

Quote:
Originally Posted by mehulved View Post
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.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 23-06-2008, 08:09 PM   #14 (permalink)
G.A.M.E.R
 
Shloeb's Avatar
 
Join Date: Jan 2007
Location: Bangalore
Posts: 403
Default Re: goto statement in JAVA

I have made this program.
Code:
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.
__________________
Core2Duo E7200 @ 3.16 Ghz| ABIT IP35-E | 2GB Transcend 800Mhz | GeCube ATI HD 4850 | 250 GB Western Digital | Corsair VX450W

Dell Studio 1558
Shloeb is offline  
Old 23-06-2008, 08:29 PM   #15 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: goto statement in JAVA

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

Code:
import java.io.*;
class bank
{
    String s,actype;
    int acno,bal,amt;
    
    bank()
    {
        // You have already declared them, you just have to assign.
        // DONT REDECLARE AS STRING. It makes it local.
        s="Satinder";
        acno=101023;
        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.");
            }
        }
    }
}
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 23-06-2008, 09:23 PM   #16 (permalink)
G.A.M.E.R
 
Shloeb's Avatar
 
Join Date: Jan 2007
Location: Bangalore
Posts: 403
Default Re: goto statement in JAVA

Oops!! I am really sorry. I didn't notice that. U have been of great help. Thanks.
__________________
Core2Duo E7200 @ 3.16 Ghz| ABIT IP35-E | 2GB Transcend 800Mhz | GeCube ATI HD 4850 | 250 GB Western Digital | Corsair VX450W

Dell Studio 1558
Shloeb is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help for the following statement nitinm Programming 4 05-03-2008 08:42 PM
':' turning into label statement redhat Programming 13 04-02-2008 12:25 AM
What is a null statement in C language? fun2sh Programming 11 13-07-2007 07:36 PM
Clarification of a Problem Statement... Important. Thor QnA (read only) 1 28-09-2006 07:44 PM
goto command for shell scripting? paragkalra Open Source 1 24-07-2006 10:33 AM

 
Latest Threads
- by Sujeet
- by clmlbx
- by Sujeet
- by icebags

Advertisement




All times are GMT +5.5. The time now is 11:04 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2