Jump to content

need some Help with String, "letters in first name, and display their initials"

- - - - -

  • Please log in to reply
10 replies to this topic

#1
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hi all as you know by now i am still a beginner so go easy, we have started on Classes and Objects this week, a question i have answered and am getting the result the question asks for. What i want to know is, is it write the way i have done it..

Question: write a program which prompts the user to enter their full name as a single String. The program will then tell the user how many letters are in their first name, and will display their initials. ( their second initial will be the letter immediately after the space)

My code:

// Lecture 1 Classes and Objects

// gman

// 29/01/2012


import java.util.Scanner;


public class Q7NameCountInitals

{

	public static void main(String []args)

	{

		Scanner kb = new Scanner(System.in);

		

		// declare string

		String name = new String();

		int firstChar =0;

		

		// get user input		

		System.out.println("Enter your name ");

		name = kb.nextLine();

		

			char fNc = name.charAt(0);

			int pos = name.indexOf(" "); 

			char sNc = name.charAt(pos + 1); 

			

				

				for (int i = 0; i < pos; i++) 

				{

					firstChar++;	

				}

				

			System.out.println("your first name has " + firstChar + " characters." );

			System.out.println("your initials are " + fNc + "" + sNc);

	}

}


Thanks in Advance:

Gman

#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
For the length of the first name, can you just use pos-1?

#3
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
it works but it give 1 letter less, if i use pos-1. :confused:

#4
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Well that should be a problem that is easy to fix!

#5
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
just used pos and it worked, was told today that it not the way i should have used a substring ??

---------- Post added at 03:46 PM ---------- Previous post was at 03:21 PM ----------

ok have done a bit different, let me know what you think..


// Strings

// gman

// 31/01/2011


import java.util.Scanner;


public class Q7ANameCountInitals

{

	public static void main(String []args)

	{

		Scanner kb = new Scanner(System.in);

		

		// declare string

		char space = ' ';

		String name, first = new String();

		char in1, in2;

		int pos = 0;

				

			// get user input		

			System.out.println("Enter your name ");

			name = kb.nextLine();

			

			// 1st space

			pos = name.indexOf(space);

			

			// count char from first(0) to Pos(space), 

			int firstCharCount = pos;

			

			// first name

			first = name.substring(0, pos);

			

			// find initials.

			in1 = name.charAt(0);

			in2 = name.charAt(pos +1);

		

				System.out.println("your first name is " + first );

				System.out.println("your first name has " + firstCharCount + " charaters." );

				System.out.println("your initials are " + in1 + in2 );

	}

}



#6
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts
What if the user enters a name without a space? You should test if a space was found and issue a message or skip trying to get a letter that is not there.

#7
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hi Norm , no it give an error:

Enter your name
tomas
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1952)
at Q7ANameCountInitals.main(Q7ANameCountInitals.java:30)


what would i do here?

gman

#8
Norm

Norm

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 327 posts

Quote

StringIndexOutOfBoundsException: String index out of range: -1
You need to check the value of the index you are using on line 30 and make sure it is valid. -1 is not a valid index value.
You could use an if statement to test the variables value before using it.

What do you want the program to do if there is no space?

#9
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
I have taken your advice and it works but not work for second name now, where am i going wrong :confused:


// Strings

// gman

// 31/01/2011


import java.util.Scanner;


public class Q7ANameCountInitals

{

	public static void main(String []args)

	{

		Scanner kb = new Scanner(System.in);

		

		// declare string

		char space = ' ';

		String name, first = new String();

		char in1, in2;

		int pos = 0;

				

			// get user input		

			System.out.println("Enter your name ");

			name = kb.nextLine();

			

			if (space != space)

			{ 	// 1st space

				pos = name.indexOf(space);

				// first name

				first = name.substring(0, pos);

				

				in2 = name.charAt(pos +1);

				// count char from first(0) to Pos(space), 

				int firstCharCount = pos;

				// find initials.

				in1 = name.charAt(0);

			

				System.out.println("your first name is " + first );

				System.out.println("your first name has " + firstCharCount + " charaters." );

				System.out.println("your initials are " + in1 + in2 );


			}

				else

				{	

					// count char

					int firstCharCount = name.length();

					// find initials.

					in1 = name.charAt(0);

				

					System.out.println("your first name is " + name );

					System.out.println("your first name has " + firstCharCount + " charaters." );

					System.out.println("your first initial is " + in1);

	

				}

			

	}

}


Thanks for helping :)

Gman

#10
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
if (space != space)
This is like saying
 if( 5 != 5 ) { ... do this }

You have to think about this logically


-get the name from the user

   -if the user's name doesn't have a space in it, then the name doesn't have a first name AND a last name

      -how will you handle this?

           -Do you want the program to quit?

           -Do you want the program to ask the user for the name again?

           -Do you want to compute data on the 1 name given?

      -if you want to quit, then quit

      -if you want to ask the user to try again, go back to step 1

           -going back to step 1 means that you'll need a loop

      -If you want to compute data on the 1 name given, then compute



#11
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Thanks for that lethalwire, will try that and get back..




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users