aspose file tools*
The moose likes Beginning Java and the fly likes odd n even numbers program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "odd n even numbers program" Watch "odd n even numbers program" New topic
Author

odd n even numbers program

prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi all,

I wrote the following program. I need to get the number of odd n even numbers from "data.in" file .

I am getting an exception as :Exception in thread "main" java.lang.StringIndexoutofBoundsException : index out of range:-1
at java.lang.String.substring<String.java:1768>
at oddeven.main<oddeven.java:23>


data.in file contains :The first data value is
1066
The second data value is
1492
The third data value is
1939
The fourth data value is
1944
The fifth data value is
2000

help me out
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This is pretty good information:

Ignore the String info since you didn't write that. What's happening at your line 23 with an index that might be -1?

Look up a line to see how it maybe got to be -1. I think your code does not match your input file layout. See if that helps!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
prerna boja
Ranch Hand

Joined: Aug 19, 2004
Posts: 67
Hi ,

I tried to chage the program. It is compiling n giving no exception, but I amnot getting the output.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Originally posted by prerna boja:
Hi ,

I tried to chage the program. It is compiling n giving no exception, but I amnot getting the output.


Prerna, I don't think you fixed the problem. In fact, I think it is worse. I believe the reason why you are not getting anymore exceptions is because the code in question is no longer running.

BTW, you should fix the idents. It is very difficult to read.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

Opps, Sorry. Forgot to give a hint to help you fix the problem.



When does readLine() return null? And is the logic that you want to handle the null, in the "if", or the "else"?

Henry
Adam Vinueza
Ranch Hand

Joined: Apr 16, 2001
Posts: 76
Your first problem is that the code you've given reads only the first number forever: you never change inputString. Strictly speaking, you can't change it--Strings are immutable--but you could at least replace it with another String that's shorter. If you're just starting out, try the substring() method; if you're feeling fancy, you could do it the correct way, which is by using a StringBuffer.

I have no idea how you got a runtime error with the code you've given; I ran it on my JVM and it just kept reading the first number for ever and ever. But in this context, -1 should indicate either that you've reached end-of-file or that inputString.indexOf(loc) can't find the loc substring.

Whenever you see a -1, you should check the API to see what values your function calls might be returning. In general, beginning Java programmers should always code with the API right in front of them at all times.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: odd n even numbers program