Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: User Input help

  1. #1
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default User Input help

    I am trying to save up spare change and tips from my work to buy a nexus 7 and I thought I could make a program that tracks my balance and allows me to make deposits. Here is what I have. I need help making it where the user can input how much the deposit amount is.
    import java.util.Scanner;
    class newDeviceFund
    {
    	//instance variables
    	double balance;
    	double goal;
    	double difference;
    	double amount;
     
    	//constructors
    	newDeviceFund(double bal, double gol)
    	{
    		balance = bal;
    		goal = gol;
    		difference = gol - bal;
    	}
    	//methods
    	double getBalance()
    	{
    		return balance;
    	}
    	void deposit(double amt)
    	{
    		amount = amt;
    		Scanner scan = new Scanner(System.in);
    		System.out.println("How much is your deposit? $");
    		amount = scan.nextDouble();
    		balance = balance + amount;
    	}
    	void allInfo()
    	{
    		System.out.println("Balance: $" + balance + "\nGoal: $" + goal + "\nYou have $" + difference + " to go!");
    	}
    }
    class newDevice
    {
    	public static void main(String[] args)
    	{
    		String device;
    		String numChoice;
    		newDeviceFund grouper = new newDeviceFund(0, 199);
    		newDeviceFund manta = new newDeviceFund(0, 399);
    		grouper.deposit();
    		/*System.out.println("Type device codename");
    		Scanner choice = new Scanner(System.in);
    		device = choice.nextLine();
    		if (device.equals("grouper"))
    			System.out.println("Press 1 to deposit \nPress 2 to view stats on account");
    			Scanner num = new Scanner(System.in);
    			numChoice = num.nextLine();
    			if (numChoice.equals("1"))
    				grouper.deposit();
    			if (numChoice.equals("2"))
    				grouper.allInfo();
    		if (device.equals("manta"))
    			System.out.println("Press 1 to deposit \nPress 2 to view stats on account");
    			Scanner next = new Scanner(System.in);
    			numChoice = num.nextLine();
    			if (numChoice.equals("1"))
    				manta.deposit();
    			if (numChoice.equals("2"))
    				manta.allInfo();
    		*/
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    Am I at least on the right track?
    Does the code do what you want? If not explain.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    When I call the method by adding grouper.deposit();

    This error is thrown
    javac newDevice.java
    newDevice.java:43: deposit(double) in newDeviceFund cannot be applied to ()
    grouper.deposit();
    ^
    1 error

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    deposit(double) in newDeviceFund cannot be applied to ()
    grouper.deposit();
    The compiler is saying that it can not find a method deposit() that doesn't take any arguments.
    The deposit() method it does find takes a double for an argument.

    Change the code to use an arg in the call to the method.
    Or change the method's definition so it does not take an arg and can be called without an arg

    		amount = amt;
    		Scanner scan = new Scanner(System.in);
    		System.out.println("How much is your deposit? $");
    		amount = scan.nextDouble();
    This code doesn't make sense the way it puts values into amount. The value from amt is immediately replaced by a new value read in with the nextDouble() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    lanmonster (January 19th, 2013)

  6. #5
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    If I put any random number in the arguments for deposit() for example: deposit(2) is it going to automatically deposit what I set the argument or is it going to start to use the user input?

    How would you suggest revising the code that does not make sense?
    Thank you for your help!

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    How would you suggest revising the code that does not make sense
    First think about what you want the code to do. When you have decided what it should do, write the code to do that. I gave you two choices in post #4 on how to correct the compiler error.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    okay, I got that problem fixed with your help. Can you help me create a GUI similar to one like the attached picture?
    screenshot02.jpg
    the GUI in the pic works by selecting the number associated with the task you want to do. It does it and then returns to the page with all the options.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    help me create a GUI
    Start by reading the tutorial:
    Trail: Creating a GUI With JFC/Swing (The Java™ Tutorials)
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    that guide is not for a GUI like the picture.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    If its not a Swing GUI, what kind of GUI is it?

    The image looks like a console which I wouldn't call a GUI.
    A console is where the output from System.out.print() goes.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    oh, sorry. How do I make a console similar to that one?

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    How do I make a console
    Do you want to make your own console and not use the one that comes with the OS?
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    No, I'll stick with the included one.

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    Use the System.out.print() and println() methods to write on the OS's console.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #15
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    How could I make only the balance be sent to a file? I tried java newDevice > balance.txt but it sends "type device codename" to the file.

  17. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    Are you asking about an OS feature or about how to have a java program write a file?
    If you don't understand my answer, don't ignore it, ask a question.

  18. #17
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    Sorry if I am getting on your nerves. I certainly am not trying to. Java program write to file please.

  19. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    If the output is text, the PrintWriter class could do it.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #19
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    one last question. I need to make it where when I can go back to the point where I make the decision to view stats or deposit. How do I do this?

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: User Input help

    go back to the point
    That sounds like you need a loop where you can decide either "to view stats or deposit"
    If you don't understand my answer, don't ignore it, ask a question.

  22. #21
    Member
    Join Date
    Jan 2013
    Location
    Central Texas
    Posts
    39
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: User Input help

    Got it! Now I just have to get it GUI'd up and I'll be all done!

Similar Threads

  1. Take user input
    By frabbi.cse in forum Java Theory & Questions
    Replies: 1
    Last Post: July 22nd, 2012, 12:48 PM
  2. Valid user input
    By ChristopherLowe in forum Java Programming Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  3. Valid user input
    By ChristopherLowe in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: June 21st, 2011, 04:53 PM
  4. User Input File Name
    By PineAppleKing in forum Java Theory & Questions
    Replies: 12
    Last Post: June 3rd, 2011, 10:23 AM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM