NEED IMMEDIATE HELP PLEASE!!! Java Program Alright so I have an assignment here
ID: 3857206 • Letter: N
Question
NEED IMMEDIATE HELP PLEASE!!! Java Program
Alright so I have an assignment here that is due in less than two hours and for some reason, I can't get the damned program to run and I feel like pulling my hair out. The program is supposed to read data from this text file: ProductInventory.txt with the following text
70 Gourmet Popcorn 2.80 50
71 Veggie Burgers 5.00 60
72 Italian Bread 4.00 70
73 Electrolyte Water 2.20 80
74 Shaved Parmesan 8.00 90
75 Lemonade 1.50 40
76 High Protein Bars 1.30 30
77 Ginger Shampoo 7.50 20
78 Blueberry Toothpaste 3.00 10
79 Goodnight Candles 2.50 100
But for some reason, I keep getting an error back from my application saying this: java.util.nosuchelementexception. This is the code that is accesses and utilizes the text file in question.
HW5.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
// Define a class
public class HW5
{
// Declare a array variable
static Product prd[] = new Product[20];
public static void PerformOperation(int Op,int num )
{
Scanner scan = new Scanner(System.in);
}
// Implement a method
public static void DisplayMenu()
{
System.out.println("Half Foods Menu ");
System.out.println("1 - Sell Product ");
System.out.println("2 - Order Product ");
System.out.println("3 – List Product I");
System.out.println("4 – Exit");
System.out.println("Enter Your Choice:");
}
// Define a main program
public static void main(String args[])
{
int i=0, Code,numb,Options;
double Cost;
String Name;
// Read a text file
File fileLoc = new File("H:\ProductInventoryIn.txt");
try
{
Scanner scan = new Scanner(fileLoc);
Scanner scan1= new Scanner(System.in);
// checks loop condition
while(scan.hasNextLine())
{
Code = scan.nextInt();
Name = scan.next();
while(!scan.hasNextDouble())
Name= Name +" "+scan.next();
Cost = scan.nextDouble();
numb = scan.nextInt();
prd[i] = new Product(Code,Name,Cost,numb);
i++;
}
DisplayMenu();
Options = scan1.nextInt();
int n=10;
while(Options!=4)
{
int Qty;
if(Options==1)
{
System.out.println("Enter The Code");
Code = scan.nextInt();
while(true)
{
for(i=0;i {
if(prd[i].GetCode()==Code)
break;
}
if(i>=n)
{
System.out.println("Enter Valid Code");
Code = scan.nextInt();
continue;
}
break;
}
System.out.println("Enter The Quantity");
Qty = scan.nextInt();
while(Qty>prd[i].GetCount())
{
System.out.println("Enter Valid Quantity");
Qty = scan.nextInt();
}
double dbl;
int ic;
dbl=prd[i].GetInventoryValue() - (Qty* prd[i].GetCost());
ic=prd[i].GetInventoryCount() - Qty;
numb=prd[i].GetCount() - Qty;
prd[i].SetInventoryValue(dbl);
prd[i].SetInventoryCount(ic);
prd[i].SetCount(numb);
System.out.println("Code - "+Code);
System.out.println("Quantity - "+Qty);
System.out.println("Revenue From Sale - $"+Qty* prd[i].GetCost());
}
else
if(Options==2)
{
System.out.println("Enter The Code");
Code = scan.nextInt();
while(true)
{
for(i=0;i {
if(prd[i].GetCode()==Code)
break;
}
if(i>=n)
{
System.out.println("Enter Valid Code");
Code = scan1.nextInt();
continue;
}
break;
}
System.out.println("Enter The Quantity");
Qty = scan.nextInt();
while(Qty>prd[i].GetCount())
{
System.out.println("Enter Valid Quantity");
Qty = scan.nextInt();
}
double dbl1;
int ic;
dbl1=prd[i].GetInventoryValue() +(Qty* prd[i].GetCost());
ic=prd[i].GetInventoryCount() +Qty;
numb=prd[i].GetCount() +Qty;
prd[i].SetInventoryValue(dbl1);
prd[i].SetInventoryCount(ic);
prd[i].SetCount(numb);
System.out.println("Code - "+Code);
System.out.println("Quantity Ordered - "+Qty);
System.out.println("Cost Of Order - $"+Qty* prd[i].GetCost());
}
else
{
for(i=0;i {
System.out.println(prd[i].toString());
}
System.out.println("Inventory Count - "+prd[i].InventoryCount);
System.out.println("Inventory Value - "+prd[i].InventoryValue);
System.out.println("Product Count - "+prd[i].ProductCount);
}
DisplayMenu();
Options = scan.nextInt();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Product.java
// Define a class
public class Product
{
// define a required variables
static int ProductCount=0;
static double InventoryValue=0;
static int InventoryCount=0;
private int Code;
private String Name;
private double Cost;
private int Count;
// Define a Method
public Product()
{
ProductCount = ProductCount + 1;
Code = -1;
Name = "(not set)";
Cost = -1;
Count = -1;
}
// Define a Method
public Product(int C,String N, double cost,int Ctr)
{
ProductCount = ProductCount + 1;
InventoryValue = InventoryValue + (Cost * Ctr);
InventoryCount = InventoryCount + Ctr;
Code=C;
Name=N;
Cost=cost;
Count =Ctr;
}
public static int GetProductCount()
{
return ProductCount;
}
public static double GetInventoryValue()
{
return InventoryValue;
}
public static int GetInventoryCount()
{
return InventoryCount;
}
public int GetCode()
{
return Code;
}
public String GetName()
{
return Name;
}
public double GetCost()
{
return Cost;
}
public int GetCount()
{
return Count;
}
public static void SetProductCount(int prd )
{
InventoryCount = prd;
}
public static void SetInventoryValue(double inv )
{
InventoryValue=inv;
}
public void SetInventoryCount(int inc )
{
InventoryCount = inc;
}
public void SetCode(int cd )
{
Code = cd;
}
public void SetName(String N )
{
Name= N;
}
public void SetCost(double c )
{
Cost = c;
}
public void SetCount(int c )
{
Count = c;
}
public boolean Equals(Product prd)
{
if(Code==prd.Code)
return true;
return false;
}
public String ToString()
{
return +Code + " "
+Name + " "
+Cost + " "
+Count;
}
}
Can anyone give me a hand here and tell me what is going on with my code?:
Explanation / Answer
It's working now, you have some incomplete for loops which are giving you the exceptions
There were three incomplete for loops, line-71, 120, 161 and on line 180 some unwanted statement was written which I commented out.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
// Define a class
public class HW5
{
// Declare a array variable
static Product prd[] = new Product[20];
public static void PerformOperation(int Op,int num )
{
Scanner scan = new Scanner(System.in);
}
// Implement a method
public static void DisplayMenu()
{
System.out.println("Half Foods Menu ");
System.out.println("1 - Sell Product ");
System.out.println("2 - Order Product ");
System.out.println("3 – List Product I");
System.out.println("4 – Exit");
System.out.println("Enter Your Choice:");
}
// Define a main program
public static void main(String args[])
{
int i=0, Code,numb,Options;
double Cost;
String Name;
// Read a text file
File fileLoc = new File("H:\ProductInventoryIn.txt");
try
{
Scanner scan = new Scanner(fileLoc);
Scanner scan1= new Scanner(System.in);
// checks loop condition
while(scan.hasNextLine())
{
Code = scan.nextInt();
Name = scan.next();
while(!scan.hasNextDouble())
Name= Name +" "+scan.next();
Cost = scan.nextDouble();
numb = scan.nextInt();
prd[i] = new Product(Code,Name,Cost,numb);
i++;
}
DisplayMenu();
Options = scan1.nextInt();
int n=10;
while(Options!=4)
{
int Qty;
if(Options==1)
{
System.out.println("Enter The Code");
Code = scan.nextInt();
while(true)
{
for(i=0;i<prd.length;i++) {
if(prd[i].GetCode()==Code)
break;
}
if(i>=n)
{
System.out.println("Enter Valid Code");
Code = scan.nextInt();
continue;
}
break;
}
System.out.println("Enter The Quantity");
Qty = scan.nextInt();
while(Qty>prd[i].GetCount())
{
System.out.println("Enter Valid Quantity");
Qty = scan.nextInt();
}
double dbl;
int ic;
dbl=prd[i].GetInventoryValue() - (Qty* prd[i].GetCost());
ic=prd[i].GetInventoryCount() - Qty;
numb=prd[i].GetCount() - Qty;
prd[i].SetInventoryValue(dbl);
prd[i].SetInventoryCount(ic);
prd[i].SetCount(numb);
System.out.println("Code - "+Code);
System.out.println("Quantity - "+Qty);
System.out.println("Revenue From Sale - $"+Qty* prd[i].GetCost());
}
else
if(Options==2)
{
System.out.println("Enter The Code");
Code = scan.nextInt();
while(true)
{
for(i=0;i<prd.length;i++) {
if(prd[i].GetCode()==Code)
break;
}
if(i>=n)
{
System.out.println("Enter Valid Code");
Code = scan1.nextInt();
continue;
}
break;
}
System.out.println("Enter The Quantity");
Qty = scan.nextInt();
while(Qty>prd[i].GetCount())
{
System.out.println("Enter Valid Quantity");
Qty = scan.nextInt();
}
double dbl1;
int ic;
dbl1=prd[i].GetInventoryValue() +(Qty* prd[i].GetCost());
ic=prd[i].GetInventoryCount() +Qty;
numb=prd[i].GetCount() +Qty;
prd[i].SetInventoryValue(dbl1);
prd[i].SetInventoryCount(ic);
prd[i].SetCount(numb);
System.out.println("Code - "+Code);
System.out.println("Quantity Ordered - "+Qty);
System.out.println("Cost Of Order - $"+Qty* prd[i].GetCost());
}
else
{
for(i=0;i<prd.length;i++) {
System.out.println(prd[i].toString());
}
System.out.println("Inventory Count - "+prd[i].InventoryCount);
System.out.println("Inventory Value - "+prd[i].InventoryValue);
System.out.println("Product Count - "+prd[i].ProductCount);
}
DisplayMenu();
Options = scan.nextInt();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//Product.java
// Define a class
public class Product
{
// define a required variables
static int ProductCount=0;
static double InventoryValue=0;
static int InventoryCount=0;
private int Code;
private String Name;
private double Cost;
private int Count;
// Define a Method
public Product()
{
ProductCount = ProductCount + 1;
Code = -1;
Name = "(not set)";
Cost = -1;
Count = -1;
}
// Define a Method
public Product(int C,String N, double cost,int Ctr)
{
ProductCount = ProductCount + 1;
InventoryValue = InventoryValue + (Cost * Ctr);
InventoryCount = InventoryCount + Ctr;
Code=C;
Name=N;
Cost=cost;
Count =Ctr;
}
public static int GetProductCount()
{
return ProductCount;
}
public static double GetInventoryValue()
{
return InventoryValue;
}
public static int GetInventoryCount()
{
return InventoryCount;
}
public int GetCode()
{
return Code;
}
public String GetName()
{
return Name;
}
public double GetCost()
{
return Cost;
}
public int GetCount()
{
return Count;
}
public static void SetProductCount(int prd )
{
InventoryCount = prd;
}
public static void SetInventoryValue(double inv )
{
InventoryValue=inv;
}
public void SetInventoryCount(int inc )
{
InventoryCount = inc;
}
public void SetCode(int cd )
{
Code = cd;
}
public void SetName(String N )
{
Name= N;
}
public void SetCost(double c )
{
Cost = c;
}
public void SetCount(int c )
{
Count = c;
}
public boolean Equals(Product prd)
{
if(Code==prd.Code)
return true;
return false;
}
public String ToString()
{
return +Code + " "
+Name + " "
+Cost + " "
+Count;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.