Results 1 to 16 of 16
Thread: Weird Error With Methods
- 10-16-2011, 11:20 PM #1
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
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:
All help is appreciated!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("--------------------------------------------"); } }
-
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.
- 10-16-2011, 11:31 PM #3
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
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);
}
- 10-16-2011, 11:31 PM #4
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: Weird Error With Methods
Why the semicolon?if (scanner.nextLine().equalsIgnoreCase("Deposit"));
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); }
- 10-16-2011, 11:36 PM #5
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
Re: Weird Error With Methods
Thanks for the responses everyone, My code is changed to:
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?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("--------------------------------------------"); } }
-
Re: Weird Error With Methods
- 10-17-2011, 12:11 AM #7
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
Re: Weird Error With Methods
Sorry for all the questions but now I've got 1 more probelm
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 syntaxJava 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 + "$"); } }Java Code:public static double balance; void Balance(double balance) {
- 10-17-2011, 12:14 AM #8
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
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 }
- 10-17-2011, 12:31 AM #9
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
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-17-2011, 12:50 AM #10
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
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.
- 10-17-2011, 02:24 AM #11
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
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
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 nullJava 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; } }Last edited by skaterboy987; 10-17-2011 at 02:28 AM.
- 10-17-2011, 02:58 AM #12
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
Re: Weird Error With Methods
Nevermind I solved it...
^^worksJava 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; } }
-
Re: Weird Error With Methods
Let's look at your code, OK?
First let's look at your method: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) } }
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. }
-
- 10-17-2011, 03:42 AM #15
Member
- Join Date
- Sep 2011
- Posts
- 56
- Rep Power
- 0
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;
}
}
}
-
Re: Weird Error With Methods
Look carefully at your code: you're calling returnSomething twice.
Similar Threads
-
BlueJ help! weird error text meesed up
By linux1man in forum Other IDEsReplies: 6Last Post: 02-07-2013, 03:54 PM -
Error Accessing Methods in My JAR file
By avu in forum Advanced JavaReplies: 4Last Post: 03-23-2010, 12:35 AM -
Weird error with FileHandler, java.util.logging.FileHandler
By nmvictor in forum New To JavaReplies: 2Last Post: 03-09-2010, 08:18 AM -
[SOLVED] Simple Calculator Applet Weird Error
By sari in forum New To JavaReplies: 5Last Post: 01-28-2009, 04:57 AM -
Weird Error?
By sciguy77 in forum New To JavaReplies: 4Last Post: 01-20-2009, 02:32 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks