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

• November 28th, 2012, 01:03 AM
Jaloney
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

Code java:

``` <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.
• November 28th, 2012, 07:26 AM
Norm
Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code
[code=java]
[/code]
to get highlighting and preserve formatting.

Where does the program read any input from the user?
• November 28th, 2012, 11:18 AM
Jaloney
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..
• November 28th, 2012, 11:34 AM
Norm
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.
• November 28th, 2012, 02:43 PM
Jaloney
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

Code java:

``` <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?
• November 28th, 2012, 02:55 PM
Norm
Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code
Quote:

gives me multiple errors
Please copy and paste here the full text of the compiler's error messages.
• November 28th, 2012, 08:45 PM
Jaloney
Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code
Quote:

Originally Posted by Norm
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.
• November 29th, 2012, 12:33 PM
Jaloney
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...
• November 30th, 2012, 09:10 PM
Jaloney
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.

Code java:

``` <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-----");     } } }```
• December 1st, 2012, 07:37 AM
Norm
Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code
Quote:

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.
• December 1st, 2012, 02:04 PM
Jaloney
Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code (cash register program)
Quote:

Originally Posted by Norm
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...[-O<
__________________________________________________ __________________________________________________ ________________________
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.
• December 1st, 2012, 02:13 PM
Norm
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.
• December 1st, 2012, 05:33 PM
Jaloney
Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code
Quote:

Originally Posted by Norm
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
• December 1st, 2012, 05:51 PM
Norm
Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code
Quote:

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.
• December 1st, 2012, 09:07 PM
Jaloney
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.
:)
• December 1st, 2012, 09:13 PM
Norm
Re: Newbie.. Parrell arays with sentienal value . I am given RESULT must write the source code
Quote:

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,