Help with indexOf

Having trouble seperating parts of a sequence

Page 1 of 1

12 Replies - 982 Views - Last Post: 01 April 2007 - 11:19 PM Rate Topic: -----

#1 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Help with indexOf

Posted 31 March 2007 - 11:27 PM

I'm having some problem learning and using the indexOf method. I have a list of numbers and letters that I'm trying to separate out. It looks something like Allen Smith/666888999/20/6.

I'm trying to separate out the first name, last name, and the 3 sections of numbers. So far I have:

public Customer  (String customerInfo)		
	{
		firstName = customerInfo.indexOf(' ');
		lastName = customerInfo.indexOf(' ', '/');

	}


I know I should have more there, but I can't even get that to compile. When I try I get the following error:

C:\Documents and Settings\Brad\Desktop\CSE\Customer.java:40: incompatible types
found : int
required: java.lang.String
firstName = customerInfo.indexOf(' ');
^
C:\Documents and Settings\Brad\Desktop\CSE\Customer.java:41: incompatible types
found : int
required: java.lang.String
lastName = customerInfo.indexOf(' ', '/');

I'm pretty sure that I'm not using the indexOf method even remotely correct. Any help at all would be appreciated.

This post has been edited by Bradwick: 31 March 2007 - 11:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help with indexOf

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 289
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Help with indexOf

Posted 01 April 2007 - 05:17 AM

why don't you use String.split() which given a String splits it into an array of strings,
http://java.sun.com/...ang/String.html

e.g.
  String s1="Allen Smith/666888999/20/6";
  String s1split[]=s1.split("[ /]");
  for(int i=0; i< s1split.length;i++)
	System.out.println(s1split[i]);


prints
Allen
Smith
666888999
20
6
Was This Post Helpful? 0
  • +
  • -

#3 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Re: Help with indexOf

Posted 01 April 2007 - 09:59 AM

Unfortunately the purpose of the assignment is to learn to use the indexOf method.
Was This Post Helpful? 0
  • +
  • -

#4 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Help with indexOf

Posted 01 April 2007 - 10:21 AM

What is occurring is that the indexOf() method returns an integer that indicates which position in the string that the specified character has occurred at. You are trying to place that integer into a string variable type. What you will want to do is use the indexOf() method to determine the position of each character you are looking for, then use the substring() method of th String class (or something similar) to return the string that begins at that position and ends at the next.
Was This Post Helpful? 0
  • +
  • -

#5 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Re: Help with indexOf

Posted 01 April 2007 - 02:54 PM

Thanks, I see what I was doing wrong now. Now I'm stuck though with pulling out the numbers from the list of information there. substring doesn't seem to work because when you do a substring of what is between the first to slashes, it returns an int, which apparently substring doesn't like. I've been looking and I can't find something similar to substring for ints. Am I just using substring wrong?
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Help with indexOf

Posted 01 April 2007 - 03:06 PM

Can you post the code you are using? The substring() method has two varieties, one of which takes two integers, the starting and ending position within the original string. Assuming that you have a start position of 0 and an end position of say 10, you would call it like so:
firstName = customerInfo.substring(1,10);



http://java.sun.com/...ang/String.html

If you post the code you are using, we may be able to more effectively debug.
Was This Post Helpful? 0
  • +
  • -

#7 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Re: Help with indexOf

Posted 01 April 2007 - 03:27 PM

Sure, this is the code for the method I have so far:

public Customer  (String customerInfo)		
	{
		int firstSpace, firstSlash, secondSlash, thirdSlash;

		firstSpace = customerInfo.indexOf(' ');
		firstSlash = customerInfo.indexOf('/');
		secondSlash = customerInfo.indexOf('/', '/');

		firstName = customerInfo.substring(0, firstSpace+1);
		lastName = customerInfo.substring(firstSpace+1, firstSlash+1);
		customerID = customerInfo.substring(firstSlash+1, 17);
		  }



The compilation error I'm getting is:

C:\Documents and Settings\Brad\Desktop\CSE\Customer.java:49: incompatible types
found : java.lang.String
required: int
customerID = customerInfo.substring(firstSlash+1, 17);


The 17 is just a random number I picked that was after the first slash to see if I could get it to compile. I commented out both the "secondSlash = customerInfo.indexOf('/', '/');" and "customerID = customerInfo.substring(firstSlash+1, 17);" lines to ensure that they're the problem and it's definitely the "customerID = customerInfo.substring(firstSlash+1, 17);" part that I'm doing wrong.
Was This Post Helpful? 0
  • +
  • -

#8 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Help with indexOf

Posted 01 April 2007 - 04:10 PM

Where have you declared customerID? What variable type is it?
Was This Post Helpful? 0
  • +
  • -

#9 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Re: Help with indexOf

Posted 01 April 2007 - 04:19 PM

customerID was declared at the beginning of the class with a list of other common variables:

public class Customer
{
	private int customerID;



I'm pretty sure that I'm trying to use a method that returns a string to return an int, I just don't know what I'm doing wrong. I've seen on the boards "parseInt" but since we haven't been taught that yet I'm sure I'm just missing something with substring.
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Help with indexOf

Posted 01 April 2007 - 04:22 PM

You're not. The substring() method returns a string, which you are trying to assign to an integer. You will either have to declare customerID as a string, or parse the return value of the substring() method into an integer.

If customerID is simply that, an ID, then a string is certainly a valid option. If it will have to have some mathematical functions performed on it at some point, I'd advise parsing to an integer.
Was This Post Helpful? 0
  • +
  • -

#11 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Re: Help with indexOf

Posted 01 April 2007 - 10:10 PM

Okay, I parsed out the ID to an integer but now I'm realizing that I don't think I did the indexOf part correctly. What I have is:

public Customer (String customerInfo)		
	{
		String cusID, maTickets, noTickets;
		int firstSpace, firstSlash, secondSlash, thirdSlash;

		firstSpace = customerInfo.indexOf(' ');
		firstSlash = customerInfo.indexOf('/');
		secondSlash = customerInfo.indexOf('/', '/');

		firstName = customerInfo.substring(0, firstSpace+1);
		lastName = customerInfo.substring(firstSpace+1, firstSlash+1);
		cusID = customerInfo.substring(firstSlash+1, secondSlash+1);
		customerID = Integer.parseInt(cusID);
		  }



The part I'm worried about is the "secondSlash = customerInfo.indexOf('/', '/');" and "cusID = customerInfo.substring(firstSlash+1, secondSlash+1);" sections. What I'm intending for that to do is return the index of the int of the second slash and get the substring between the two slashes. I'm not sure if that does that though.
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 415
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Help with indexOf

Posted 01 April 2007 - 11:06 PM

The problem you are having is in this bit of code.
secondSlash = customerInfo.indexOf('/', '/');


The second parameter that the indexOf method takes is an integer of the index of the string where it should begin searching. Keep in mind that the indexOf method only looks for the first occurrence of the search string. So you will need to start searching after the first occurrence is discovered.

Try this:
secondSlash = customerInfo.indexOf('/', firstSlash+1);

Was This Post Helpful? 0
  • +
  • -

#13 Bradwick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 21-March 07

Re: Help with indexOf

Posted 01 April 2007 - 11:19 PM

Ahhh, that works so much better. Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1