Question

I'm attempting to convert ch object array to double array public void calculateSalesPrice() { double salePrice[]...

I'm attempting to convert ch object array to double array
public void calculateSalesPrice() {
    double salePrice[] = new double[ch.length];
    int p = 0;
    for (int i = 0; i < ch.length; i += 3) {
        p = i + 1;
        for(int j = 0;j
    }
}

error message:items.java:16: error: cannot find symbol
for(int j = 0;j ^
symbol: variable length
location: class Object
items.java:17: error: array required, but Object found
salePrice[j] = (double

full program

import java.io.File;
import java.util.Scanner;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import java.util.Arrays;
public class shoppingCart {
private ArrayList<String>listOfItems;
private String userInput;
private int itemCount;
private int selection_index;
public Object ch[];
public Object shopList, shopPrice;
private int itemsNum;
public int c;
public void shopperName(){
userInput=JOptionPane.showInputDialog(null, "Hello valued customer, please enter your name to continue.");
String userName= userInput;
int dialogButton= JOptionPane.showConfirmDialog ( null,"Welcome "+userInput+". Are you ready to shop?" );
if(dialogButton==JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null, "Okay, Let's shop");
}
  
if(dialogButton== JOptionPane.NO_OPTION ){
JOptionPane.showMessageDialog(null, "Have a great day.");
System.exit(0);   
}
}
public void ListOfItemsAvailable(){
listOfItems= new ArrayList<String>(10);

Scanner itemList = null;
try {
itemList= new Scanner(new File ("list_Of_items.txt"));
while( itemList.hasNext()){
listOfItems.add(itemList.nextLine());
itemCount++;
}
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Error reading available items");
System.exit(1);
}
itemList.close();
}
public void printAvailableItems(){
System.out.println("Enter number in parenthesis to select item");
  
ch=listOfItems.toArray();
int i=0;int j=0;
for(i=0;i<ch.length;i+=3){
j=i+1;
System.out.println(ch[i]+" $" + ch[j] + " ("+ i+ ")");
shopList=ch[i];
shopPrice=ch[j];}

}
public void customerSelection(){// allow customer to make selection
System.out.println(" How many items do you want to buy");
Scanner scan= new Scanner(System.in);
itemsNum=scan.nextInt();
String[] itemsL= new String[itemsNum];

for(int k=0; k<itemsNum; k++){
System.out.println("Enter the number assigned to item: " + (k+1));
itemsL[k]=scan.next();}
scan.close();
for(int k=0; k<itemsNum; k++){
c=Integer.parseInt(itemsL[k]);
System.out.println("The items you selected are: " + ch[c]);
}
}
public void cart(){
Object Kart=ch[c];


}

public static void main( String args[]){
shoppingCart shopper = new shoppingCart();
shopper.shopperName();
shopper.ListOfItemsAvailable();// access list of available items from txt file
shopper.printAvailableItems();// print available items
shopper.cart();// holds the items in customers cart
shopper.customerSelection();// allow customer to select items
items register = new items();
register.calculateSalesPrice();// calculate sales price
register.payment();// accept customer payment

} }

import java.io.File;
import java.util.Scanner;
public class items extends shoppingCart{
private Object discount;
String itemName; double itemPrice; String ItemOnsale;
public items(){
}
public void calculateSalesPrice(){
double salePrice[]= new double[ch.length];
  
int p=0; int d=0;
for(int i=0;i<ch.length;i+=3){
p=i+1;
d=i+2;   
discount=ch[d];}
for(int j = 0;j<ch[p].length;j++){
salePrice[j] = (double) ch[p][j];   
}
}
public void payment(){
}    
}
txt file:

popcorn
3.50
yes
chips
2.00
no
corn curls
1.10
no

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You can do this by having another loop variable j and increment it by 1 on each loop. Since price is at i+1 location, we can fill salePrice[] array as below:

for (int i = 0, j = 0; i < ch.length; i += 3, j++) {
            p = i + 1;
            d = i + 2;
            discount = ch[d];

            salePrice[j] = (double) ch[p]; // since price is at location p
}

In your code, you are trying to access 2d array ch which does not exist as ch is the single dimension array.

salePrice[j] = (double) ch[p][j];    // wrong

This completes the requirement. Let me know if you have any queries.

Thanks!

Add a comment
Know the answer?
Add Answer to:
I'm attempting to convert ch object array to double array public void calculateSalesPrice() { double salePrice[]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • ​I have to create two classes one class that accepts an object, stores the object in...

    ​I have to create two classes one class that accepts an object, stores the object in an array. I have another that has a constructor that creates an object. Here is my first class named "ShoppingList": import java.util.*; public class ShoppingList {    private ShoppingItem [] list;    private int amtItems = 0;       public ShoppingList()    {       list=new ShoppingItem[8];    }       public void add(ShoppingItem item)    {       if(amtItems<8)       {          list[amtItems] = ShoppingItem(item);...

  • Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{       ...

    Cant figure out how to fix error Code- import java.io.File; import java.io.IOException; import java.util.*; public class Program8 {    public static void main(String[] args)throws IOException{        File prg8 = new File("program8.txt");        Scanner reader = new Scanner(prg8);        String cName = "";        int cID = 0;        double bill = 0.0;        String email = "";        double nExempt = 0.0;        String tExempt = "";        int x = 0;        int j = 1;        while(reader.hasNextInt()) {            x = reader.nextInt();}        Customers c1 [] = new Customers [x];        for (int...

  • Below, you can find the description of your labwork for today. You can also find the...

    Below, you can find the description of your labwork for today. You can also find the expected output of this code in the Application Walkthrough section. You are going to improve your existing Money & Stock Trading Platform on previous week’s labwork by incorporating Collections. In previous labworks, you have used arrays for holding Customer and Item objects. For this labwork you need to use ArrayList for holding these objects. So, rather than defining Customer[] array, you need to define...

  • Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)   ...

    Currently, I'm getting this as my output: "Exception in thread "main" java.lang.NullPointerException    at InfixExpression.execute(InfixExpression.java:98)    at InfixExpression.Evaluate(InfixExpression.java:65)    at InfixExpression.setWholeExpr(InfixExpression.java:24)    at InfixExpression.<init>(InfixExpression.java:17)    at Main.testHW1(Main.java:17)    at Main.main(Main.java:6)" I need to get this as my output: "Testing InfixExpression: When passing null, the String and double = Infix String: , result: 0.0 When passing a valid String, the String and double = Infix String: ( 234.5 * ( 5.6 + 7.0 ) ) / 100.2, result: 29.488023952095805 ..." I...

  • import java.util.Arrays; public class lab {    public static void main(String args[])    {    int...

    import java.util.Arrays; public class lab {    public static void main(String args[])    {    int arr[] = {10, 7, 8, 9, 1, 5,6,7};    int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1};    int arr3[] = {1, 3, 5, 3, 2, 6, 20};    quicksort(arr,0,arr.length-1);    quicksort(arr2,0,arr2.length-1);    quicksort(arr3,0,arr3.length-1);    System.out.println(Arrays.toString(arr));    System.out.println(Arrays.toString(arr2));    System.out.println(Arrays.toString(arr3));       }    private static int partition(int[] items,int low, int high)    {    int i=0;    int j=0;...

  • Can anyone helps to create a Test.java for the following classes please? Where the Test.java will...

    Can anyone helps to create a Test.java for the following classes please? Where the Test.java will have a Scanner roster = new Scanner(new FileReader(“roster.txt”); will be needed in this main method to read the roster.txt. public interface List {    public int size();    public boolean isEmpty();    public Object get(int i) throws OutOfRangeException;    public void set(int i, Object e) throws OutOfRangeException;    public void add(int i, Object e) throws OutOfRangeException; public Object remove(int i) throws OutOfRangeException;    } public class ArrayList implements List {   ...

  • this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea {...

    this is a jave program please help, I'm so lost import java.util.Scanner; public class TriangleArea { public static void main(String[] args) { Scanner scnr = new Scanner(System.in);    Triangle triangle1 = new Triangle(); Triangle triangle2 = new Triangle(); // Read and set base and height for triangle1 (use setBase() and setHeight())    // Read and set base and height for triangle2 (use setBase() and setHeight())    // Determine larger triangle (use getArea())    private int base; private int height; private...

  • ***\\\\I have been trying to run this but I think there is something wrong with the...

    ***\\\\I have been trying to run this but I think there is something wrong with the variables not being saved properly. I really appreciate some help. *** class Main { public static void main(String[] args) { final int NUM_ITEMS = 2; ItemToPurchase[] itemList = new ItemToPurchase[NUM_ITEMS];    clearScreen();    promptItem(itemList); total(itemList); }    public static void promptItem(ItemToPurchase[] itemList) { Scanner scnr = new Scanner(System.in);    String tempItem; int tempPrice; int tempQuantity;    int count; //-----Print prompts by number of items------//...

  • (Reading & Writing Business Objects) I need to have my Classes be able to talk to...

    (Reading & Writing Business Objects) I need to have my Classes be able to talk to Files. How do I make it such that I can look in a File for an account number and I am able to pull up all the details? The file should be delimited by colons (":"). The Code for testing 'Select' that goes in main is: Account a1 = new Account(); a1.select(“90001”); a1.display(); Below is what it should look like for accounts 90000:3003:SAV:8855.90 &...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT