Error with Java charAt ()

I am trying to create a program in which the user enters a string, and an echo image program on the monitor with one character per line.

However, I cannot compile what is written. A message appears on this line:

char c = word.charAt(i);

Here is my code:

import java.util.Scanner;
public class charAt

{
  public static void main(String[] args)
  {
   System.out.println("Give me a word, just one word:");
   Scanner kb = new Scanner(System.in);
   String word = kb.nextLine();

   for(int i=0; i<word.length(); i++)
   char c = word.charAt(i);
   System.out.println(" " + c);
  }
}

Also, if you could explain your answer, that would be very helpful.

+4
source share
7 answers

Without parentheses, Java will assume that you meant this:

for(int i = 0; i < word.length(); i++)
{
    char c = word.charAt(i);
}
System.out.println(" " + c);

, c . , c, , . @CupawnTae, , , for .

, :

for(int i = 0; i < word.length(); i++)
{
    char c = word.charAt(i);
    System.out.println(" " + c);
}

. , , .

+3

, ,

for(int i=0; i<word.length(); i++)
  char c = word.charAt(i); // <-- Also, not a valid location to declare the char
                           // as noted by cupawntae
System.out.println(" " + c);

, c println. , char c, - . , , println() -

for(int i=0; i<word.length(); i++) {
  char c = word.charAt(i);
  System.out.println(" " + c);
}

for(int i=0; i<word.length(); i++)
  System.out.println(" " + word.charAt(i));

String.toCharArray(), ,

for (char c : word.toCharArray())
  System.out.println(" " + c);
+2

, , println char c.

, . , , , , , , .

, System.out.println, . , :

for (;;) char c='a';

char c='a'; .

for ( , ..)

  • System.out.println(...);
  • , {...}

, , , {...}

, , println . , , , , .

+2

System.out.println(c) for.

import java.util.Scanner;
public class charAt

{
    public static void main(String[] args)
    {
        System.out.println("Give me a word, just one word:");
        Scanner kb = new Scanner(System.in);
        String word = kb.nextLine();

        for(int i=0; i<word.length(); i++)
        {
            char c = word.charAt(i);
            System.out.println(" " + c);
        }
    }
}
+1

! char c . .

for(int i=0; i<word.length(); i++) {
   char c = word.charAt(i);
   System.out.println(" " + c);
}

for(int i=0; i<word.length(); i++)
System.out.println(" " + word.charAt(i));
+1

, , - :

for(int i=0; i<word.length(); i++)
    char c = word.charAt(i);

- " ". Java , :

.

" , " (JLS).

char c = ... ( , for).

Java , , , ( 6 Eclipse). , " , ", , .

; Java, "", ( Java ). , , . , .

for (int i = 0; i < word.length(); i++) {
    char c = word.charAt(i);
}

The next step, of course, is to move your statement System.out.printlninside this block, since local variables are visible only inside the block in which they are declared.

+1
source
public class charAt

{
  public static void main(String[] args)
  {
   System.out.println("Give me a word, just one word:");
   Scanner kb = new Scanner(System.in);
   String word = kb.nextLine();

   char[] charArray = word.toCharArray();

   for (int i = 0; i < charArray.length(); i++) {
       System.out.println("char[" + i + "]: " + new String(charArray[i]");
   }
}

DISCLAIMER: Did not actually test this

0
source

All Articles