-3

So I am running into this problem where I am trying to see if one input is the same as a pre defined group of variables for a class. It can be any of the variables, but I keep getting an error saying that char can not be converted to char[] the code is below. Would very much appreciate the help!

 {
  Scanner reader = new Scanner(System.in);
  int i;
  int found = 0;
  char[] letter;
  char A, C, T, H;
  char[] okaycodes = {A, C, T, H};
  System.out.printf("A - air\nC - car\nT - truck\nH - hand deliver\n");
  System.out.print("Enter shipping code:  ");
  letter = reader.next().charAt(0);
  for(i = 0; i < 5; i++)
  {
    if(letter == okaycodes)
    {
         System.out.println("Good code");
manmark
  • 1
  • 1
  • sorry for not typing the full error, It is actually in line 10. 2 errors found: File: C:\Users\murph\Downloads\SCHOOL STUFF\db61.java [line: 10] Error: incompatible types: char cannot be converted to char[] – manmark Nov 20 '21 at 19:00
  • Add details as edits to your question rather than comments. – Basil Bourque Nov 20 '21 at 19:13

2 Answers2

0

[] means array

To address your error: you declared letter as an array rather than a char.

This:

char[] letter;

… should be:

char letter;

char broken; use code points

Bigger picture: The char type is legacy since Java 2, essentially broken, incapable as a 16-bit value of representing most characters. Use code point integer numbers instead.

List < String > codes = input.codePoints().boxed().map( Character :: toString ).map( String :: toUpperCase ).toList();
List < String > faultyCodes = codes.stream().filter( s -> ! List.of( "A" , "C" , "T" , "H" ).contains( s ) ).toList();
if ( faultyCodes.size() > 0 ) { … }

Here is a full example to run.

String input = "cats";

List < String > codes = input.codePoints().boxed().map( Character :: toString ).map( String :: toUpperCase ).toList();
List < String > faultyCodes = codes.stream().filter( s -> ! List.of( "A" , "C" , "T" , "H" ).contains( s ) ).toList();

if ( faultyCodes.size() > 0 )
{
    System.out.println( "faultyCodes = " + faultyCodes );
}
System.out.println( "codes = " + codes );
System.out.println( "valid codes = " + codes.stream().filter( s -> List.of( "A" , "C" , "T" , "H" ).contains( s ) ).toList() );

When run.

faultyCodes = [S]
codes = [C, A, T, S]
valid codes = [C, A, T]
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

I believe that the char[] letter has not been initialized (created). In java, Arrays ( in this case a character Array) have a set size, it can be set like this:

char[] letter = new Char[size];

or, size can be set like this:

char[] letter = {0, 1, 2, 3}; // size of array in this case would be set to 4

Your next (Main) issue is that your trying to assign the a char to letter. Java will basically take that command as the initialisation of letter (as explained above, your char[] letter has not been initialized yet). By instantiating letter when you first created it, like the first example above, your issue will be mostly fixed.

The next issue is that you don't want to initialize letter again but instead assign the user's input to each index of letter.

letter[0] = reader.next().charAt(0);

The [0] is a small change. This targets index 0 in the char[] letter, rather than targeting letter itself.

Also you may want to move:

  System.out.print("Enter shipping code:  ");
  letter[i] = reader.next().charAt(0);

into your for loop and use i to determine which index is to be changed.

I hope this solves your issue :)