Results 1 to 16 of 16
Like Tree1Likes
  • 1 Post By jammas615

Thread: Weird Error With Methods

  1. #1
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Weird Error With Methods

    Okay so I've wrote part of a banking program but the withdrawl command does not work and it instead does the deposit command. I'm not sure why.
    Here's my code:
    Java Code:
    import java.util.Scanner;
    
    
    public class Bank_Main {
    	public static void main (String args[]){
    	System.out.println("Welcome to 1337 Bank Type ? for help");
    	
    	//Get User Input
    	double balance = 10;
    	Scanner scanner = new Scanner(System.in);
    	
    	
    	//Help Command
    	if (scanner.nextLine().equalsIgnoreCase("?"))
    	{
    		Help();
    	}
    	//Deposit Command
    	if (scanner.nextLine().equalsIgnoreCase("Deposit"));
    	{
    		Deposit(balance);
    	}
    	//Withdrawl Command
    	if (scanner.nextLine().equalsIgnoreCase("Withdrawl"));
    	{
    		Withdrawl(balance);
    	}
    		
    	
    	
    	
    	
    }
    
    	
    	private static void Withdrawl(double balance) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("How much money would you like to Withdrawl");
    		balance = balance - Integer.parseInt(scanner.nextLine());
    		System.out.println("New balance = " + balance + "$");
    	}
    
    	public static void Deposit(double balance) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("How much money would you like to deposit?");
    		balance = balance + Integer.parseInt(scanner.nextLine());
    		System.out.println("New balance = " + balance + "$");
    	}
    
    	public static void Help() {
    		System.out.println("Welcome to the help menu");
    		System.out.println("Command List:");
    		System.out.println("Deposit, Withdrawl, Transfer, NewAccount, Balance");
    		System.out.println("--------------------------------------------");
    		
    	}
    	
    	
    	
    }
    All help is appreciated!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Weird Error With Methods

    You're calling scanner.nextLine() multiple times when you don't want to since you're doing your scanner reading in each if conditional statement; if you walk through your program's logic mentally, you'll see what I mean. Better to read from the scanner once and store the result into a String before all your if blocks, and then use that String in your if conditional statements.

  3. #3
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Weird Error With Methods

    Write your scanner.nextline() to a string and then compare it with equals().
    Because equalsIgnoreCase() returns true or false.

    so:
    [CODE]
    String str = scanner.nextLine();
    if (str.equals("blah")
    {
    Desposit(balance);
    }

  4. #4
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Weird Error With Methods

    if (scanner.nextLine().equalsIgnoreCase("Deposit"));
    Why the semicolon?

    Write your scanner.nextline() to a string and then compare it with equals().
    Because equalsIgnoreCase() returns true or false.

    so:
    Java Code:
    String str = scanner.nextLine();
    if (str.equals("blah")
    {
    Desposit(balance);
    }
    Fubarable likes this.

  5. #5
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Weird Error With Methods

    Thanks for the responses everyone, My code is changed to:
    Java Code:
     import java.util.Scanner;
    
    
    public class Bank_Main {
    	public static void main (String args[]){
    	System.out.println("Welcome to 1337 Bank Type ? for help");
    	
    	//Get User Input
    	double balance = 10;
    	Scanner scanner = new Scanner(System.in);
    	String input = scanner.nextLine();
    	
    	//Help Command
    	if (input.equalsIgnoreCase("?"))
    	{
    		Help();
    	}
    	//Deposit Command
    	if (input.equalsIgnoreCase("Deposit"))
    	{
    		Deposit(balance);
    	}
    	//Withdraw Command
    	if (input.equalsIgnoreCase("Withdraw"))
    	{
    		Withdraw(balance);
    	}
    		
    	
    	
    	
    	
    }
    
    	
    	private static void Withdraw(double balance) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("How much money would you like to Withdraw");
    		balance = balance - Integer.parseInt(scanner.nextLine());
    		System.out.println("New balance = " + balance + "$");
    	}
    
    	public static void Deposit(double balance) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("How much money would you like to deposit?");
    		balance = balance + Integer.parseInt(scanner.nextLine());
    		System.out.println("New balance = " + balance + "$");
    	}
    
    	public static void Help() {
    		System.out.println("Welcome to the help menu");
    		System.out.println("Command List:");
    		System.out.println("Deposit, Withdrawl, Transfer, NewAccount, Balance");
    		System.out.println("--------------------------------------------");
    		
    	}
    	
    	
    	
    }
    But now I've got 1 more question. How would I allow the user to enter a second command after the first one is entered without a goto because goto is not in java?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Weird Error With Methods

    Quote Originally Posted by skaterboy987 View Post
    But now I've got 1 more question. How would I allow the user to enter a second command after the first one is entered without a goto because goto is not in java?
    There is a goto of sorts in Java but better not to use it. You would use a loop of some sort or another.

  7. #7
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Weird Error With Methods

    Sorry for all the questions but now I've got 1 more probelm

    Java Code:
    import java.util.Scanner;
    
    
    public class Bank_Main {
    	public static void main (String args[]){
    	System.out.println("Welcome to 1337 Bank Type ? for help");
    	
    	//Get User Input
    	double balance = 10;
    	for(int i=1; i<(20000); i++)
    	{
    	Scanner scanner = new Scanner(System.in);
    	String input = scanner.nextLine();
    	
    	//Help Command
    	if (input.equalsIgnoreCase("?"))
    	{
    		Help();
    	}
    	//Deposit Command
    	if (input.equalsIgnoreCase("Deposit"))
    	{
    		Deposit(balance);
    	}
    	//Withdraw Command
    	if (input.equalsIgnoreCase("Withdraw"))
    	{
    		Withdraw(balance);
    	}
    	//Balance Command
    	if (input.equalsIgnoreCase("Balance"))
    	{
    		Balance(balance);
    	}
    	
    	
    	
    	}
    }
    
    	
    
    
    	public static void Withdraw(double balance) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("How much money would you like to Withdraw");
    		balance = balance - Integer.parseInt(scanner.nextLine());
    		System.out.println("New balance = " + balance + "$");
    	}
    
    	public static void Deposit(double balance) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("How much money would you like to deposit?");
    		balance = balance + Integer.parseInt(scanner.nextLine());
    		System.out.println("New balance = " + balance + "$");
    	}
    
    	public static void Help() {
    		System.out.println("Welcome to the help menu");
    		System.out.println("Command List:");
    		System.out.println("Deposit, Withdraw, Transfer, NewAccount, Balance");
    		System.out.println("--------------------------------------------");
    		
    	}
    	
    	public static double balance; void Balance(double balance) {
    		System.out.println("You Balance is:");
    		System.out.println(balance + "$");
    	}
    	
    }
    It does not remember the balance, I changed the private methods to public. But I thought in order to have the other methods see the new balance they would have to return balances value, however I get the error "Duplicate field Bank_Main.balance" when I have more than 1 method return balance with the following syntax
    Java Code:
    public static double balance; void Balance(double balance) {

  8. #8
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Weird Error With Methods

    You could implement a loop into your program and split it up into some other functions. Like:
    mainLoop();
    getInput();

    and have a boolean that you can use to keep the program running.
    Here:
    Java Code:
    boolean running = false;
    
    public static void main(String[] args) {
    running = true;
    mainLoop();
    
    }
    public void mainLoop() {
    while (running) {
    getInput();
    //do something else...
    //etc.
    
    }
    System.exit(0);//exit if running == false
    }

  9. #9
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Weird Error With Methods

    Thanks for the response but it didn't really make sense to me :(
    This is what I came up with (it has tons of errors i couldn't work out)
    Java Code:
    import java.util.Scanner;
    
    
    public class Bank_Main {
    	System.out.println("Welcome to 1337 Bank Type ? for help");
    
    	
    	
    	
    boolean running = false;
    	public static void main(String[] args) {
    		System.out.println("Welcome to 1337 Bank Type ? for help");
    
    	
    		running = true;
    		mainLoop();
    	
    		}
    		public void mainLoop() {
    			//Get User Input
    			double balance = 10;
    			
    		while (running) {
    		getInput();
    		{
    			
    			}
    			}
    		}
    		
    		}
    		private void getInput() {
    			{
    				for(int i=1; i<(20000); i++)
    				{
    				Scanner scanner = new Scanner(System.in);
    				String input = scanner.nextLine();
    				
    				//Help Command
    				if (input.equalsIgnoreCase("?"))
    				{
    					Help();
    				}
    				//Deposit Command
    				if (input.equalsIgnoreCase("Deposit"))
    				{
    					Deposit(balance);
    				}
    				//Withdraw Command
    				if (input.equalsIgnoreCase("Withdraw"))
    				{
    					Withdraw(balance);
    				}
    				//Balance Command
    				if (input.equalsIgnoreCase("Balance"))
    				{
    					Balance(balance);
    				}
    				
    			
    		}
    		//do something else...
    		//etc.
    		 
    	
    
    
    	 static void Withdraw(double balance) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("How much money would you like to Withdraw");
    		balance = balance - Integer.parseInt(scanner.nextLine());
    		System.out.println("New balance = " + balance + "$");
    	}
    
    	public static void Deposit(double balance) {
    		Scanner scanner = new Scanner(System.in);
    		System.out.println("How much money would you like to deposit?");
    		balance = balance + Integer.parseInt(scanner.nextLine());
    		System.out.println("New balance = " + balance + "$");
    	}
    
    	public static void Help() {
    		System.out.println("Welcome to the help menu");
    		System.out.println("Command List:");
    		System.out.println("Deposit, Withdraw, Transfer, NewAccount, Balance");
    		System.out.println("--------------------------------------------");
    		
    	}
    	
    	public static void Balance(double balance) {
    		System.out.println("You Balance is:");
    		System.out.println(balance + "$");
    
    		}}

  10. #10
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: Weird Error With Methods

    I can see a few extra curly braces and some obvious syntax mistakes.
    Firstly you should do a bit more reading about methods and their identifiers and also about calling methods from other methods.
    Because you are calling static methods from non-static methods and some of your code is all over the place.

    I think what you should do now is read over the example i gave you and from that, write down on paper how the program goes and the flow of it. Especially the order of things.
    Once on paper, you can start to code just the barebones of your program, so empty methods, make sure to include all the ones you need. When the methods are all done and its error-free, add some more code to the body of each method so your program starts to take shape.
    Just remember to compile regularly so you find your errors when they arise.

    Good luck, James.

  11. #11
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Weird Error With Methods

    Okay, so i'm trying to work with methods here. I've looked up every tutorial, read over Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects) a good deal of times and I still cant figure out how to make the method return a value then print it!
    Can someone please tell me whats wrong? How to make a method return a value, and what the value is that gets returned? like the name of the variable

    Java Code:
    import java.util.Scanner;
    
    
    public class Bank_Main {
    	public static void main (String args[]){
    		String x = null;
    		UserInput(null);
    		//CompareInput();
    		//ShowResult();
    		
    		System.out.println(x);
    	
    	
    	
    }
    
    	static String UserInput(String x) {
    		Scanner scanner = new Scanner(System.in);
    	   String input = scanner.nextLine();
    		return input;
    	}
    }
    PS: I just changed the code so if you were looking at it before look at it now, at least it doesn't error but x always returns null
    Last edited by skaterboy987; 10-17-2011 at 02:28 AM.

  12. #12
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Weird Error With Methods

    Nevermind I solved it...
    Java Code:
    import java.util.Scanner;
    
    
    public class Bank_Main {
    	public static void main (String args[]){
    		System.out.println(returnSomething() + " Has been returned");
    }
    
    	public static String returnSomething(){
    		Scanner scanner = new Scanner(System.in);
    	   String input = scanner.nextLine();
    		return input;
    	}
    }
    ^^works

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Weird Error With Methods

    Let's look at your code, OK?

    Java Code:
    import java.util.Scanner;
    
    public class Bank_Main {
    	public static void main (String args[]){
    		String x = null;  // (a)
    		UserInput(null);  // (b)
    		
    		System.out.println(x); // (c)
    }
    
    	static String UserInput(String x) { // (d)
               Scanner scanner = new Scanner(System.in);
    	   String input = scanner.nextLine();
    	   return input;  // (e)
    	}
    }
    First let's look at your method:
    Line (d): note that the method name must start lower-case, lest you might confuse folks helping you or more importantly grading you. So change it to userInput(...). The method takes in a parameter x, but does nothing with it. Often a method like this will use that parameter as a prompt, but yours does nothing with x. Finally, your method creates a Scanner object but does not close it. While this likely will have no effect on your small program it's always a good habit to close resources when done with them. Finally on line (e) you return from the method the String obtained from the user -- a good thing to do.

    Next let's see how you use your method:
    You first create a null String variable x on line (a) and then on line (c) print out x. There should be no surprise that x will be null there as there's no possible way that it can be anything else. Then on line (b) you call your method but ignore the String that it returns. Better would be to do something like this:

    Java Code:
    String myResult = userInput("Enter a String: "); // here I don't ignore the String returned by the method
    
    System.out.println(myResult);
    
    
    //....
    
    // then in my userInput method:
    public static String userInput(String prompt) {
       System.out.print(prompt);
       // code to get the input as you've already done
       // code to return the input as you've already done.
    }

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Weird Error With Methods

    Quote Originally Posted by skaterboy987 View Post
    Nevermind I solved it...
    Dang, just wasted a lot of time! :(

    But not really. The bottom line is that you figured it out, and that's the only thing that matters. Congrats!
    Last edited by Fubarable; 10-17-2011 at 03:28 AM.

  15. #15
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Weird Error With Methods

    Sorry for all the problems, but I got one more, I can't figure out why but, you have to type Hello twice for this program to recognize it


    import java.util.Scanner;
    public class Bank_Main {
    public static void main (String args[]){
    System.out.println("Type Hello");
    String input = returnSomething();
    System.out.println("Hello = " + returnSomething() + " is " + checkText(input));
    }

    public static String returnSomething(){
    Scanner scanner = new Scanner(System.in);
    String input = scanner.nextLine();
    return input;
    }

    public static boolean checkText(String input){
    if (input.equalsIgnoreCase("Hello")){
    return true;
    }
    else
    {
    return false;
    }
    }
    }

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. BlueJ help! weird error text meesed up
    By linux1man in forum Other IDEs
    Replies: 6
    Last Post: 02-07-2013, 03:54 PM
  2. Error Accessing Methods in My JAR file
    By avu in forum Advanced Java
    Replies: 4
    Last Post: 03-23-2010, 12:35 AM
  3. Replies: 2
    Last Post: 03-09-2010, 08:18 AM
  4. [SOLVED] Simple Calculator Applet Weird Error
    By sari in forum New To Java
    Replies: 5
    Last Post: 01-28-2009, 04:57 AM
  5. Weird Error?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •