Results 1 to 4 of 4
Thread: help with methods
- 03-11-2011, 03:30 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
help with methods
How do I get the user input from one method and use it another method? also i'm having a problem with when my "setup" method runs i can't pinpoint why the array is out of bounds
Java Code:import java.util.Scanner; public class Shop { public static void init(long[]arr) { for (int i=0; i<arr.length;i++) { arr[i]=0; } } //should return # of items public static int setup(Scanner input, String[]names,double[]prices) { System.out.print("Please enter the number of items: "); int items = input.nextInt(); if (items<=0) return 0; else { names= new String[items]; for (int i=0; i<=items; i++){ System.out.print("Enter name of product "+i+ " : "); names[i]= input.next(); System.out.print("Enter price of product " + i+ " : "); prices[i] = input.nextDouble(); } System.out.println("Please enter the amount to qualify for discount: "); int discount=input.nextInt(); System.out.println("Please enter the discount rate(0.1 for 10%): "); Double rate=input.nextDouble(); } return items; } public static int buy(Scanner input, String [] names,int[]amounts) { for (int i=0; i<names.length;i++) System.out.print("Enter the amount of "+ names[names.length]+" : " ); int purchase=input.nextInt(); if (purchase<=0) return 0; else return purchase; } public static void main(String[] args) { String []names = new String[0]; int []amounts= new int[0]; double []prices = new double[0]; int items=0; int purchase =0; Scanner input = new Scanner(System.in); while(true) { System.out.println("This program supports 4 functions:"); System.out.println("1. Setup Shop"); System.out.println("2. Buy"); System.out.println("3. List Items"); System.out.println("4. Checkout"); System.out.print("Please choose the function you want:"); int function=input.nextInt(); if (function==1) items=setup(input,names,prices); //figure out which arguments need else if (function==2){ if (items<=0); System.out.println("Shop is not setup yet!"); if (items>0) buy(input,names,amounts); } else if (function==3){ if (purchase<=0) System.out.println("Try again: You have not bought anything"); } else if (function==4){ } else { //error no option } } } }Last edited by Eranga; 03-11-2011 at 05:54 AM. Reason: code tags added
- 03-11-2011, 04:39 AM #2
You call methods, pass parameters and use return values.
Java Code:public int doubleUp(int value) { return 2 * value; } public int getInput() { Scanner reader = new Scanner(System.in); System.out.print("Enter a number: "); return reader.netxtInt(); } public void doStuff() { int input = getInput(); System.out.println(doubleUp(input)); }The error message provides valuable information, such as the value which is out of bounds and the line the exception occurs on. This should help pinpoint the problem.also i'm having a problem with when my "setup" method runs i can't pinpoint why the array is out of bounds
- 03-11-2011, 05:55 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@OP, please use code tags next time when you are posting code segments in the forum. Unformatted codes are hard to read. If you don't know how to do that, check my forum signature, you can find a link to relevant FAQ page.
- 03-11-2011, 06:10 AM #4
Member
- Join Date
- Mar 2011
- Posts
- 2
- Rep Power
- 0
okay so i got my first method to work but now i'm having trouble trying to get the second choice (function==2) to take the return from the setup method and not the initial amount in main.help?
XML Code:public static int setup(Scanner input, String[]names,double[]prices) { System.out.print("Please enter the number of items: "); int numItems = input.nextInt(); if (numItems<=0) return 0; else { names= new String[numItems]; prices = new double[numItems]; for (int i=0; i<numItems; i++){ System.out.print("Enter name of product "+i+ " : "); names[i]= input.next(); System.out.print("Enter price of product " + i+ " : "); prices[i] = input.nextDouble(); } System.out.print("Please enter the amount to qualify for discount: "); int discount=input.nextInt(); System.out.print("Please enter the discount rate(0.1 for 10%): "); Double rate=input.nextDouble(); return numItems;} } public static void main(String[] args) { String []names= new String[0]; int []amounts= new int[0]; double []prices= new double[0]; int numItems=0; int purchase=0; Scanner input = new Scanner(System.in); while(true) { System.out.println("This program supports 4 functions:"); System.out.println("1. Setup Shop"); System.out.println("2. Buy"); System.out.println("3. List Items"); System.out.println("4. Checkout"); System.out.print("Please choose the function you want:"); int function=input.nextInt(); if (function==1) numItems=setup(input,names,prices); //figure out which arguments need else if (function==2) { if (numItems<=0); System.out.println("Shop is not setup yet!"); if (numItems>0) buy(input,names,amounts); }
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
methods vs. non methods
By Latanyar in forum New To JavaReplies: 10Last Post: 10-11-2010, 11:46 PM -
Help with get and set methods
By shroomiin in forum New To JavaReplies: 4Last Post: 11-23-2009, 08:04 PM -
Help with Methods Please :-)
By jkhamler in forum New To JavaReplies: 22Last Post: 11-10-2009, 08:54 PM -
How to get methods to see variables in other methods
By ejs7597 in forum New To JavaReplies: 4Last Post: 04-03-2009, 06:36 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks