3

So, It's so annoying.

I made 2 programs.

Number #1 :

class Arrays2 {
public static void main(String[] args){
    
    String sentenceBest[] = {"This is the first sentence!"};
    
    char chR[] = sentenceBest.toCharArray();        
    
    for (int counter = 0; counter < chR.length; counter++){
        char now = chR[counter];
        if (now != ' ') {
            System.out.println(now);
        }else {
            System.out.println('.');
        }
    }



}
  }

And for this program it says:

Arrays_ToCjarArray(not working).java:6: cannot find symbol
symbol  : method toCharArray()
location: class java.lang.String[]
char chR[] = sentenceBest.toCharArray();        
                             ^
 1 error

Number #2 Program:

class Arrays_3 {
public static void main(String[] args){
    
    boolean numbers[] [] = new boolean[10] [10];
    
    numbers[9] [8] = true;
    
    System.out.println(numbers[9][8] + "!!!");
    
    
    String names[] = {"Marton", "Balint", "Thomas", "David", "John", "Peter", "Andy", "Daniel", "Josh", "James", "Erling", "Romeo", "Vincent", "Fabian"};
    
    System.out.println("The origional order: ");
    for (int counter = 0; counter < names.length; counter++){
        String newName = names[counter];
        System.out.println(counter + ": " + newName);
    }
    
    
    System.out.println("The Alphabetical order: ");
    
    Arrays.sort(names);
    
    for (int counter2 = 0; counter2 < names.length; counter2++) {
        System.out.println(counter2 + ": " + names);
    }
    
    
}
       }

And the same thing. Cannot find Symbol. Sooo annoying.

Arrays_3.java:21: cannot find symbol
symbol  : variable Arrays
location: class Arrays_3
Arrays.sort(names);
    ^
1 error

I really don't understand this because this source code was from a great(so far) book called Sams teach you Java in 24 hours. So I really don't understand this. Any help would be apreciated well.

4 Answers 4

12

1.toCharArray() is for String not String[]

2.You need to import Arrays by adding

import java.util.Arrays;
Sign up to request clarification or add additional context in comments.

Comments

1

In program 1:

String sentenceBest[] = {"This is the first sentence!"};
char chR[] = sentenceBest.toCharArray();

sentenceBest is a String array, not a single String. You should call the toCharArray method from one of the Strings contained in the array. For this case, it should work with:

char chR[] = sentenceBest[0].toCharArray();

In program 2:

Class Arrays comes from java.util.Arrays that looks you haven't imported. Just add the clause:

import java.util.Arrays;

1 Comment

Yeahhh!!! Thanks, it works perfectly by now, you are a great programmer!!! Thanks again!!!
0

You're calling toCharArray on an array. Arrays do not support this. Instead you must call the method on the string with:

char chR[] = sentenceBest[0].toCharArray();        

You also need to import java.util.Arrays and figure out what that Arrays on a line by itself is doing.

1 Comment

Alright, thanks for answering, and now it works! thanks for spending your time solving my problem!! thanks again!!!
0

For your first error, you're trying to invoke String.toCharArray on a String[]. That's not happening because there is not any toCharArray method defined on arrays.

For your second error, you need to import java.util.Arrays.

Sooo annoying.

Sorry, but you have to read the error messages.

I really don't understand this because this source code was from a great(so far) book called Sams teach you Java in 24 hours.

First, they probably leave off the import statements for brevity in the book, but include them in the source code that you can obtain elsewhere (online, included DVD, etc.). Second, are you sure that you typed everything in correctly. Check again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.