Results 1 to 4 of 4
  1. #1
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Input Validation Help

    so this is a program that i am writing. how do i make it so that when it asks for how many shares do you want to sell, the input validation makes sure there is only an integer entered rather than eg. a character?

    also side note, im not sure why but my tValue isnt changing...

    edit** ps: its very late so forgive me for being confusing.

    Java Code:
    //Any necessary imports
    import java.util.*;
    
    public class StockTrader 
    { 
        public static void main(String[] args) 
        {
    
            // declare and initialize a Scanner
            Scanner in = new Scanner(System.in);
            // get seed value from user and use it to make the random number
            // generator
            System.out.println("Please enter a seed value [integer]:");
            // you may assume the user inputs an int for the seed
            int seed = in.nextInt();
            in.reset();
            Random rng = new Random(seed);
    
            // declare and initialize any other needed variables
            int pORNG = 100;
            int pMCRO = 100;
            int pGUUG = 100;
            int sORNG = 0;
            int sMCRO = 0;
            int sGUUG = 0;
            int money = 10000;
            int day = 1;
            int tValue = (sORNG * pORNG + sMCRO * pMCRO + sGUUG * pGUUG + money);//why isnt this changing?
    
    
            // begin day loop
            while (day < 12) 
            {
                // print daily report
                System.out.println("****************************");
                System.out.println("Day " + day + " - You Currently Have:");
                System.out.println("Money: " + money);
                System.out.println("ORNG Shares: " + sORNG);
                System.out.println("MCRO Shares: " + sMCRO);
                System.out.println("GUUG Shares: " + sGUUG);
                System.out.println("Total Value: " + tValue);
                System.out.println();
                // begin menu loop
    
                boolean done = false;
                while(!done)
                {
                    // prompt user for their decision (buy, sell, or end turn)
                    System.out.println("Would you like to:");
                    System.out.println("1. Buy");
                    System.out.println("2. Sell");
                    System.out.println("3. End your trading");
                    String userChoice = in.next();
    
                    while (!userChoice.equals("1") && !userChoice.equals("2") && !userChoice.equals("3"))
                    {
                        System.out.println("Invalid input: enter 1, 2, or 3.");
                        System.out.println("Would you like to:");
                        System.out.println("1. Buy");
                        System.out.println("2. Sell");
                        System.out.println("3. End your trading");
                        userChoice = in.next();
    
                    }
                    // if user chose buy
    
                    if (userChoice.equals("1")) 
                    { 
                        // Start buy loop
                        // prompt for which stock to buy
                        System.out.println("Which stock would you like to buy?");
                        System.out.println("1. Orange Inc.");
                        System.out.println("2. Macrosoft");
                        System.out.println("3. Guugle");
                        String iBuy = in.next();
    
                        while (!iBuy.equals("1") && !iBuy.equals("2") && !iBuy.equals("3"))
                        {
                            System.out.println("Invalid input; enter 1, 2, or 3.");
                            System.out.println("Which stock would you like to buy?");
                            System.out.println("1. Orange Inc.");
                            System.out.println("2. Macrosoft");
                            System.out.println("3. Guugle");
                            iBuy = in.next();
    
                        }
    
                        // prompt for how many shares to buy
    
                        System.out.println("Enter how many shares you would like to buy:");
                        int sBuy = in.nextInt();
                        
                        //if (in.hasNextInt())
                        // update money and shares count
                            if (iBuy.equals("1")) 
                            {
                                int nMoney = pORNG * sBuy;
        
                                while (nMoney > money)
                                {
                                    System.out.println("You don't have that much money.");
                                    System.out.println("Enter how many shares you would like to buy:");
                                    sBuy = in.nextInt();
                                    nMoney = pORNG * sBuy;
                                }
        
                                money = money - nMoney;
                                sORNG = sORNG + sBuy;
        
                                System.out.println("You now have " + money + " money.");
                            } 
                            else if (iBuy.equals("2"))
                            {
                                int nMoney = pMCRO * sBuy;
                                
                                while (nMoney > money)
                                {
                                    System.out.println("You don't have that much money.");
                                    System.out.println("Enter how many shares you would like to buy:");
                                    sBuy = in.nextInt();
                                    nMoney = pMCRO * sBuy;
                                }
        
                                money = money - nMoney;
                                sMCRO = sMCRO + sBuy;
        
                                System.out.println("You now have " + money + " money.");
                            } 
                            else if (iBuy.equals("3"))
                            {
                                int nMoney = pGUUG * sBuy;
                                
                                while (nMoney > money)
                                {
                                    System.out.println("You don't have that much money.");
                                    System.out.println("Enter how many shares you would like to buy:");
                                    sBuy = in.nextInt();
                                    nMoney = pGUUG * sBuy;
                                }
        
                                money = money - nMoney;
                                sGUUG = sGUUG + sBuy;
        
                                System.out.println("You now have " + money + " money.");
                            } 
                        }
                    //    else
                    //    {
                    //        System.out.println("Please enter an integer.");
                    //    }
                    
    
                    // End buy loop
                    // if user chose sell
                    
                    if (userChoice.equals("2")) 
                    {
                        // Start sell loop
    
                        // prompt for which stock to sell
                        System.out.println("Which stock would you like to sell?");
                        System.out.println("1. Orange Inc.");
                        System.out.println("2. Macrosoft");
                        System.out.println("3. Guugle");
                        String iSell = in.next();
                        
                        while (!iSell.equals("1") && !iSell.equals("2") && !iSell.equals("3"))
                        {
                            System.out.println("Invalid input; enter 1, 2, or 3.");
                            System.out.println("Which stock would you like to sell?");
                            System.out.println("1. Orange Inc.");
                            System.out.println("2. Macrosoft");
                            System.out.println("3. Guugle");
                            iSell = in.next();
    
                        }
                        // prompt for how many shares to sell
                        System.out.println("Enter how many shares you would like to sell:");
                        int sSell = in.nextInt();
                    
                            if (iSell.equals("1")) 
                            {
                                int nMoney = pORNG * sSell;
        
                                while (sSell > sORNG)
                                {
                                    System.out.println("You don't have that many shares.");
                                    System.out.println("Enter how many shares you would like to sell:");
                                    sSell = in.nextInt();
                                    nMoney = pORNG * sSell;
                                }
        
                                money = money + nMoney;
                                sORNG = sORNG - sSell;
        
                                System.out.println("You now have " + money + " money.");
                            } 
                            else if (iSell.equals("2"))
                            {
                                int nMoney = pMCRO * sSell;
                                
                                while (sSell > sMCRO)
                                {
                                    System.out.println("You don't have that many shares.");
                                    System.out.println("Enter how many shares you would like to sell:");
                                    sSell = in.nextInt();
                                    nMoney = pMCRO * sSell;
                                }
        
                                money = money + nMoney;
                                sMCRO = sMCRO - sSell;
        
                                System.out.println("You now have " + money + " money.");
                            } 
                            else if (iSell.equals("3"))
                            {
                                int nMoney = pGUUG * sSell;
                                
                                while (sSell > sGUUG)
                                {
                                    System.out.println("You don't have that many shares.");
                                    System.out.println("Enter how many shares you would like to sell:");
                                    sSell = in.nextInt();
                                    nMoney = pGUUG * sSell;
                                }
        
                                money = money + nMoney;
                                sORNG = sORNG - sSell;
        
                                System.out.println("You now have " + money + " money.");
                            }
                        
                
                        // update money and shares count
                
    
                        }
                    
                    // end of loop sell loop
                    // if user chose to end turn
                    if (userChoice.equals("3")) 
                    {
                        done = true;
                    }
                }
            
    
            // update data such that menu loop will end 
    
            // end menu loop
    
            // calculate closing prices for Orange Inc., Macrosoft, and Guugle
    
            int change = rng.nextInt(2);
            int sChange = (int) (Math.random() * 10) +1;
    
            if (change == 0) 
            {
                pORNG = pORNG + sChange;
    
                //System.out.println("Orange Inc. Increases by " + sChange);
                //System.out.println("New price is: " + pORNG);
            } 
            else 
            {
                pORNG = pORNG - sChange;
                //System.out.println("Orange Inc. Decreases " + sChange);
                //System.out.println("New price is: " + pORNG);
            }
    
            int change2 = rng.nextInt(2);
            int sChange2 = (int) (Math.random() * 10) +1;
            // You need to make all the stocks change mang :|
            if (change2 == 0) {
                pMCRO = pMCRO + sChange2;
                //System.out.println("Macrosoft Increases by " + sChange2);
                //System.out.println("New price is: " + pMCRO);
            } 
            else 
            {
                pMCRO = pMCRO - sChange2;
                //System.out.println("Macrosoft Decreases by " + sChange2);
                //System.out.println("New price is: " + pMCRO);
            }
            int change3 = rng.nextInt(2);
            int sChange3 = (int) (Math.random() * 10) +1;
            if (change3 == 0) 
            {
                pGUUG = pGUUG + sChange3;
                //System.out.println("Guugle Increases by " + sChange3);
                //System.out.println("New price is: " + pGUUG);
            } 
            else 
            {
                pGUUG = pGUUG - sChange3;
                //System.out.println("Guugle Decreases by " + sChange3);
                //System.out.println("New price is: " + pGUUG);
            }
    
            // print end of day report
            System.out.println("Day " + day + " - Closing Prices:");
            System.out.println("Money: " + money);
            System.out.println("ORNG Price: " + pORNG);
            System.out.println("MCRO Price: " + pMCRO);
            System.out.println("GUUG Price: " + pGUUG);
            System.out.println("");
            // end of day loop
            day++;
            }
            // print end-of-game report
    
            if (day == 12) 
            {
                System.out.println("*******************");
                System.out.println("*******************");
                System.out.println("End of Game Summary");
                System.out.println("Money: " + money);
                System.out.println("ORNG Shares: " + sORNG);
                System.out.println("MCRO Shares: " + sMCRO);
                System.out.println("GUUG Shares: " + sGUUG);
                System.out.println("Total Value: " + tValue);
            }
        }
    }
    Last edited by Spyderpig; 02-18-2011 at 11:58 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Spyderpig View Post
    so this is a program that i am writing. how do i make it so that when it asks for how many shares do you want to sell, the input validation makes sure there is only an integer entered rather than eg. a character?

    also side note, im not sure why but my tValue isnt changing...
    Java Code:
            // declare and initialize any other needed variables
            int pORNG = 100;
            int pMCRO = 100;
            int pGUUG = 100;
            int sORNG = 0;
            int sMCRO = 0;
            int sGUUG = 0;
            int money = 10000;
            int day = 1;
            int tValue = (sORNG * pORNG + sMCRO * pMCRO + sGUUG * pGUUG + money);//why isnt this changing?
    Do your math: tValue = 0*100 + 0*100 + 0*100 + money ( == money )

    About the input validation: read a String from the user input stream; in a try-catch block try to Integer.parsInt( ... ) the String; if the input wasn't valid an Exception is thrown; you catch the exception and tell the user about the mistake and try again. If no Exception was thrown you have your int value.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    Spyderpig is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default

    tValue is being changed throughout the program though. Shouldnt that update those variables?

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default

    Quote Originally Posted by Spyderpig View Post
    tValue is being changed throughout the program though. Shouldnt that update those variables?
    I searched your program text for "tValue=" (with and without space(s) before the = sign) but nowhere do you assign it a (new) value. It doesn't change automagically.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Input technique for unknown lines of input
    By ducreative in forum New To Java
    Replies: 16
    Last Post: 09-23-2009, 09:26 AM
  2. JTable input validation
    By Manfizy in forum New To Java
    Replies: 10
    Last Post: 06-10-2009, 09:04 AM
  3. Input Validation
    By kickflipper1087 in forum New To Java
    Replies: 5
    Last Post: 11-03-2008, 06:47 AM
  4. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 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
  •