I'm currently writing a program where the program reads in words from a .txt file and stores the data into an array. The user then has the ability to enter in entries into array and the program then writes everything to the txt file where it can be read again. As well as do various other fuctions...Yet, I got every other part of it working but then i came up with the idea for the program to display the total number of elements active within the array. I did this so if the user prints out all the data to the screen or someone were to look into the file they'd know how many entries there are.
A sample file would look like this(3 is the total number within the current array/number of entries:

3 
Jane Hook 1 02 11 1998
Sam golden 2 11 12 1989
Jim carl 3 12 30 1991

So I created Get/Set methods to handle the 3 when it came up with errors but now I cannot get the class that it reads in from to skip the first line of the file and just read the entries effectivly.

 //This is main:Assume all the proper declaritions and imports have been done.
 Scanner sf = null;
 new 

        try
        {
            sf = new Scanner(new FileReader(filename));
        }
        catch (Exception e)
        {
            System.out.printf("ERROR no file found");
        }
        class.Setnumelements(sf);

        class.Store(sf,linenumber);



public void Store(Scanner s,int linenumber)
    {

                do
                {
                    m_Patient[count].Read(s); 
                    count++;
                    m_NumElements++;
                }while(linenumber > count);

            }  
        }     




Code blocks are created by indenting at least 4 spaces
... and can span multiple lines

So does anyone know anything that I scanner can do to SKIP over the first line of the txt file which would allow this code to work? Otherwise it'll get a inputmismatch because the first thing m_Patient reads in is a string.

scanner can do to SKIP over the first line of the txt file

Read the first line and ignore it.

    public void Store(Scanner s,int linenumber)

        {
            int pc;
            pc = s.nextInt();

                do
                {
                    m_Patient[count].Read(s); 
                    count++;
                    m_NumElements++;
                }while(linenumber > count);


        }  

Okay so if were to do something like this I still recieve an InputMismatchException I'm not really sure why..
Also I don't think reading it in and then ignoring it is good because I still need that value to display later.

Edited 4 Years Ago by jamie6441

scanner can do to SKIP over the first line

Can you define what you want to do? If you need to display is later, then save it.

If you use nextLine() to read a line from the file you won't get the exception.

Can you define what you want to do? If you need to display is later, then save it.

If you use nextLine() to read a line from the file you won't get the exception.

Okay I want to take the sample file and read in the first int so it can be saved for later. Then the program will read in each record and store it in an array. When a user manually adds a record it would increment the number and when the program terminates that same number would be used. I feel like this should be simple I've tried what you said before like so:

    public void Store(Scanner s,int linenumber)
        {
            String d;
            d = s.nextLine();
                    do
                    {
                        m_Patient[count].Read(s); 
                        count++;
                        m_NumElements++;
                    }while(linenumber > count);

        }  

And now I recieve a NoSuchElementException on the m_Patient line...I'm not sure why I feel like its one of those things that's so small you easily overlook it >.<

Edited 4 Years Ago by jamie6441

Yeah I figured it out. The s.nextLine(); worked it was just that linenumber was wrong and make it loop past what the element has in the array.

This question has already been answered. Start a new discussion instead.