Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: array size [10] need help!! any experts

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default array size [10] need help!! any experts

    Hi Guys,

    my choice 1. i key in the calories for the day but my program can calculate up till 4 days when i exit. so i am asking how to use array to calculate up to 10 days when i press 3 exit.

    an example of my output // display

    1. Enter Food and Intake per day
    2. Display food intake status
    3. Exit

    so let say i key in 8 days in 1. Enter Food and Intake per day , my exit 3. should show 8 days of the calculated intake by using array

    heres the code thanks alot

    import java.util.*; // for function scanner input
     
    public class Diet {
     
    	static int i = 0; 
     
    	static Scanner input = new Scanner (System.in).useDelimiter ("\r\n"); //system scann and print
     
    	static int[] breakfast = new int[10];   // declare as int 
        static int[] lunch = new int[10];		// delcare as int
        static int[] dinner =new int[10];		// declare as int
     
        static double[] getdata = new double [10];       // create an array of integers
     
    	static char selection = 'x'; // dummy initialization
     
        public Diet() {
     
        }
     
        public static void main(String [] args) throws Exception{
     
     
     
     
     
    	    while (selection !='Q'){ //loop
     
     
    	    	//display on run
     
     
    		   		System.out.println("**********Daily Food Intake**********");
    		    	System.out.println("1. Enter Food and Intake per day");
    		    	System.out.println("2. Display food intake status");
    		    	System.out.println("3. Exit");
    		    	System.out.println("*************************************");
     
    		    	System.out.print("Please Enter Your Choice: ");
     
    		    		selection = (char)System.in.read(); // dummy selection to capture input
    		    		System.in.read();
    		    		System.in.read();
     
     
    		    	if (selection == '1' || selection =='2' || selection == '3'){    // enter food(i) to store calories for the day using int array
     
     
    			    	if (selection == '1')
    			    	{
     
    			    		for (int i = 0; i < 10; i++) 
    			    			breakfast[i]=0;
    			    			lunch[i]=0;
    			    			dinner[i]=0;	
    			    		{
     
    				    		if (i == 0){
    				    			EnterFood(i);
    				    			i = 1;
    				    		}
    				    		else if (i == 1){
    				    			EnterFood(i);
    				    			i = 2;
    				    		}
    				    		else if (i == 2){
    				    			EnterFood(i);
    				    			i = 3;
    				    		}
    				    		else if (i == 3){
    				    			EnterFood(i);
    				    			i = 4;
    				    		}
    			    		}
    			    		//else{
    			    			//System.out.println("You have enter for all 3 day");
    			    	//	}		
     
     
    			    	}
     
    			    	if (selection == '2')   // choice 2, show calories for the day using selection 2 by totalcalories(i)
    			    	{
    			    		totalCalories(i);
    			    	}
     
    			    	if (selection == '3') // array store results and displayed the array data before terminates
    			    	{                            
     
    			    		System.out.println("Your Total intake for this day is " + getdata[0] + "Calories");
    			    		checkCalories(getdata[0]);
     
     
    			    		System.out.println("Your Total intake for this day is " + getdata[1] + "Calories");
    			    		checkCalories(getdata[1]);
     
                            System.out.println("Your Total intake for this day is " + getdata[2] + "Calories");
                            checkCalories(getdata[2]);
     
                             System.out.println("Your Total intake for this day is " + getdata[3] + "Calories");
                            checkCalories(getdata[3]);
     
                            	breakfast[i]=0;
    			    			lunch[i]=0;
    			    			dinner[i]=0;
     
     
     
    			    		selection = 'Q';   //while loop 
    			    	}	    		
    		    	}
    		    	else{
    		    		System.out.println("Please enter an valid option");  // display an error message
    		    	}
     
    			//}//end do	
     
    			//while loop
    			//while (selection !='Q');   	
     
    	    }//end while	
     
     
        }//end main
     
       //call method for Option 1-------------------------------------- 
       public static void EnterFood(int i)throws Exception{
     
       		setBreakfast();
       		setLunch();
       		setDinner();
       		getdata[i] = breakfast[i] + lunch[i]+ dinner[i]; //store to array
       		System.out.println("Total calories for the day = " + getdata[i]);	
     
       }
     
     
     
    //DAY 1------------  
       public static void setBreakfast()throws Exception{
    		System.out.print("1. Enter Breakfast intake <calories> : ");	
    		breakfast[i] = input.nextInt();  //capture input
     
       }
     
       public static void setLunch()throws Exception{
     
    		System.out.print("2. Enter Lunch intake <calories> : ");	
    		lunch[i] = input.nextInt(); //capture input
     
       }
     
       public static void setDinner()throws Exception{
    		System.out.print("3. Enter Dinner intake <calories> : ");	
    		dinner[i] = input.nextInt(); //capture input
     
       }
     
     
       //call method for Option 2--------------------------------------
       public static void totalCalories(int i){
     
       	double total = 0.0;
     
       		if (i==1){
       			total = getdata[0];  // total for intake breakfast + lunch + dinner
       			System.out.println("Your Total intake for this day is " + total + "Calories");
       			checkCalories(total);	
     
       		}
     
       		if (i==2){
       			total = getdata[1]; // total for intake breakfast + lunch + dinner
       			System.out.println("Your Total intake for this day is " + total + "Calories");	
       			checkCalories(total);
     
     
       		}
     
       		if (i==3){
       			total = getdata[2]; // total for intake breakfast + lunch + dinner
       			System.out.println("Your Total intake for this day is " + total + "Calories");
       			checkCalories(total);	
     
       		}   		 
     
       			if (i==4){
       			total = getdata[3]; // total for intake breakfast + lunch + dinner
       			System.out.println("Your Total intake for this day is " + total + "Calories");
       			checkCalories(total);	
     
       		}  
       		breakfast[i] = 0;    // reset after the diplaying day 1 and day 2 and day 3
       		lunch[i] = 0;	
       		dinner[i] = 0;	  	
       }
     
     
       public static void checkCalories(double a){  // check the calories intake if a is low, healthy or too much
     
    	   	if (a < 2000){
    	   		System.out.println("This intake is LOW");
     
    	   	}
     
    	   	if (a>=2000 && a <= 2500){
    	   		System.out.println("This intake is Healthy");
    	   	}
     
    	   	if (a > 2500){
    	   		System.out.println("This intake is TOO MUCH");
    	   	}
     
       } 
     
     
    }//end class
    Last edited by asdin; February 10th, 2012 at 11:53 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: array size [10] need help!! any experts

    I am not really sure what your question is. What exactly are you asking? Please read the link in my signature on asking questions the smart way and post an SSCCE with a specific question, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: array size [10] need help!! any experts

    When you have code that is repeated again and again and again with only a small consistent change from one to the next you should think about how to reduce the code to one instance by using a variable instead of hardcoding new numbers.
    Look at the relationship between the value of i and the array index for these statements and the ones following it:
    if (i==1){
        total = getdata[0]; // total for intake breakfast + lunch + dinner
    Is the index always one less than the value of i?

  4. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: array size [10] need help!! any experts

    Quote Originally Posted by KevinWorkman View Post
    I am not really sure what your question is. What exactly are you asking? Please read the link in my signature on asking questions the smart way and post an SSCCE with a specific question, and we'll go from there.
    sorry , is hard to type here if you have seen the program output. i am using methods for my java, as i am require to do an array size[10]

    for my choice 1. i key in the calories for the day but my program can calculate up till 4 days when i exit. so i am asking how to use array to calculate up to 10 days when i press 3 exit.

    an example of my output // display

    1. Enter Food and Intake per day
    2. Display food intake status
    3. Exit

    so let say i key in 8 days in 1. Enter Food and Intake per day , my exit 3. should show 8 days of the calculated intake by using array

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: array size [10] need help!! any experts

    Quote Originally Posted by Norm View Post
    When you have code that is repeated again and again and again with only a small consistent change from one to the next you should think about how to reduce the code to one instance by using a variable instead of hardcoding new numbers.
    Look at the relationship between the value of i and the array index for these statements and the ones following it:
    if (i==1){
        total = getdata[0]; // total for intake breakfast + lunch + dinner
    Is the index always one less than the value of i?
    yeah i am figuring but i just cant do array really need help lol. for the question you ask. my index 1 represent my 1st selection choice in calories intake.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: array size [10] need help!! any experts

    Have you worked on the change I suggested to get rid of the 4 if statements and reduce the code to a few statements that uses an index that is computed from the value of i to access the contents of the array?
    try something like this:
     total = getdata[i-1];
    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: array size [10] need help!! any experts

    Quote Originally Posted by Norm View Post
    Have you worked on the change I suggested to get rid of the 4 if statements and reduce the code to a few statements that uses an index that is computed from the value of i to access the contents of the array?
    try something like this:
     total = getdata[i-1];
    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting
    yea, i am trying but i cant figure how to use method with arrays thats the problem... any ideas??? if i am not wrong i suppose to use array on my intergers values for breakfast,lunch,dinner. so it can store and loop back.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: array size [10] need help!! any experts

    how to use method with arrays
    Can you explain what data you are having problems using arrays with.
    You are using an array here:
    total = getdata[i-1];

Similar Threads

  1. Needs experts help and advice
    By Addo kofi in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 29th, 2011, 08:44 PM
  2. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  3. hello java experts
    By bidex in forum Member Introductions
    Replies: 2
    Last Post: August 23rd, 2010, 05:52 AM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  5. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM