Source code for GetIndexOfAtSignExample.java:

public class GetIndexOfAtSignExample
{
	public int getIndexOfAtSign(String emailAddr)
	{
		int index;
		int length = emailAddr.length();
		for(index = 0; index < length; index++)
		{
			if(emailAddr.charAt(index) == '@')
			{
				return(index);
			}
		}
		return(-1);
	}
	
	public static void main(String args[])
	{
		System.out.println("GetIndexOfAtSignExample");
		GetIndexOfAtSignExample ex = new GetIndexOfAtSignExample();
		String email = "java.joe@sun.com";
		int retVal = ex.getIndexOfAtSign(email);
		System.out.println("Index of @ in " + email + ": " + retVal);
		email = "com!sun!java.joe";
		retVal = ex.getIndexOfAtSign(email);
		System.out.println("Index of @ in " + email + ": " + retVal);
	}
}

Output from GetIndexOfAtSignExample.java:

GetIndexOfAtSignExample Index of @ in java.joe@sun.com: 8 Index of @ in com!sun!java.joe: -1

--- Output Ends ---

NOTE:

You are reading previously generated output. You are not currently running the GetIndexOfAtSignExample application at the momement. You need to compile and run the source code first.

To run this program:


Authors: Kevin Chu and Eric Brower
Copyright 2000 Prentice Hall PTR