Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with these java errors? Exception in thread main java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at Homework5.HW5.addProduct(HW5.java:208) at Homework5.HW5.main(HW5.java:40) Here is my code. Product.java

Can someone help me with these java errors?

Exception in thread "main" java.util.NoSuchElementException

at java.util.StringTokenizer.nextToken(Unknown Source)

at Homework5.HW5.addProduct(HW5.java:208)

at Homework5.HW5.main(HW5.java:40)

Here is my code.

Product.java

public class Product

{

// declaring and setting product count to 0

private static int productCount = 0;

// declaring and setting inventory value to 0

private static float inventoryValue = 0;

//declaring and setting inventory count to 0

private static int inventoryCount = 0;

//declaring all other variables.

private String name;

private int code;

private float cost;

private int count;

//constructor to set all variables

public Product()

{

productCount = productCount + 1;

code = -1;

name = "(not set)";

cost = -1;

count = -1;

}

//a constructor with four parameters to set the values

public Product(String givename, int givecode, float givecost,int givecount)

{

productCount = productCount + 1;

inventoryValue = inventoryValue + (cost * count);

inventoryCount = inventoryCount + count;

name = givename;

code = givecode;

cost = givecost;

count = givecount;

}

// getting the methods for each of the four field as well as the variables

public static int getProductCount()

{

return productCount;

}

public static float getInventoryValue()

{

return inventoryValue;

}

public static float getInventoryCount()

{

return inventoryValue;

}

public String getName()

{

return name;

}

public int getCode()

{

return code;

}

public int getCount()

{

return count;

}

public float getCost()

{

return cost;

}

//Setter methods for each field.

public static void setProductCount(int ProductCount)

{

Product.productCount = ProductCount;

}

public static void setInventoryValue(int InventoryValue)

{

Product.inventoryValue = InventoryValue;

}

public static void setInventoryCount(int InventoryValue)

{

Product.inventoryValue = InventoryValue;

}

public void setName(String name)

{

this.name = name;

}

public void setCode(int code)

{

this.code = code;

}

public void setCost(int cost)

{

this.cost = cost;

}

public void setCount(int count)

{

this.count = count;

}

// checks if the two objects are the same.

public boolean equals(int itemToCompare)

{

if (itemToCompare != code)

{

return false;

}

return true;

}

// returning the values

public String toString()

{

return "Name : " + name + " Count : " + count + " Cost : " + cost+ " Code : " + code;

}

}

HW5.java

package Homework5;

public class HW5

