Error : java.lang.String cannot be converted to String

import java.util.*;
class String

    String str;
    String newstr;
    void inputString()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a string:");
        str = sc.nextLine();
    }
    void checkFrequency()
    {
        int freq = 0;
        System.out.println("CHARCTER    FREQUENCY");
        if(str.isUpperCase)
        {
            char []sArray = new String[str.length - 1];
            for(int i = 0; i<= str.length() - 1; i++)
            {
                sArray[i] = str.charAt[i];
            }
            char temp;
            for(int i = 0; i <= Array.length - 2; i++)
            {
                for(int i = 0; i <= sArray.length - 2; i++) 
                if(sArray[j] > sArray[j + 1]);
                {
                    temp = sArray[j];
                    sArray[j] = sArray[j + 1];
                    sArray[j + 1] = temp;
                }
            }
            for(int i = 0; i<= sArray.length - 1; i++)
            {
                if(exists(sArray[i]))
                freq++;
                else
                {
                    newstr = newstr + sArray[i];
                    freq++;
                }
                System.out.println(sArray[i] + freq);
                freq = 0;
            }
            }
    }
    boolean exists(char a)
    {
        for(int i = 0; i <= newstr.length() - 1; i++)
        {
            if(newstr.charAt(i) == a)
            return true;
        }
        return false;
    }
    static void main()
    {
        String obj = new String();
        obj.inputString();
        obj.checkFrequency();
    }
}

Dani AI

Generated

The immediate compile error comes from naming a user class String, which hides java.lang.String. As noted, that makes String str = sc.nextLine(); try to assign a java.lang.String to the user-defined String type, hence the message "java.lang.String cannot be converted to String". Renaming the class (for example to CharFrequency) is the clean fix; qualifying every variable as java.lang.String is only a stopgap.

Several other problems in the posted code will prevent it from compiling or working correctly:

  • Missing class brace and an incorrect main signature (public static void main(String[] args) is needed to run from the JVM).
  • Confusing String methods and Character helpers (there is no String.isUpperCase; use Character.isUpperCase(char)).
  • Wrong array construction and indexing (char[] should be allocated with new char[str.length()]; charAt is a method: str.charAt(i)).
  • Loop bounds and reuse of the same loop variable in nested loops lead to logic bugs; prefer < length rather than <= length - 1.
  • A stray semicolon after if and references to Array.length (should be sArray.length) break control flow.
  • newstr is never initialized before concatenation, causing a NullPointerException at runtime.

A simpler, robust approach is to avoid manual sorting and count with a map. Example:

public class CharFrequency {
    public static void main(String[] args) {
        java.util.Scanner sc = new java.util.Scanner(System.in);
        java.lang.String input = sc.nextLine();
        java.util.Map<Character,Integer> freq = new java.util.LinkedHashMap<>();
        for (char c : input.toCharArray()) {
            if (Character.isWhitespace(c)) continue;
            freq.put(c, freq.getOrDefault(c, 0) + 1);
        }
        freq.forEach((ch, count) -> System.out.println(ch + "    " + count));
        sc.close();
    }
}

Troubleshooting tips: fix one compiler error at a time, use an IDE or javac to reveal exact line errors, initialize variables, and run small examples (e.g., str.toCharArray() + Arrays.sort) before adding more logic. Refer back to ’s advice to avoid naming collisions with core Java classes.

that's because you've named your own class String, which is a very bad idea.

class String
    String str;
    String newstr;

first of all, you're missing a { there, which means that this is not the actual code you are trying.

try this:

class String{
    java.lang.String str;
    java.lang.String newstr;

otherwise the compiler will assume you meant to use your own String class.
anyway, better sollution: give your class another name

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.