This program is supposed to printout all the first letters of the Strings enterned in an Array. Which works.
but when I added some code to convert the first letters to uppercase, there's an error.

I put a comment in the code where the error is.
what's wrong with that part?

 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];

            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];
                str[counter].toUpperCase(charAt(0));/* error: cannot find symbol
                                                str[counter].toUpperCase(charAt(0));
                                                                         ^
                                                      symbol:   method charAt(int)
                                                      location: class myfirstarray*/
                }



            char firstletter0 = word[0].charAt(0);
            char firstletter1 = word[1].charAt(0);
            char firstletter2 = word[2].charAt(0);
            char firstletter3 = word[3].charAt(0);
            char firstletter4 = word[4].charAt(0);




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





        }

    }

**

UPDATE ##**

**Now it's like this. It wouldn't work because the output of

/Character.toUpperCase(str[counter].charAt(0));/

is a char. I want it to output the whole string with the first letter in uppercase. What should I do?

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];

        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];
            Character.toUpperCase(str[counter].charAt(0));//i changed it like this
            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" + str[0] + " " + str[1] + " " +  str[2] + " " + str[3] + " " + str[4] );





    }

}

Edited 4 Years Ago by Johannady2

How about using substring?

Edited 4 Years Ago by scudzilla

Comments
yeah.. I tried substring and length.. it works now :D

FINISHED CODE

i

mport 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] );





    }

}

IMPROVED \o/

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

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


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

    public static void main(String[]args)
    {   do{
            getData();
        } while (yesorno.equalsIgnoreCase("y"));
    }
                public static void getData()
                {
                firstLetterToUpperCasePrint();
                doYouWantTo();
                }

                public static void firstLetterToUpperCase()
                {

                    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);
                            }
                }

                public static void firstLetterToUpperCasePrint()
                { firstLetterToUpperCase();
                        System.out.println("\n Entered words are: \n");
                        for (counter=0; counter < word.length ; counter++ )
                            { System.out.println( word[counter]);}

                        System.out.println("\n There first letters are: \n");
                        for (counter=0; counter < word.length ; counter++ )
                            { System.out.println( firstletter[counter]);}

                        System.out.println("\n FIRST LETTER TO UPPERCASE: \n");

                        for (counter=0; counter < word.length ; counter++ )
                            { System.out.println( newstr[counter]);}

                }

                public static void doYouWantTo()
                {           Scanner keyboard = new Scanner (System.in);
                            System.out.println("Do you want to try again? y/n ");
                            yesorno = keyboard.nextLine();

                            if (yesorno.equalsIgnoreCase("n") == true )
                            { System.exit(0);}
                            if ( yesorno.equalsIgnoreCase("n") == false && yesorno.equalsIgnoreCase("y") == false)
                            {System.out.println(" Invalid input. try again? y/n ");
                            yesorno = keyboard.nextLine();}
                }












}

That crashed on me:

I:\Java\code>java everythingcombined
Enter 5 words:
tony jarkko veijalainen kalastajankuja,  Espoo
word assigned to word[0].

word assigned to word[1].
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String in
ex out of range: 1
        at java.lang.String.substring(Unknown Source)
        at everythingcombined.firstLetterToUpperCase(everythingcombined.java:44

        at everythingcombined.firstLetterToUpperCasePrint(everythingcombined.ja
a:52)
        at everythingcombined.getData(everythingcombined.java:27)
        at everythingcombined.main(everythingcombined.java:22)

I:\Java\code>
Comments
@pyTony OH! It's not supposed to be seperated by space. It's like Tony(press Enter), jarkko(press Enter), etc... :p

@pyTony OH! It's not supposed to be seperated by space. It's like Tony(press Enter), jarkko(press Enter), etc... :p

This question has already been answered. Start a new discussion instead.