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!!!

Recommended Answers

All 10 Replies

It compiled for me. What exactly is the problem?

To me, it looks as if the file you are trying to read from is set up differently than what you have coded to read it. the nextInt() method (if im not mistaken) checks the next position in the file, after what it has currently read. If the next group of characters separated by spaces is NOT a number, nextInt() with throw a mismatch exception like you got. I hope that made it a bit more clear.
I think you have the structure right, method calls just aren't lining up with your file... Look over your file structure, and see if it matches up with what your code is asking for =]. You might want to look at the hasNext() and hasNextInt() methods too, to make sure that you about to refrence to the correct object type

I keep getting theese errors...
Exception in thread "main" java.io.FileNotFoundException: shape.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at sample2.AdditionalMethods.main(AdditionalMethods.java:25)

I keep getting theese errors...
Exception in thread "main" java.io.FileNotFoundException: shape.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at sample2.AdditionalMethods.main(AdditionalMethods.java:25)

Make sure the file exists and is located in the correct directory. Is it "shape.dat" or "shapes.dat" (the code says "shapes.dat")?

its shapes.dat I accidently erased the "s". I have a copt of the shapes.dat under the System library and also the src folder.

its shapes.dat I accidently erased the "s". I have a copt of the shapes.dat under the System library and also the src folder.

Are you using/running it from NetBeans? If so, try putting it in the project's directory (the directory ABOVE the src directory).

Im useing Eclipse. But it is there to. That is why I am so confused!!

Im useing Eclipse. But it is there to. That is why I am so confused!!

Add this line in red:

File f = new File("shapes.dat");
System.out.println (f.getAbsolutePath());

I'm not familiar with Eclipse and where you're supposed to put the files, but this will tell you where it's looking. Make sure you have the file there.

Thank you!!!!! I got it now. Now the only problem is it only draws the rectangle haha

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.