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 16 of 16

Thread: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    This is the output below. I am given the 3 menu items, prices, and 3 Product numbers.

    I have to create the source code. I am using elcipse ( My code at bottom)
    OUTPUT
    __________________________________________________ __________________________________________________ ________________________________


    Enter ItemNumber of Product (OR ZERO to end) order.
    1
    Cheeseburger $2.49
    Enter ItemNumber of Product (OR ZERO to end) order.
    2
    Pepsi
    Enter ItemNumber of Product (OR ZERO to end) order.
    3
    Chips
    Enter ItemNumber of Product (OR ZERO to end) order.
    4
    Sorry, we do not carry that item.
    4 Enter ItemNumber of Product (OR ZERO to end) order.
    0
    Billy Goat Fast-Foods
    Description Price
    ------------ -----
    Cheeseburger $2.49
    Pepsi $1.00
    Chips $0.59
    The total cost of this order is $ 4.08
    Thank you from Bily Goat Fast-Foods.
    __________________________________________________ __________________________________________________

    My start on this problem

     <YOUR CODE HERE>
    import.java.util.Scanner;
    public class BillyGoatArrays {
     
     
    	public static void main(String[] args) {
    Scanner input = new Scanner (System.in);
    		int itemNumber [] = {1,2, 3};
     
    		String menu[] = {"Cheeseburger", "Pepsi", "Chips"};
    	      double price[] = {2.49, 1.00, 0.59};
    	      int loopIndex = 0;
    	      for (loopIndex = 0; loopIndex < 3; loopIndex++)
    	      {
    	    	  System.out.println("Enter itemNumber of Product or enter ZERO to end order");
    	    	  System.out.println("Menu is = " + menu[loopIndex]);
    	          System.out.println(menu[loopIndex] +" Price is = " + price[loopIndex]);
    	          }  
    	       }
    	    }
    __________________________________________________ __________________________________________________ ____________________________________
    I think I can figure out the print prompts . I am stuck on the array for the item number and the message for item 4. I do not understand why item number 4 shows up in output twice in a row but shows the Sorry message only once. To get the program to display all of the products sequentially and then total them has me stuck. I am wondering if I need an if statement for the number 4. Instructor says he wants this program to allow up to 100 items to be ordered. I don't know how to make the third array of item numbers with an index of 100 . Would I do something like itemnumber[100] and then go itemnumber[1] = cheeseburger, itemnumber [2] = Pepsi... and so on and then do the Sorry message for item number 4, or would I do some type of catch statement? I don't want the answers given to me.. Just a little guidance. My instructor is a a very old programmer and he DOES not Teach. He just throws this stuff at us with the chapter and expects it done when this is supposed to be our first INTRO to Programmming class. Basically everyone eventually copies the program of the one guy in the class that has a programming background.. But I don't learn anythng this way.. A littel advice appreciated. I also still have to creat a scanner object to allow console input.. That I know.. Setting the item numbet array to not use zero so it can be used as a way to end loop and total order is beyond me, too.
    Last edited by Jaloney; November 28th, 2012 at 12:12 PM. Reason: add scanner object and code wrap


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Where does the program read any input from the user?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Jaloney (November 30th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    Thank you! That little code wrap helps make the code a lot easier to read. Now I am trying to figure out if I need to add something like
    item number 1 = input.next. I don't understand how to connect this item number wiith the other two parrell arrays..

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    The way you use parallel arrays is to use the same index for all the arrays. For example if you search in one array and find what you were looking for, save the found index's value and use it in the other arrays to get the corresponding value from the other arrays.
    Like columns written on a piece of paper. If you find something on a row in one column, the corresponding values are on the same row in the other columns. The index is the row.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Jaloney (November 30th, 2012)

  7. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    So the if loop referencing the index should apply to the item number array also and pick up each item number.. I think that is what you are saying.

    I added the scanner util and created new scanner object to allow input from user but this is gives me multiple errors in Eclipse now. Before it at least ran.

    It says I have a misplaced contructor in the class,
    in the main method it says their is a syntax error in my void . It shows multiple references to brackets but I don't see any missing brackets



     <YOUR CODE HERE>
    public class BillyGoatArrays {	
    import java. util.Scanner; 
     
    	public static void main(String[] args) {
    		Scanner input = new  Scanner (System.in);
     
     
    		int itemNumber [] = {1,2, 3};
     
    		String menu[] = {"Cheeseburger", "Pepsi", "Chips"};
    	      double price[] = {2.49, 1.00, 0.59};
    	      int loopIndex = 0;
    	      for (loopIndex = 0; loopIndex < 3; loopIndex++)
    	      {
    	    	  System.out.println("Enter itemNumber of Product or enter ZERO to end order");
    	    	  System.out.println("Menu is = " + menu[loopIndex]);
    	          System.out.println(menu[loopIndex] +" Price is = " + price[loopIndex]);
    	          }  
    	       }
    	    }

    Do you see an error here?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    gives me multiple errors
    Please copy and paste here the full text of the compiler's error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    Quote Originally Posted by Norm View Post
    Please copy and paste here the full text of the compiler's error messages.
    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token "void", @ expected
    Syntax error on token "]", invalid (
    Syntax error, insert "]" to complete ArrayAccess
    Syntax error, insert ")" to complete SingleMemberAnnotation
    Syntax error, insert "enum Identifier" to complete EnumHeader

    at BillyGoatArrays.main(BillyGoatArrays.java:6)

    It is probably something dumb but I just don't see it. I am not too familar with Eclipse. My instructor has us use Jgrasp but Jgrasp won't run from home.

  10. #8
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token "void", @ expected
    Syntax error on token "]", invalid (
    Syntax error, insert "]" to complete ArrayAccess
    Syntax error, insert ")" to complete SingleMemberAnnotation
    Syntax error, insert "enum Identifier" to complete EnumHeader

    at BillyGoatArrays.main(BillyGoatArrays.java:6)
    Don't see any problems with brackets or paranthese... stumped...

  11. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    I am making some progress! I tested my input and everything works so far. AMAZING. I am stuck on how to "play back" the thired array in sequence to print out the description, price and total the order.. I am just learning arrays so this has me stumped. I am thinking I need to write a method of some sort that somehow references the index for the previous input.next Item numbers enter, but how do I only reference the previous input? Advice appreciated.


     <YOUR CODE HERE>
    import java. util.Scanner; 
     
    public class BillyGoatArrays {	
     
     
    	public static void main(String[] args) {
    	Scanner input = new  Scanner (System.in);
    		int userInput = -1;
     
     
     
    		int[] itemNumber  = new int[100];
     
     
     
    		String menu[] = {"Cheeseburger", "Pepsi", "Chips"};
    		double price[] = {2.49, 1.00, 0.59};
    		int position = 0;
    		while ( userInput != 0 )  
    		{
    			System.out.println("Enter itemNumber of Product (OR enter ZERO to end order): ");
    			userInput = input.nextInt();
     
    			if (userInput > 0 && userInput < 4)	
    			{
     
    			}
    			else if (userInput != 0)
    			{
    				System.out.println("Sorry we do not carry that item ");
    			} System.out.println (" Billy Goat Fast-Foods");
    			System.out.println (" Description\t Price");
    			System.out.println ("-----------\t-----");
     
     
    		}  
    	}
    }
    Last edited by Jaloney; November 30th, 2012 at 10:13 PM. Reason: I did not copy off al the code the first time...!

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    how to "play back" the thired array in sequence to print out the description, price and total the order
    Can you explain what "play back" means.

    Remember parallel arrays are like rows of data in columns on a piece of paper. The columns are represented by the arrays in your program. The row is selected by the array index
    If you find an item on a row in a column that you are interested in, the data on the same row in the other columns are all related. Once the index is set to point to a row, then that same index can be used in the other arrays to get the corresponding data from the other arrays.
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    Jaloney (December 1st, 2012)

  14. #11
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code (cash register program)

    Quote Originally Posted by Norm View Post
    Can you explain what "play back" means.
    This is my very first array assignment. I am so happy you wrote back. I have spent hours on this and at the tutoring lab and the tutors are not very experienced in Java so I am on my own. I keep rewriting the code, reading and trying different options. I think the instructor should give us easier programs to start with and then move us up to this.. but this is what I have to do.
    My assignment gives me the ouput with the instructions to total all of a persons order, based on the numbers they enter, and print out the description, item prices and final total after they end their order by pressing zero. The instructors instructions say: As shown in ouptut, AFTER THAT THIRD ARRAY WAS "played back" in sequence to print three descriptions and then price of each ordered item while a total was being kept of the prices of all items ordered. He wants the person to be able to enter up to 100 menu items.

    The instructions give me this table

    Product Number Product Description Price($)
    1 Cheesburger 2.49
    2 Pepsi 1.00
    3 Chips 0.59

    This is the output I am trying to write a program for below.. Something like a cash register program...
    __________________________________________________ __________________________________________________ ________________________
    Enter ItemNumber of Product (or Press Zero to end order).
    1
    Cheesburger S 2.49
    Enter ItemNumber of Product (or Press Zero to end order).
    2
    Pepsit $1.00
    Enter ItemNumber of Product (or Press Zero to end order).
    3.
    Chips $0.59
    Enter ItemNumber of Product (or Press Zero to end order).
    4
    Sorry, we do not carry that item.
    0
    Billy Goat Fast-Foods
    Description Price
    ________ ____
    Cheeseburger $2.49
    Pepsi $1.00
    Chips $0.59
    The total of this order is $4.06
    Thank your from Billy Goat Fast-Foods.

  15. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    Do you have any specific questions about your assignment?
    What are you trying to do next? Do one thing at a time, test it, get it to work before moving to the next thing. Use println statements to print out the results of each step to be sure the code is doing what you expect.
    If you don't understand my answer, don't ignore it, ask a question.

  16. The Following User Says Thank You to Norm For This Useful Post:

    Jaloney (December 1st, 2012)

  17. #13
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    Quote Originally Posted by Norm View Post
    Do you have any specific questions about your assignment?
    What are you trying to do next? Do one thing at a time, test it, get it to work before moving to the next thing. Use println statements to print out the results of each step to be sure the code is doing what you expect.
    I am stuck at the end.. How to total everything and play it back after entering 0
    I have no clue how to make it print out the previously entered menu items and total them..
    This is my output..below... which all works. Its after ending the order tht I am stumped.. I can do a print statment that prints out items ordered and prices for the entire order,seperately,
    but I need to write something that will print this out and total it sequentially for each individual order after 0 is entered. Any clues to point me in the righ direction appreciated. I am lost.


    Enter itemNumber of Product (OR enter ZERO to end order):

    1
    Enter itemNumber of Product (OR enter ZERO to end order):
    2
    Enter itemNumber of Product (OR enter ZERO to end order):
    3
    Enter itemNumber of Product (OR enter ZERO to end order):
    4
    Sorry we do not carry that item
    Enter itemNumber of Product (OR enter ZERO to end order):
    0
    Billy Goat Fast-Foods
    Description Price

  18. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    How to total everything
    Use a variable to hold the total. As you get a new amount for the selected item, add it to the variable.
    When you exit the loop the variable will have the total and can be printed.
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Jaloney (December 1st, 2012)

  20. #15
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    Thank you Norm. I tried to make a variable sum with a for loop that adds to each loop to hold the input but it would not let me make an int sum variable to print out menu.. which is a string.. or use it to total Price which is a double. I am crossed eyed at this point.. I am going to take a break and watch some more Java tutorial videos and hope something gels with me.

  21. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,309
    Thanks
    56
    Thanked 2,373 Times in 2,344 Posts

    Default Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code

    it would not let me
    Any time you get error messages you need help with should post them and the code.


    I'm done for tonight, back tomorrow,
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Jaloney (December 2nd, 2012)

Similar Threads

  1. Source code please
    By Gavin Fraser in forum Java Networking
    Replies: 2
    Last Post: September 17th, 2012, 04:46 PM
  2. need help with a new way of source code
    By Balger in forum Object Oriented Programming
    Replies: 15
    Last Post: August 26th, 2012, 02:41 PM
  3. newbie question: Error: Could not find or load main class Java Result: 1
    By ideaman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 1st, 2012, 11:40 PM
  4. Getting Value from Website source code.
    By Blackbird94 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 26th, 2011, 08:16 AM
  5. Beginner trying to write Java code, has issue w/ printing result and 2 decimals
    By flpanthers1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2011, 12:11 PM

Tags for this Thread