I am reading code out of a txt file which contains text in this format..
[text file]
Name of Project
Experiment One
cows, dogs, pigs, horses, sheep, goats
1,1,1,1,20,10
cowweight,dogweight,pigweight,horseweight,sheepweight,goatweight
560,50.5,54.3,641.1,35.4,42.5
deer,elk, moose
2,4,5
etc...
[text file]

I am trying to read in the numeric values to use for a program and am having some problems. My code looks like this

public class OpenFile {
throws FileNotFoundException {
    FileInputStream fis = new FileInputStream(test.txt);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    
    Scanner s= new Scanner(dis);
    try{
        s.nextLine();
        s.nextLine();
        s.nextLine();
        int cow = s.nextInt(); System.out.println(cow);
        int dog = s.nextInt(); System.out.println(dog);
        int pig = s.nextInt(); System.out.println(pig);
        int hor = s.nextInt(); System.out.println(hor);
        int she = s.nextInt(); System.out.println(she);
        int goa = s.nextInt(); System.out.println(goa):
        s.nextLine();
        double coww = s.nextDouble(); System.out.println(coww);
        double dogw = s.nextDouble(); System.out.println(dogw);
        double pigw = s.nextDouble(); System.out.println(pigw);
        double horw = s.nextDouble(); System.out.println(horw);
        double shew = s.nextDouble(); System.out.println(shew);
        double goaw = s.nextDouble(); System.out.println(nmax):
        S.nextLine();
        ///etc. and continues from there

When I run the code I get
1
1
1
1
20
Error: null

and the program ceases to show anything else.
I have run the code with various txt files of the same format and sometimes it will print something like this
1
Error: null
3
3
7
8

Any advice?

A suggestion: Beef up your debug output by IDing what is being printed. For example:
System.out.println("shew=" + shew);

That makes it a lot easier to find the last line printed than having to count down thru the lines of code to find where the problem was.

Edited 6 Years Ago by NormR1: n/a

Thanks. I just did and its showing me something interesting. I now show
cow=1
dog=1
pig=1
hor=1
she=20
Error: null

or

cow=1
Error: null
dog=3
pig=3
hor=7
she=8

I don't know why the error would show in the second line there but it doesn't seem to want to print the last int on that line, goa. I know that the value is an int.

What code generates the "Error: null" output?
What is null? Are you NOT printing out a stack trace in a catch block?

Yes I have and its telling me I have an inputmismatchexception. The line of code that corresponds to line 17 of my code. The line being read in the test file however is "1,1,1,1,20,10" however. I made a loop to help debug

while (s.hasNext()) {
      if (s.hasNextInt()) {
        System.out.println(s.nextInt());
      } else {
        break;

and it printed
1
1
1
1
20


I don't understand why it wont recognize the last number 10 as an int

Instead of breaking out of the loop, read and display what Scanner has in its buffer that is NOT an int. Be sure to add delimiters before and after what you print:
println(">" + data + "<");

Edited 6 Years Ago by NormR1: n/a

I think my delimiter might be causing the problem. After line 7 i added

s.useDelimiter(", *");

if this is the problem how would i change the delimiter so that it picked up the 10?

What was printed out by the code I recommended on my last post?

try it and see what happens.

Edited 6 Years Ago by NormR1: n/a

I know its a delimiter error. The delimiter is set to ", *" so it's not picking up the 10 because it doesn't follow the delimiter's pattern. I don't know of another delimiter that would work with this problem. Is there another way to write it?

Edited 6 Years Ago by cac186: corrected delimiter

read and display what Scanner has in its buffer

Read into a variable called data and then display it.

That's strange. That was after it read and printed this and before it did the break out of the loop

and it printed
1
1
1
1
20

It read those numbers all again ???
What is in the input file it is reading?

Your printed output shows that the input file contain:
1 1 1 1 20 1 1 1 1 20 10


This is all a waste of time. Unless you show complete code for what you are trying to do, no one can help you. Obviously you last post didn't show what I was requesting: to print out what was in the buffer when the call to hasNextInt() returned false.

Edited 6 Years Ago by NormR1: n/a

No sorry. I had set it so that in the event that there was not a next int it would read the whole line back.

So how are you going to

recognize the last number 10 as an int

without debugging the code to see what is happening?

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