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**

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.