Results 1 to 4 of 4
  1. #1
    geraldd is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Quote Originally Posted by geraldd View Post
    How do I get the user input from one method and use it another method?
    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));
    }
    also i'm having a problem with when my "setup" method runs i can't pinpoint why the array is out of bounds
    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.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    @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.

  4. #4
    geraldd is offline Member
    Join Date
    Mar 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. methods vs. non methods
    By Latanyar in forum New To Java
    Replies: 10
    Last Post: 10-11-2010, 11:46 PM
  3. Help with get and set methods
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 11-23-2009, 08:04 PM
  4. Help with Methods Please :-)
    By jkhamler in forum New To Java
    Replies: 22
    Last Post: 11-10-2009, 08:54 PM
  5. Replies: 4
    Last Post: 04-03-2009, 06:36 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •