Need Some Help When I Run The Program I Get This Error Message Exception in thre
ID: 3672072 • Letter: N
Question
Need Some Help When I Run The Program I Get This Error Message
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at Driver.main(Driver.java:101)
//Driver.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
String fileName = "items.txt";
Scanner scanner = null;
ItemsList itemsList = new ItemsList(5);
int i = 0;
try
{
scanner = new Scanner(new File(fileName));
while(scanner.hasNextLine())
{
String itemName = scanner.next();
double price = scanner.nextDouble();
int qty = scanner.nextInt();
//Add the OneItem object to the itemList
itemsList.addItem(new OneItem(itemName, price, qty));
i++;
}
//close the file objct
scanner.close();
//print the list of the items in itemsList
System.out.println(itemsList.toString());
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
boolean repeat=true;
//Create an instance of Scanner class
scanner = new Scanner(System.in);
while(repeat)
{
int choice = menu();
switch (choice)
{
//Add an item to the itemsList
case 1:
System.out.println("Enter item name : ");
String name = scanner.nextLine();
System.out.println("Enter price : ");
double price = Double.parseDouble(scanner.nextLine());
System.out.println("Enter quantity : ");
int quantity = Integer.parseInt(scanner.nextLine());
//Add the OneItem to the itemsList
itemsList.addItem(new OneItem(name, price, quantity));
break;
case 2:
//print the list
System.out.println(itemsList.toString());
break;
case 3:
//Terminate the program
System.out.println("Exit the program.");
//set repeat to false
repeat=false;
break;
default:
System.out.println("Error!! Try Again");
break;
}
}
writeToFile(itemsList);
}
private static void writeToFile(ItemsList itemsList)
{
//Create a file name called items.txt
String filename = "items.txt";
//Create a variable of Class PrintWriter
PrintWriter filewriter = null;
try
{
//create an instance of PrintWriter
filewriter = new PrintWriter(new File(filename));
filewriter.println(itemsList.toString());
//close the filewriter
filewriter.close();
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}
//Method : Menu
private static int menu()
{
//Create an instance of Scanner class
Scanner keyboard=new Scanner(System.in);
System.out.println(" Menu ");
System.out.println(" 1. Add item");
System.out.println(" 2. Display items");
System.out.println(" 3. Exit");
System.out.println(" Enter your choice");
int choice=Integer.parseInt(keyboard.nextLine());
return choice;
}
}//end Driver class
---------------------------
public class ItemsList
{
//declare variables
private OneItem items[];
private int size;
private int count;
//constructor to set items, size and count to zero
public ItemsList()
{
items = null;
size = 0;
count = 0;
}
//Parameter constructor
public ItemsList(int size)
{
items = new OneItem[size];
for (int i = 0; i < items.length; i++)
{
items[i] = new OneItem();
}
this.size = size;
count = 0;
}
//Add OneItem to the itemlist
public void addItem(OneItem item)
{
if(items.length == count)
{
resize();
items[count] = item;
count++;
}
else
{
items[count] = item;
count++;
}
}
//Resize
private void resize()
{
int oldsize = size;
count = oldsize;
int newsize = 2 * this.size;
size = newsize;
OneItem[] tempList = new OneItem[size];
for (int i = 0; i < oldsize; i++)
tempList[i] = items[i];
items = new OneItem[size];
items = tempList;
}
//getSize
public int getSize()
{
return count;
}
//toString
public String toString()
{
String description = "";
description += String.format("%-10s%-10s%-10s ", "Item","Price","Quantity");
for (int i = 0; i <count; i++)
{
description += items[i].toString();
}
return description;
}
}
-------------------------------------------
//OneItem.java
public class OneItem
{
//declare a variables
private String name;
private double price;
private int quantity;
//default constructor
public OneItem()
{
name="";
price=0;
quantity=0;
}
//parameter constructor
public OneItem(String name, double price, int quantity)
{
this.name=name;
this.price=price;
this.quantity=quantity;
}
//Override the toString
@Override
public String toString() {
return String.format("%-10s%-10.2f%-10d ", name,price,quantity);
}
}//end of the OneItem class
Explanation / Answer
InputMismatchException: Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.
You need to check the content of "items.txt". They should comply with "String Double Int". e.g. Sam 12.2 90
I think you want to do this on your own. If you need full code let me know in comments.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.