Question
here is my program, i having difficulties, it is displaying toppings as null and it not counting the toppings in the dat file. please help
here is my program, i having difficulties, it is displaying toppings as null and it not counting the toppings in the dat file. please help me
pizza.java
import java.util.*;
/** Class to represent the pizza orders. */
public class Pizza {
private String Crust; private String Size; private String [] Toppings; /**Initializes the members of Pizza object object. @param crust a string representing the type of crust on the pizza. @param size string representing the size of the pizz @param toppings string representing the toppings of the pizza **/
public Pizza (String Crust, String Size, String [] Toppings) { this.Crust= Crust; this.Size = Size; this.Toppings= Toppings; } // constructor
/** Assigns a new type of crust @param Crust a string representing the type of crust */ public void setCrust(String Crust) { this.Crust = Crust; } // method setCrust
/** Assigns a new size to the pizza. @param Size a string representing the size of pizza */ public void setSize(String Size) { this.Size = Size; } // method setSize
/** Assigns a new toppings to the pizza. @param topping a string representing the toppings of the pizza */ public void setToppings( String [] Toppings) { this.Toppings = Toppings; } // method setToppings
/** Returns the type of crust of the pizza. @return crust tyoe */ public String getCrust() { return Crust; } // method getCrust
/** Returns the size of the pizza @return pizza size */ public String getSize() { return Size; } // method getSize
/** Returns the pizza toppings. @return pizza toppings */ public String [] getToppings() { return Toppings; } // method getToppings
public double getTotal_Cost() { String[]Toppings= new String[7];
double Topping_Cost=1.50; //constant per topping double Small_Cost=9.50; // cost for small sized pizza double Medium_Cost= 12.25;// cost for medium sized pizza double Large_Cost=14.50; //cost for large size pizza. double Sum_Toppings;// the total cost of toppings double Total_Cost=0;// total cost of pizza double base_cost=0;// base cost of the sizes double Amount=0; int index =0; double Toppings_size=0; if (base_cost==0) { if(Size.equalsIgnoreCase("Small")) { base_cost= Small_Cost; } else if(Size.equalsIgnoreCase("Medium")) { base_cost= Medium_Cost; } else { base_cost=Large_Cost; } while(index<7 && Toppings[index]!= null) { Amount++; index++;
} Total_Cost = base_cost +(Toppings_size* Topping_Cost); }
return Total_Cost;
}// pizza
********************************************
pizza.dat
Thin, medium Spanich, GreenPepper, Sausage hand-tossed, large Pineapple, Mushroom, Tomato, Onion
PizzaArrayList.java
**************************************
public class PizzaArrayList { private ArrayList
public static void main(String[] args) throws IOException { PizzaArrayList current; current = new PizzaArrayList(); current.getStarted(); } // method main /** Starts the program */ public void getStarted() throws FileNotFoundException, IOException { openFile(); readData(); //calculateData(); // sortData(); //displayResults(); infile.close();
} // method getStarted /** Opens the input data file for read access. */
public void openFile() throws FileNotFoundException { Scanner input; String infile_name; File file;
input = new Scanner(System.in); do { System.out.print("Enter the input file name: "); infile_name = input.next();
// open the input data file file = new File(infile_name); if (file.exists()) infile = new Scanner(new File(infile_name)); else System.out.println(infile_name + " does not exist"); } while (!file.exists()); } // method openFile
/** Reads in text from file and save them in an array list. @param infile the file which has been open successfully */ public void readData() {
// instantiate an ArrayList object pizzas = new ArrayList
// read in values from the file and assign them to the elements
int index;
while (infile.hasNextLine()) { Pizza who; String Crust,Size; String[]Toppings =new String[7]; Toppings=new String[7]; String read=infile.nextLine(); StringTokenizer line =new StringTokenizer(read, ","); Crust=line.nextToken(); Size=line.nextToken();
System.out.println("Crust " + Crust + " Size " + Size); read=infile.nextLine(); line =new StringTokenizer(read, ","); index=0; while(line.hasMoreTokens()) { Toppings[index]=line.nextToken(); index++; System.out.println("Toppings " + Toppings[index]); }
// number of toppings of the pizza
who = new Pizza(Crust,Size,Toppings); pizzas.add(who);
} } // method readData
How it is displaying.
***************************************
I am having difficulty with my read funtion
why is my output displaying toppings as null
Crust Thin Size medium
Toppings null
Toppings null
Toppings null
Crust hand-tossed Size large
Toppings null
Toppings null
Toppings null
Toppings null
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java:349)
at PizzaArrayList.readData(PizzaArrayList.java:92)
at PizzaArrayList.getStarted(PizzaArrayList.java:35)
at PizzaArrayList.main(PizzaArrayList.java:26)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started