Okay, We have a Laboratory Exercise that, asks a user to input his/her name and gives an output of its initial (from the input NAME).

example:
My name is Dani A. Ona
and the output is >> D A O.

My instructor said that I will use `CharAt()` ...
is it true??
how can I use it???.
kindly explain...

i want to have a discussion about this, so, I don't want a code.
and i will make a code, then your GUYS, will check it...

THANKS!!!

public class StringFindCharacterExample {

    public static void main(String[] args) {

        String abc="This is java string find character at particular index";

        System.out.println("Find character in string  :"+abc.charAt(3));
    }
}

I got this code from the internet, the output of the program is `s`
why?? where is the position of `s` there that is 3 ?? :)

charAt is a method of the String class. In case you don't know it:

String s = "abs";
char ch = s.charAt(0);
System.out.println(ch);

You might want to loop the String and use the charAt to take each character, using the charAt. Have a new String an initialize it outside the for loop.
In the loop once you find an empty character, means that you have finished with a word so the next character is the first letter of the next word. Concatenate only the next character to the String.

in your code, the output will be `a` ??

hey there! i have this code...

import java.io.*;
public class charat{
	public static void main (String args [])throws IOException{
		BufferedReader in = new BufferedReader (new InputStreamReader (System.in));

		System.out.print("Enter First Name: ");
		String fname = in.readLine();
		System.out.print("Enter Middle Name: ");
		String mname = in.readLine();
		System.out.print("Enter Last Name: ");
		String lname = in.readLine();

		System.out.println("Initials are::>>");
		System.out.println("\t "+fname.charAt(0)+ "." +mname.charAt(0)+ "." +lname.charAt(0)+ ".");

	}
}

I didn't know that the user could enter the names separately. It is much easier that way. Have you tried running the program?

hey there! i have this code...

import java.io.*;
public class charat{
	public static void main (String args [])throws IOException{
		BufferedReader in = new BufferedReader (new InputStreamReader (System.in));

		System.out.print("Enter First Name: ");
		String fname = in.readLine();
		System.out.print("Enter Middle Name: ");
		String mname = in.readLine();
		System.out.print("Enter Last Name: ");
		String lname = in.readLine();

		System.out.println("Initials are::>>");
		System.out.println("\t "+fname.charAt(0)+ "." +mname.charAt(0)+ "." +lname.charAt(0)+ ".");

	}
}

Wait a second, you said you had an assignment using charAt method which is very basic. Then you get an example from the net. Then this piece of code comes up, did you write and understand this code? or did you just get it from the net?

@javaaddict, yes i ran the program...and its alright.
i have a question...
if i will input my name once, not in separate ones...
if i will use the if statement, then my program can read the first letter, but how can my program read the rest of my initials, and output them?

@Akill, yes i got the code from the net
then the program you said, i wrote it.. :)
i do not... copy and paste :)

@javaaddict, yes i ran the program...and its alright.
i have a question...
if i will input my name once, not in separate ones...
if i will use the if statement, then my program can read the first letter, but how can my program read the rest of my initials, and output them?

@Akill, yes i got the code from the net
then the program you said, i wrote it.. :)
i do not... copy and paste :)

You mean if you have a String like this: "aaa bbb cc" how can you get the initials?

yes yes... how can i do that?

First of all, have you been told how will you need to read the input?
If it is ok to read the Names separately then you are fine.
If it is required to read once the whole string with the words then you can do what I suggested.
Remember, you can loop the String and get each character with that method. You can use the String.length() method to find the upper limit of the index.

My suggestion was:

"
You might want to loop the String and use the charAt to take each character, using the charAt. Have a new String an initialize it outside the for loop.
In the loop once you find an empty character, means that you have finished with a word so the next character is the first letter of the next word. Concatenate only the next character to the String.
"

If you can use any method to help you then you can use the split method, that will give you the words of the String in an array:

String s = "Dani A. Ona";
String [] tok = s.split(" ");

// tok now is an array with elements:
// tok={"Dani", "A.", "Ona"}

Now you can loop that array and get the first character of each element, but you need to make sure that you are allowed to use that method. Maybe not if you said that you can use only the charAt.

You could also use "StringTokenizer" to split the string up and get the initials of each of the tokens

You could also use "StringTokenizer" to split the string up and get the initials of each of the tokens

The String method split() is generally recommended for new code.

From the Java API Docs:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

ok, i have this program...

import java.io.*;
public class Name{
	public static void main(String args[])throws IOException{
		BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
		System.out.print("Enter Full Name: ");
		String name = in.readLine();
		String splitname[] = name.split("\\s*\\s");
		for (int i = 0; i <= name.length(); i++){
			char fname = splitname[i].charAt(0);
			System.out.println(fname);
		}
	}
}

with an output of

Enter Full Name: Dani Antonio Ona
D
A
O

the output is fine, but i have an exception. it says;

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
	at Name.main(Name.java:9)

and i don't know what will i write in `.length` in the loop..
in the program, i wrote `name.length` and i know its wrong.
so, what is the right length??

Thanks..

ok, my fault..
i figured it out...
i changed `<=` into `<` ..'
because i declared `i` as 0... so i have to use i less than the length of the splitname variabel..

my loop is this..

for (int i = 0; i < splitname.length; i++){
			char fname = splitname[i].charAt(0);
			System.out.print(fname+ ".");
		}
commented: The best way to learn is by solving things on your own. This is exactly what you did! :) +8

When looping arrays, an array of length: name.length() , has indexes from 0 to name.length()-1.
An array of length 3 has arr[0], arr[1], arr[2], not arr[3]
So the for loop shouldn't be: for (int i = 0; i <= name.length(); i++)

When looping arrays, an array of length: name.length() , has indexes from 0 to name.length()-1.
An array of length 3 has arr[0], arr[1], arr[2], not arr[3]
So the for loop shouldn't be: for (int i = 0; i <= name.length(); i++)

I think you are confusing array's length attribute with class String's length() method.

[edit]
@kumpul101:

// Using a "normal" for-loop.
for (int i = 0; i < splitname.length; i++) {
  char fname = splitname[i].charAt(0);
  System.out.print(fname + ".");
}

Can alternatively be rewritten as:

// Using an "enhanced" for-loop.
for (String n : splitname) {
  char fname = n.charAt(0);
  System.out.print(fname + ".");
}

[/edit]

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.