简体   繁体   中英

'.class.' error in java

I tried a problem which worked fine on C++; here is the C++ implementation.

#include<iostream>
#include<string>
#include<string.h>
using namespace std;
int main()
{
int t;
char str[1000100];
char num[1010];
cin>>t;
while(t--)
{
    cin>>num;
    cin>>str;
    int a=strlen(str);
    int b=strlen(num);
    int x=0;
    int flag=0;
    int m=0;
    for(int j=0;j<2*b;j++)
    {
        if(m==b)
        {
        flag=1;
        m--;
        }
        if(m==0)
        {
        flag=0;

        }



        num[j]=num[m];
        if(flag==0)
        m++;
        if(flag==1)
        m--;
    }
    for(int i=0;i<a;i++)
    {
        if(x==2*b)
            x=0;
        if((str[i]-(num[x]-'0'))<97)
        str[i]=char(str[i]-(num[x]-'0')+26);
         else
         str[i]=char(str[i]-(num[x]-'0'));
        x++;



     }
     cout<<str<<endl;
    //cout<<num;
}
return 0;
  }

Here is my Java implementation.

import java.util.Scanner;
class sngmsg
{
public static void main(String[] s)
{
    Scanner sc=new Scanner(System.in);
    int t;
    /*char[] str;
    str = new char[1000100];
    char[] num=new char[1010];*/
    String str;
    String num;
    char[] num1=new char[1010];
    char[] str1=new char[1000100];
    t=sc.nextInt();
    for(int l=0;l<t;l++)
    {

        num=sc.next();
        str=sc.nextLine();
        int a=str.length();
        for(int c=0;c<a;c++)
            str1[c]=str.charAt(c);
    int b=num.length();
    int x=0;
    int flag=0;
    int m=0;
    for(int j=0;j<2*b;j++)
    {
        if(m==b)
        {
        flag=1;
        m--;
        }
        if(m==0)
        {
        flag=0;

        }



        num1[j]=num.charAt(m);
        if(flag==0)
        m++;
        if(flag==1)
        m--;
    }
    for(int i=0;i<a;i++)
    {
        if(x==2*b)
        x=0;
        if(int(str1[i])-(num1[x])<97)
        str1[i]=char(int(str1[i])-num1[x])+26);
        else
        str1[i]=char(str1[i]-(num1[x]-'0'));
        x++;



    }

    }
}
}

In the section

if(int(str1[i])-(num1[x])<97)
str1[i]=char(int(str1[i])-num1[x])+26);
else
str1[i]=char(str1[i]-(num1[x]-'0'));
x++;

it is giving an error:

unexpected type
required:value
found: class
'.class' expected
; expected

and similar errors in these lines. Any help?

it should be

if((int)(str1[i])-(int)(num1[x])<97)

cast operator should have () over them like (int) 1 and (char) 'a' instead of int(1) or char('a')

int and char aren't functions, but are primitives.

str1[i]=char(int(str1[i])-num1[x])+26)

You're trying to use int as a function. What do you expect from it?

Usualy, you will have a public class with your main method. It seems that you are compiling your class and you are not able to execute it. Do this to solve your problem:

1 - Create a file with your class name, example "MyClass.java"

2 - Your class declaration needs to be public:

public class MyClass {
    // your code here
}

3 - Place your main method inside the created class:

public class MyClass {
    public static void main( String[] args ) {
        // your code here
    }
}

4 - Compile it (javac MyClass.java)

5 - Execute it: java MyClass

Your code seems to have some problems too. If you want to cast some primitive or reference type, you need to put the desired type inside parenthesis. Something like:

// a char
char c = 'a';

// casting that char to an integer
int i = (int) c;

Let me know if it worked.

You are missing a package name at the start of the file. Also the class should be public.

package something.somethingelse;
import java.util.Scanner;
public class sngmsg{

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM