I getting an error from a code that I have been working on. The error is
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at sample2.AdditionalMethods.main(AdditionalMethods.java:57)
My code for the information is
package sample2;
public class shape {
private String Name;
private String type;
private int length, width, radius;
public shape ()
{
length = 0;
width = 0;
Name = "unknown";
type = "";
}
public void setname(String n)
{
Name = n;
}
public void settype(String t)
{
type = t;
}
public void setlength(int l){
length = l;
}
public void setwidth(int w){
width = w;
}
public void setradius(int r){
radius = r;
}
public String getname(){
return Name;
}
public String gettype(){
return type;
}
public int getlength(){
return length;
}
public int getwidth()
{
return width;
}
public int getradius()
{
return radius;
}
//setters/getters.
}
and the second part is
package sample2;
//import DrawableShape;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.IOException;
public class AdditionalMethods {
/**
* @param args
*/
public static void main(String[] args)throws IOException, FileNotFoundException {
// TODO Auto-generated method stub
File f = new File("shapes.dat");
Scanner infile = new Scanner(f);
String tempname, temptype;
int templ=0, tempw=0;
shape [] allshapes;
int numofshapes = infile.nextInt();
allshapes = new shape[numofshapes];
for (int i = 0; i < numofshapes; i++)
allshapes[i] = new shape();
System.out.println(" we now have " + numofshapes + " shape objects");
for (int i = 0; i < numofshapes; i++)
{
tempname =infile.next();
temptype = infile.next();
allshapes[i].setname(tempname);
allshapes[i].settype(temptype);
if (temptype.equals("R"))
{
templ = infile.nextInt();
tempw = infile.nextInt();
allshapes[i].setlength(templ);
allshapes[i].setwidth(tempw);
}
templ = infile.nextInt();
tempw = infile.nextInt();
System.out.println(" the information read is " + tempname+ " " + temptype+ " " + templ+ " " + tempw);
}
JFrame Win = new JFrame(" my first graphics program");
Win.setSize(500, 500);
Win.setLocation(100,100);
Win.setVisible(true);
Container content = Win.getContentPane();
Graphics g;
g = content.getGraphics();
int drawshape = 0;
content.setBackground(Color.YELLOW);
JOptionPane.showConfirmDialog(null, " Do you want to draw pictures?");
String shapetype = JOptionPane.showInputDialog(null,"Please type 1 for " + allshapes[1].getname()
+ "2 for " + allshapes[1].getname()
+ "3 for " + allshapes[1].getname()
+ "4 for" + allshapes[1].getname());
switch (drawshape)
{
case 1:
shapetype = allshapes[0].gettype();
if (shapetype.equals ("R"));
g.drawRect(50, 50, allshapes[0].getwidth(), allshapes[0].getlength());
if (shapetype.equals ("C"));
g.drawRect(50, 50, allshapes[0].getradius(), allshapes[0].getradius());
if (shapetype.equals ("E"));
g.drawRect(50, 50, allshapes[0].getwidth(), allshapes[0].getlength());
case 2:
shapetype = allshapes[1].gettype();
if (shapetype.equals ("R"));
g.drawRect(50, 50, allshapes[1].getwidth(), allshapes[1].getlength());
if (shapetype.equals ("C"));
g.drawRect(50, 50, allshapes[1].getradius(), allshapes[1].getradius());
if (shapetype.equals ("E"));
g.drawRect(50, 50, allshapes[1].getwidth(), allshapes[1].getlength());
case 3:
shapetype = allshapes[2].gettype();
if (shapetype.equals ("R"));
g.drawRect(50, 50, allshapes[2].getwidth(), allshapes[2].getlength());
if (shapetype.equals ("C"));
g.drawRect(50, 50, allshapes[2].getradius(), allshapes[2].getradius());
if (shapetype.equals ("E"));
g.drawRect(50, 50, allshapes[2].getwidth(), allshapes[2].getlength());
case 4:
shapetype = allshapes[3].gettype();
if (shapetype.equals ("R"));
g.drawRect(50, 50, allshapes[3].getwidth(), allshapes[3].getlength());
if (shapetype.equals ("C"));
g.drawRect(50, 50, allshapes[3].getradius(), allshapes[3].getradius());
if (shapetype.equals ("E"));
g.drawRect(50, 50, allshapes[3].getwidth(), allshapes[3].getlength());
}
g.drawRect(50, 50, tempw, templ);
JOptionPane.showConfirmDialog(null, " Do you want to exit the picture windown?");
Win.dispose();
}
}
This is my first time posting so I hope I am doing this correct!!!