{

//main method

public static void main(String[] args) throws FileNotFoundException

{

//scanner to get input from the user

Scanner key = new Scanner(System.in);

// creating a array list

List prodList = new ArrayList();

// call the method addProduct() and pass the object.

addProduct(prodList);

//while loop to output the options to the user

while (true)

{

// prints the options for the user

System.out.println("Enter Your Choice 1. Sell product 2. Order product 3. List products 4. Exit");

// get the input from the user

int input = key.nextInt();

//goes to read the next line

key.nextLine();

//switch

switch (input)

{

// the first case checks if whether we sell this product

case 1:

sellProduct(prodList);

break;

// the second case checks if the product is there

case 2:

OrderProduct(prodList);

break;

//this case displays the list of products

case 3:

System.out.println("\tProducts List ");

listProduct(prodList);

System.out.print(" ");

break;

//this case will output the data the user has entered to the input file with the same layout

case 4:

WriteToFile(prodList);

prodList.clear();

key.close();

System.exit(0);

break;

//default case where we say that this is an invalid input

default:

System.out.println("Error: invalid input");

}

}

}

//this will check if we have the product

public static boolean checkForCode(List list, int code)

{

for (int i = 0; i < list.size(); i++)

{

if (code == list.get(i).getCode())

{

return true;

}

}

return false;

}

//this will sell the product if present in the text file

public static void sellProduct(List list)

{

//scanner to take input from the user

Scanner keyboard = new Scanner(System.in);

//Product p=new Product();

int i=0;

int amount;

//output to the user

System.out.println("Enter Item code: ");

//stores the input and reads the next line

int next = keyboard.nextInt();

keyboard.nextLine();

//if

if (checkForCode(list, next))

{

//output to the user

System.out.println("Enter Item Quantity: ");

//reads in the next int

int second = keyboard.nextInt();

keyboard.nextLine();

int amountsInv;

int ic;

//

amountsInv=(int) (Product.getInventoryValue() - (second* list.get(i).getCost()));

ic=(int) (Product.getInventoryCount() - second);

amount=list.get(i).getCount() - second;

Product.setInventoryValue(amountsInv);

Product.setInventoryCount(ic);

list.get(i).setCount(amount);

//output to the user

System.out.println("Code - "+next);

System.out.println("Quantity - "+second);

System.out.println("Revenue From Sale - $"+second* list.get(i).getCost());

//close scanner

keyboard.close();

}

else

{

System.out.println("The Item does not exist");

}

}

//making the orderProduct method

public static void OrderProduct(List list)

{

//new scanner

Scanner key = new Scanner(System.in);

int i=0;

int amount;

//output to the user

System.out.println("Enter Item code: ");

//stores input from the user

int number = key.nextInt();

key.nextLine();

if (checkForCode(list, number))

{

//output to the user

System.out.println("Enter Item Quantity: ");

////reads in the next int

int second = key.nextInt();

key.nextLine();

//variable declarations

int invVal;

int InvCount;

//

invVal=(int) (Product.getInventoryValue() +(second* list.get(i).getCost()));

InvCount=(int) (Product.getInventoryCount() + second);

amount=list.get(i).getCount() +second;

Product.setInventoryValue(invVal);

Product.setInventoryCount(InvCount);

list.get(i).setCount(amount);

////output to the user

System.out.println("Code - "+number);

System.out.println("Quantity - "+second);

System.out.println("Cost Of Order- $"+second* list.get(i).getCost());

//close scanner

key.close();

}

else

{

System.out.println("The Item does not exist");

}

}

//making the listProduct method

public static int listProduct(List list) throws FileNotFoundException

{

StringTokenizer tokens;

// sets location of the file

String fileName = "ProductInvIn.txt";

Scanner prodInv = new Scanner(new File(fileName));

// reading line by line

while (prodInv.hasNext())

{

String line = prodInv.nextLine();

tokens = new StringTokenizer(line);

String code = tokens.nextToken();

String name = tokens.nextToken();

String cost = tokens.nextToken();

String count = tokens.nextToken();

System.out.printf("%-15s%-45s%-10s%s%n",code,name,cost,count);

//close scanner

prodInv.close();

}

return 0;

}

public static void addProduct(List list)throws FileNotFoundException

{

StringTokenizer tokens;

// Declaring the location of the file

String fileName = "ProductInvIn.txt";

Scanner prodInv = new Scanner(new File(fileName));

// reading line by line

while (prodInv.hasNext())

{

String line = prodInv.nextLine();

tokens = new StringTokenizer(line);

String first = tokens.nextToken();

String name = tokens.nextToken();

String cost = null;

String nextName = tokens.nextToken();

while (!Character.isDigit(nextName.charAt(0)))

{

name = name + " " + nextName;

nextName = tokens.nextToken();

}

cost = nextName;

String count = tokens.nextToken();

list.add(new Product(name, Integer.parseInt(first),

Float.parseFloat(cost), Integer.parseInt(count)));

}

prodInv.close();

}

//making the WriteToFile Method

public static void WriteToFile(List list)

{

PrintWriter fileOut = null;

//declaring a new variable

String fileName = "ProductInventoryOut.txt";

//try method

try

{

fileOut = new PrintWriter(fileName);

//outputs the users data to to file until there is no more user data in the same format as input file.

for (int i = 0; i < list.size(); i++)

{

fileOut.printf("%-15d%-45s%-15.2f%d%n",

list.get(i).getCode(), list.get(i).getName(),

list.get(i).getCost(), list.get(i).getCount());

}

//after finished it will close the file

fileOut.close();

}

//if the file cant be found send a error

catch (FileNotFoundException e)

{

System.out.println("Error: file '" + "ProductInventoryOut.txt"+ "' cannot be created or opened.");

System.out.println("Default folder: " + System.getProperty("user.dir"));

System.out.println("Error message: " + e.getMessage());

}

}

}

ProductInvIn.txt

80 Daypack 110.00 50

81 Duffel Bag 35.00 60

82 Hammock 70.00 70

83 Cot 155.00 80

84 Tent 430.00 90

85 Stove 100.00 40

86 Cooler 350.00 30

87 Sleeping Bag 320.00 20

88 Blanket 140.00 10

89 Camp Chair 120.00 100

ProductInventoryOut.txt

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

More Books

Students also viewed these Databases questions

Question

A form of crowd sourcing is fund sourcing. How does this work?

Answered: 1 week ago

Question

2. Are my sources up to date?

Answered: 1 week ago