aspose file tools*
The moose likes Beginning Java and the fly likes I don't know where the problem is, can u help me? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I don Watch "I don New topic
Author

I don't know where the problem is, can u help me?

annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
The error message is "String index out of range: -1". What do I have to change to make it inbound?

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

position2 = author.indexOf(' ');

You have to think about what will happen when there is no ' ' in the author. What will be the returned index ? And what to do in such a case ?


[My Blog]
All roads lead to JavaRanch
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
What is the value for parameter author you are passing. Author is not having any space so author.indexOf(' ') is returning -1. So it is giving IndexOutOfBoundException in next line.


Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
I'm trying to seperate the author name (first name, last name). There is a list of names and the only thing that separates between the first name and last name is a space.

position2 =author.indexOf(' '); //this would divide up the name.

author.substring(position, position2); //I want the substring from the first letter to the space.


You have to think about what will happen when there is no ' ' in the author. What will be the returned index ? And what to do in such a case ?


If I don't use the indexOf(), I will get the full name. Is that what you were asking?
[ April 14, 2008: Message edited by: annie roberts ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

//this would divide up the name

Some methods returning a value also return a value when something wrong happened. Look at the API for String#indexOf.

If I don't use the indexOf(), I will get the full name. Is that what you were asking?

No. You can use indexOf, but what will happen if you try to pass "AnnieRoberts" to this method ? What is indexOf going to return ?
annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
I see what you mean after reading API for indexOf().

But from the list that I was given, all the names (first name, last name) is seperated from a space. I'm confused to why that would happen.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

I'm confused to why that would happen.

That's when debugging comes to the rescue If you are using an IDE, you could try to debug step by step to see what's going on in the getFirstName method. Or you could simply add a System.out.println("["+author+"]"); before calling indexOf.
annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
^I did what you said. It said java.util.InputMismatchException .

So does that mean I accidently put a int in a String, or something similar to that?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

So you are using a Scanner ? How are you using it ? Where do you get author names from ? I think it would be faster to post some relevant code.
annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
I'm reading from a file (notepad).

edit
[ April 15, 2008: Message edited by: annie roberts ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

isbn_john myers_The New World_3

Just that ? When I run this program, I receive this :

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:558)
at TestIndex.isGoodISBN(TestIndex.java:81)
at TestIndex.main(TestIndex.java:27)
annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
Sorry, I didn't know y0u wanted the whole text.
I did the indexOf() correctly.


[ April 15, 2008: Message edited by: annie roberts ]
[ April 15, 2008: Message edited by: annie roberts ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Thanks The exception is giving you valuable information, especially the place where something went wrong (you may have different line numbers)
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:819)
at java.util.Scanner.next(Scanner.java:1431)
at java.util.Scanner.nextInt(Scanner.java:2040)
at java.util.Scanner.nextInt(Scanner.java:2000)
at TestIndex.main(TestIndex.java:59)

This brings us to the following line :

You are trying to read a number, altough it's a string (book title ?) that comes after the author. How do you describe one line of data ? For example "0321479270_Tony Gaddis_Starting Out with Java: From Control Structures through Objects_3_AW_16_98.00". ISBN, author, title,... . You have to clear this first. Understand what your data is, and then write the code. Also, always use the information given ni a stack trace when an exception is thrown.
[ April 15, 2008: Message edited by: Christophe Verre ]
annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
I think I know where I went wrong.

Thanks for everything! I appreciate your help.
[ April 15, 2008: Message edited by: annie roberts ]
Hari Srinivas
Ranch Hand

Joined: Mar 26, 2008
Posts: 46
I guess if all the names are indeed separated using a single space and if you want the first and last name you can use the split method as below

String names[] = author.split(' ');

Now name[0] will have the string before the space i.e first name and name[1] will have the last name.

If all you are trying is just to split the author name, I hope i might have said something that made sense
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I don't know where the problem is, can u help me?