Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 03-20-2012, 12:34 PM PM User | #1 __iCode__ New to the CF scene   Join Date: Mar 2012 Posts: 7 Thanks: 0 Thanked 0 Times in 0 Posts Please help, Scanner integers Hey guys, I am currently trying to create a program that has number stored in an integer, when all the numbers have been entered the program returns the numbers and on the next line the average of the numbers. I have a problem though, I only want as many variables as the user inputs that way the program will slit out the average. If you dont get what I mean i'll try make it simpler. I want the user to enter a number e.g 28, when 28 is entered the program will aoutomatically create 28 integers called num1 - num28. Then the integers will get their value from the Java scanner. Basically is there a piece of code that allows me to do this. If you can help I will be very greatful. And here is the code if it helps Code: ```import java.util.Scanner; class averageMark{ public static void main(String args[]){ System.out.println("Enter 30 numbers and the average percentage will be displayed!"); Scanner marks = new Scanner(System.in); int mark1 = marks.nextInt(); int mark2 = marks.nextInt(); int mark3 = marks.nextInt(); int mark4 = marks.nextInt(); int mark5 = marks.nextInt(); int mark6 = marks.nextInt(); int mark7 = marks.nextInt(); int mark8 = marks.nextInt(); int mark9 = marks.nextInt(); int mark10 = marks.nextInt(); int mark11 = marks.nextInt(); int mark12 = marks.nextInt(); int mark13 = marks.nextInt(); int mark14 = marks.nextInt(); int mark15 = marks.nextInt(); int mark16 = marks.nextInt(); int mark17 = marks.nextInt(); int mark18 = marks.nextInt(); int mark19 = marks.nextInt(); int mark20 = marks.nextInt(); int mark21 = marks.nextInt(); int mark22 = marks.nextInt(); int mark23 = marks.nextInt(); int mark24 = marks.nextInt(); int mark25 = marks.nextInt(); int mark26 = marks.nextInt(); int mark27 = marks.nextInt(); int mark28 = marks.nextInt(); int mark29 = marks.nextInt(); int mark30 = marks.nextInt(); int score[] = {mark1, mark2, mark3, mark4, mark5, mark6, mark7, mark8, mark9, mark10, mark11, mark12, mark13, mark14, mark15, mark16, mark17, mark18, mark19, mark20, mark21, mark22, mark23, mark24, mark25, mark26, mark27, mark27, mark28, mark29, mark30}; int sum = 0; for(int counter = 0; counter
 03-20-2012, 05:11 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,606 Thanks: 4 Thanked 2,598 Times in 2,567 Posts Use an array or collection from the start. It would just be silly to use an unknown size of variables. PHP Code: ``` int iSize = 30; // Or prompt for size.int[] aInput = new int[iSize];for (int i = 0; i < iSize; ++i){    aInput[i] = marks.nextInt();}  ``` Then iterate that to calculate the average. Using an ArrayList instead may be a better idea, then you don't even need to specify the size. You can iterate until the user enters Q or something, and the only difference is you need to try/catch an Integer.parseInt call and add to the collection.

 Bookmarks

 Tags code, java, program

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 01:32 PM.