Results 1 to 4 of 4
Thread: Input Validation Help
- 02-18-2011, 10:43 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
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 10:58 AM.
- 02-18-2011, 11:04 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-18-2011, 11:14 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 24
- Rep Power
- 0
tValue is being changed throughout the program though. Shouldnt that update those variables?
- 02-18-2011, 11:58 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Input technique for unknown lines of input
By ducreative in forum New To JavaReplies: 16Last Post: 09-23-2009, 09:26 AM -
JTable input validation
By Manfizy in forum New To JavaReplies: 10Last Post: 06-10-2009, 09:04 AM -
Input Validation
By kickflipper1087 in forum New To JavaReplies: 5Last Post: 11-03-2008, 05:47 AM -
how to take input and verify input in Java programs
By bilal_ali_java in forum Advanced JavaReplies: 0Last Post: 07-21-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks