I don't understand why it just prints numbers.

/* This program converts the first letters of the entered  strings for an array to uppercase*/
    import java.util.*;
    import java.lang.String.*;
    public class myfirstarray{
        static Scanner scan = new Scanner(System.in);

        static String str1, str2,str3, str4,str5;



        public static void main (String[]args){

            String[] items = new String[5];

            int counter;

            System.out.println("Enter 5 strings");


            for (counter = 0; counter < items.length ; counter++ )
            { items[counter] = scan.nextLine();

             str1 = items[counter];
             str2 = items[counter];
             str3 = items[counter];
             str4 = items[counter];
             str5 = items[counter];}

            char firstletter1 = str1.charAt(0);
            char firstletter2 = str2.charAt(0);
            char firstletter3 = str3.charAt(0);
            char firstletter4 = str4.charAt(0);
            char firstletter5 = str5.charAt(0);

            System.out.println(firstletter1 + firstletter2 + firstletter3 + firstletter4 + firstletter5);

        }

    }

****
**SAMPLE RUN FROM COMMAND PROMPT:
Enter 5 strings
i
have
no
idea
why
595
Press any key to continue . . .
******

**

it's supposed to output
i h n i w**

Dani AI

Generated

Short summary and diagnosis: the output was numbers because Java promoted each char to an integer and performed numeric addition, so you saw the sum of the characters' Unicode code points instead of the letters. was right to point out concatenation is needed instead of arithmetic; the other bug (all str1..str5 getting the last input) came from assigning those variables inside the input loop — switching to arrays (or assigning by index) fixes that, which already corrected in the finished code.

Simple, safe ways to print the first letters (examples not shown earlier in the thread):

  • Convert each char to a String before joining (for example String.valueOf(c) or Character.toString(c)).
  • Use a StringBuilder and append each char with a separator; appending a char to a StringBuilder concatenates, it does not add numeric values.

Example (concise) showing a robust join approach:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < firstletter.length; i++) {
    if (i > 0) sb.append(' ');
    sb.append(firstletter[i]);
}
System.out.println(sb.toString());

Practical cautions and refinements not covered above: always check word[i].isEmpty() (or trim().isEmpty()) before calling charAt(0) to avoid StringIndexOutOfBoundsException; use Character.toUpperCase(...) if you only need the first character capitalized; prefer try (Scanner s = new Scanner(System.in)) { ... } or close the scanner when done; follow Java naming conventions (class names in PascalCase) and remove unnecessary imports. Finally, avoid creating duplicate single-purpose variables (like x[] just to hold lengths) — call word[i].length() inline unless you need it repeatedly.

Recommended Answers

All 2 Replies

at line 35:

System.out.println("" + firstletter1 + firstletter2 + firstletter3 + firstletter4 + firstletter5);

Also note that because of lines 23 to 27 of your code, all strings str1 to str5 will have the same value which is equal to the last string you input. Move those lines outside of the for loop and enter the index manually. Or you can make str a string array as well. It's your choice.

commented: Now... i'm trying to convert the first letters to uppercase. can you help me. http://www.daniweb.com/software-development/java/threads/422406/help-touppercase-first-lettters-using-for-loop +0

FINISHED CODE

I forgot to save in a different file while it was still simple ^^;
It is more complicated now :p

  import java.util.*;
    import static java.lang.Character.*;
    import static java.lang.String.*;

    public class myfirstarray{
        static Scanner scan = new Scanner(System.in);




        public static void main (String[]args)
        {


                String[] word = new String[5];
                String[] str = new String[5];
                char[] firstletter = new char[5];
                int[] x = new int[5];
                String[] prefix = new String[5];
                String[] suffix = new String[5];
                String[] newstr = new String[5];

            String str0 , str1, str2,str3, str4 ;
            int counter;

            System.out.println("Enter 5 words:");


            for (counter=0; counter < word.length ; counter++ )
            { word[counter] = scan.nextLine();


                System.out.println ("word assigned to word[" + counter + "].");
                str[counter] = word[counter];

                    x[counter] = str[counter].length();
                    prefix[counter] = str[counter].substring(0,1).toUpperCase();
                    suffix[counter] = str[counter].substring(1, x[counter]);

                    newstr[counter] = prefix[counter].concat(suffix[counter]);




                firstletter[counter] = word[counter].charAt(0);}






            System.out.println("\n Entered words are: \n" + word[0] + " " + word[1] + " " +  word[2] + " " + word[3] + " " + word[4] );
            System.out.println("\n There first letters are: \n" + firstletter[0] +  ", " + firstletter[1] + ", " + firstletter[2] + ", " + firstletter[3] + " & " + firstletter[4] + " respectively.");
            System.out.println("\n words with first letters converted to uppercase: \n" + newstr[0] + " " + newstr[1] + " " +  newstr[2] + " " + newstr[3] + " " + newstr[4] );





        }



}
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.