I get an unknown source error when I try to use the Scanner class.
fabric is the name of my Scanner ojbect.

while(fabric.hasNext())
                        {

                            fabricID[fabricCounter] = fabric.nextInt();
                            name[fabricCounter] = fabric.next();
                            description[fabricCounter] = fabric.next();
                            price[fabricCounter] = fabric.nextDouble();
                            qty[fabricCounter] = fabric.nextInt();
                            vendor[fabricCounter] = fabric.next();
                            fabricCounter++;
                            System.out.println("made it down here");
                        }//end while
                    fabric.close();
                }//end try
            catch(IOException e) 
                {
                    System.out.println("Nope on the reader");
                }//end catch
            System.out.println(fabricID[2]);
            System.out.println(name[2]);
            System.out.println(description[2]);
            System.out.println(price[2]);
            System.out.println(qty[2]);
            System.out.println(vendor[2]);

But if I change my while(fabric.hasNext()) to while(fabric.hasNextInt()) it skips over the whole while section.
What am I missing with Scanner? Any help is greatly appreciated.

Recommended Answers

All 9 Replies

might be in the initialization of that Scanner instance.
without seeing more of the code, or the error message, don't think we can do much to help.

Here's the whole class. I didn't want to inundate with extra code, but if it helps.

package BonitaBoutique;
import java.util.*;
import java.io.*;

public class ReadFromFiles {

    //variable declaration
    Scanner fabric;
    Scanner items;
    Scanner orders;
    String holder;
    private int arraySize = 1000;

    //variables needed for fabrics

    private int fabricCounter;
    private int [] fabricID;
    private String [] name; 
    private String [] description;
    private String [] vendor;
    private double [] price;
    private int [] qty;

    public ReadFromFiles() 
        {

            String itemsFile = "items.txt";
            String ordersFile = "orders.txt";
            holder = "";

            //variables needed for Fabrics
            description = new String[arraySize];
            fabricID = new int[arraySize];
            String fabricsFile = "fabrics.txt";
            name = new String[arraySize];
            price = new double[arraySize];
            qty = new int[arraySize];
            vendor = new String[arraySize];

            readFabrics(fabricsFile);

        }//end of method

    public void readFabrics(String fabricsFile) 
        {
            //variable declaration
            fabricCounter = 0;

            try 
                {
                    fabric = new Scanner(new File(fabricsFile));
                    fabric.useDelimiter("@#$");
                    while(fabric.hasNext())
                        {

                            fabricID[fabricCounter] = fabric.nextInt();
                            name[fabricCounter] = fabric.next();
                            description[fabricCounter] = fabric.next();
                            price[fabricCounter] = fabric.nextDouble();
                            qty[fabricCounter] = fabric.nextInt();
                            vendor[fabricCounter] = fabric.next();
                            fabricCounter++;
                            System.out.println("made it down here");
                        }//end while
                    fabric.close();
                }//end try
            catch(IOException e) 
                {
                    System.out.println("Nope on the reader");
                }//end catch
            System.out.println(fabricID[2]);
            System.out.println(name[2]);
            System.out.println(description[2]);
            System.out.println(price[2]);
            System.out.println(qty[2]);
            System.out.println(vendor[2]);
    }//end method

    public void readItems(String itemsFile) 
        {

        }//end method



    public void readOrders(String ordersFile) 
        {

        }//end method

}//end of class

have you tried printing the stacktrace in the catchblock? have you verified that the file is actually found?
you may also have miscounted the number of columns / lines in your input file, or use a next where you should 've used a nextInt or something like that.

I got it to work...kind of. I am treating everything as a String for now, then parsing it into int and double as necessary. However, it reads the entire file in the first fabric.next(). It seems to ingore the delimiter.

fabric = new Scanner(new File(fabricsFile));
                    fabric.useDelimiter("@#$");
                    while(fabric.hasNext())
                        {
                            tempFabricID = fabric.next();
                            //fabricID[fabricCounter] = Integer.parseInt(tempFabricID);
                            //name[fabricCounter] = fabric.next();
                            //description[fabricCounter] = fabric.next();
                            //tempPrice = fabric.next();
                            //price[fabricCounter] = Double.parseDouble(tempPrice);
                            //tempqty = fabric.next();
                            //qty[fabricCounter] = Integer.parseInt(tempqty);
                            //vendor[fabricCounter] = fabric.next();
                            //fabricCounter++;
                            //fabric.nextLine();
                            System.out.println("made it down here");
                        }//end while
                    fabric.close();
                }//end try
            catch(Exception e) 
                {
                    System.out.println(e);
                }//end catch
            System.out.println(tempFabricID);
            System.out.println(name[2]);
            System.out.println(description[2]);
            System.out.println(price[2]);
            System.out.println(qty[2]);
            System.out.println(vendor[2]);

might be, but it could also be your input file is not the way the code thinks it does, based on that delimeter.
can you show a bit of your input file here?

Of course. The file I am parsing is:

1000@#$Darrel@#$Darrel is Awesome!@#$2.99@#$23@#$Walmart@#$
1001@#$DArrel@#$Darrel is Awesome!@#$2.98@#$23@#$WalMart@#$
1002@#$DaRRel@#$Darrel IS Awesome!@#$2.99@#$23@#$Walmart@#$
1003@#$DarrEl@#$Darrel is Awesome!@#$2.99@#$93@#$Walmart@#$

The delimiter you specify for a Scanner is not just a String, it's a Regex pattern, but $ is a special character in a regex, so maybe your delimiter is being parsed in a way you didn't expect.
Have a look at the API doc for Pattern for more details

That was exactly the problem! I changed it to simply @# and it worked perfectly!

Thank you.

Hey, that's great!
Please mark this "solved" for our knowledge base - others may hit the same problem in future.
J

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.