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 4 of 4

Thread: How to store a number that was inputted by the user? please help

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to store a number that was inputted by the user? please help

    Hey people im just wondering is there a way to store a number without using the non-void methods
    heres my code my code works grand but i need to store the balence and by how much it was topped up by if u get me drift
    class Rough1{
       public static void main(String[] args)
       {
        showServiceMenu();
       }
     
       // Declare Global Variables
       int input=0;
     
       public static void showServiceMenu()
       {
       // Local Variables
        int input=0;
        String password = "me";
        int cardnum = 123;
        double balence=0;
        double add=0;
        double purchase;
        char option;
        char stop = 'x';
     
    		// Loop to repeat the program until x is entered
        do{
        System.out.println("Login: Press 1");
        System.out.println("Customer Details: Press 2");
    		System.out.println("Customer Balence: Press 3");
    		System.out.println("Top-Up: Press 4");
    		System.out.println("Purchase: Press 5");
    		input = Keyboard.readInt();
     
       if(input==1){
       System.out.println("Please enter in your Debit Card number and password ");
       System.out.println("Debit Card Number: ");
       cardnum = Keyboard.readInt();
       System.out.println("Password: ");
    	 password = Keyboard.readString();}
     
    	 if(input==2){
    	 System.out.println("Your Details ");
    	 System.out.println("Name: Shane Lowry ");
    	 System.out.println("Address: 9 Sheepmoor Ville ");
    	 System.out.println("Card Number: 123");}
     
       if(input==3){
       System.out.println("Your balence is "+balence);}
     
       if(input==4){
       System.out.println("How much would you like to top-up");
       add = Keyboard.readDouble();
       if(add <= 100)
       System.out.println("Your new balence is "+(add+balence));
       else
       System.out.println("Your cannot add more than 100");}
     
       if(input==5){
       System.out.println("How much would you like to spend?");
       purchase = Keyboard.readDouble();
       if(purchase>balence){
       System.out.println("You don't have enough money to make a purchase");}
       else if(purchase<=balence){
       System.out.println("Thank you, you have made a purchase for "+purchase);}
       }
       System.out.println("Do you want to continue?");
    	 option = Keyboard.readChar();
       if(option=='x'){
    	 System.out.print("Goodbye");}
       }while(option!='x');
    	}
    }


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

    Default Re: How to store a number that was inputted by the user? please help

    a way to store a number without using the non-void methods
    Can you explain how the return type of a method is involved?
    All executed code (excluding initialization of class variables) is done in a method.

    i need to store the balence
    balence is currently defined inside of a method. It will disappear when the method exits. If you want to save the value after the method exits, you need to move its definition to the class level.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    32
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: How to store a number that was inputted by the user? please help

    yh im sorry im new and well im stupid so i kinda understand what you are saying but not fully if u get me but i trying to store the number when i top-up so when i go to purchase it will subtraction the purchase from the new balence which is in this case (add+balence) you get me?

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

    Default Re: How to store a number that was inputted by the user? please help

    if u get me
    Sorry I don't understand what you are trying to do.
    i trying to store the number
    Assign it to variable.
    variable = number; // store the number in a variable

    subtraction the purchase from the new balence
    Use an assignment statement to assign the old value - purchase to the variable:
    variable = variable - amount; // subtract amount from variable
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How To Ask the User to Enter Another Number Using the Number 1
    By Pettsa in forum Object Oriented Programming
    Replies: 6
    Last Post: May 3rd, 2012, 03:44 AM
  2. Trying to get Java to ask the user for a number
    By SunsetSkies in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 26th, 2012, 06:18 PM
  3. How to get user's browser serial number using JSF?
    By rcbandit2 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 26th, 2011, 09:52 PM
  4. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM
  5. Help with making the user input to store in file
    By dannyyy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 06:52 AM