I have to do a really strange thing.

I have to take a string of 2 words and then separate it and put it into two other strings eg

String name = "fred blogs";
move the part of the string name which says fred to the String firstname so it is like this:
String FirstName = "fred";
Move the second part of the string blogs to the String LastName;
String LastName = "blogs";

this is my code for it so far. I cannot quite work out the loops. When I test it I only get null values for printing the getter methods ( i test it in a separate class) the bit is bold is the bit I am having problems with.

import java.lang.Object;
public class Name
{
// ATTRIBUTE GO HERE
String FirstName;
String LastName;
String name = "Piers McMurdock";
public Name(String Firstname, String LastName)
{
for( int i = 0; i < name.length(); i++)
{
while (name.charAt(i) == ' ')
{
if (name.length() < name.charAt(i))
{
name = Firstname;
}
else if (name.length() > name.charAt(i))
{
name = LastName;
}
}

}
}
public Name( String name)
{
name = FirstName + LastName;
}

public String getFirstName()
{
return FirstName;
}
public String getLastName()
{
return LastName;
}
}

The getters return null because the values first,lastName are null. If you see the constructor they never get values.
The constructor:

public Name( String name)
{
name = FirstName + LastName;
}

is wrong. The argument's value must be inserted somwhere:

public Name( String n)
{
  name = n;
}

The first constructor and the while are also wrong.
What you have inside the while is wrong because you are changing the value of name. You don't want that, that's the value that you are checking and have at the for loop. If you change that, the for-loop is going to get messed up.
Read the String API and you will find 2 or 3 useful methods to use without using for or while loops.

agree with JavaAddict..

especially substring is worth taking a look at, it'll solve your entire 'problem' in two lines of code.

String name = "Hello World";

String firstName = <insert one instruction here>;
String lastName = <insert one instruction here>;

Thanks a million guys. I am almost there. I have an out of bounds exception -1 though which I dont understand. this is my code and I have included both my classes.

/**
 * Stores the name of an individual. The name is entered as a single string 
 * and may be either in the format:
 *      "firstNames lastName" e.g. "Fred Bloggs", "John Joe Smith"
 *      "lastName,firstNames" e.g "Bloggs,Fred", "Smith,John Joe"
 * The constructor will take the name parameter and parse it, storing the 
 * firstNames in a field 'first' and the lastName in a field 'last'.
 * Note the class should be able to correctly deal with trailing spaces
 * If there is no space or comma the full string is stored in last and 
 * first is set to null
 * 
 * 
 * @author Piers McMurdock
 * @version 3
 */
import java.lang.Object;
import java.lang.String;
public class Name
{
	
	// ATTRIBUTE GO HERE
 String FirstName;
 String LastName;
 String name = "Piers,McMurdock";
	
	
	public Name( String name)
	{
		this.name = name;	
	}

	
	public String getFirstName()
	{
		FirstName = name.substring(0,name.indexOf(","));
		return FirstName;
	}
	
	public String getLastName()
	{
		LastName = name.substring(name.indexOf(","),name.length());
		return LastName;
	}
}

then there is my driver class

class NameDriver
{
	public static void main (String [] args)
	{
		Name n1 = new Name ("John Edwards");
		System.out.println(n1.getFirstName());
		System.out.println(n1.getLastName());
		System.out.println(n1.name);
	}








}

Look at the api doc for indexOf(). It returns -1 when the string is not found in the target string. You are searching for a comma in a name that does not contain a comma in your driver class. The test name in your class does have the comma so it works fine for that one.

I would suggest first to get the int value of the method and then check it if it is -1:

int i = name.indexOf(",");
if (i!=-1) 
  FirstName = name.substring(0,i);
commented: thnx you have helped me kill 2 birds with 1 stone +1

Checking for the comma first could affect more than just where to split the string. For example "Joe Smith" indicates first name and then last name, whereas with the comma the last name appears first "Smith, Joe". So you have two different parsing operations to perform.

just insert a , between all the names you want to use, or change the char you want to check for in a space, but this might give some problems, having both the possibility of first and last names that exist out of multiple parts

How do I compare two input strings?
I typed exactly same two strings, but "if" condition doesn't work. The result is always "Not equal".
Here is my code. I used eclipse 3.02.

public class equalString {

    public static void main(String[] args) throws IOException {

        System.out.println("Enter any string");
        BufferedReader bf1= new BufferedReader(new InputStreamReader(System.in));
        String str1=bf1.readLine();
        System.out.println("Enter another string");
        String str2="";
        BufferedReader bf2= new BufferedReader(new InputStreamReader(System.in));
        str2=bf2.readLine();
        if (str1==str2){
            System.out.println("Two strings are equal");
        }
        else{
            System.out.println("Not equal");
        }
    }

    }

if (str1==str2) returns false correctly because they are two different objects.
if ( str1.equals(str2) ) will return true, because it compares their values.

Thanks for quick reply.
Now I can solve it.
Thanks a lot!!!!!!!!!

thnx guys for your help my work is solved.

I have to do a really strange thing.

I have to take a string of 2 words and then separate it and put it into two other strings eg

String name = "fred blogs";
move the part of the string name which says fred to the String firstname so it is like this:
String FirstName = "fred";
Move the second part of the string blogs to the String LastName;
String LastName = "blogs";

this is my code for it so far. I cannot quite work out the loops. When I test it I only get null values for printing the getter methods ( i test it in a separate class) the bit is bold is the bit I am having problems with.

use the substring method like this
String name = "fred blogs";
String FirstName = name.substring(0,4);
String LastName = name.substring(5,9);
System.out.println("Firstname"+Firstname+" "+"Lastname"+Lastname);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.