import java.util.*;
public class prac9
{
public static void main(String[] args){
Scanner scn=new Scanner(System.in);
int count=0;
String x,str=" ";
System.out.println("Regular Expression is (a+b)(ab+ba)*");
System.out.println("Enter a Word: ");
x=scn.nextLine(); //here simple x string type of varible
if(x[0]=="a"|| x[0]=="b") //here x array of string type of varible
{ //prac9.java:15: error: array
// required,but String found
for(int i=1; i<x.length(); i++)
{
str+=x[i];
if((i%2==0)==true)
{
if(str=="ab" || str=="ba")
{
count=count+2;
str=" ";
}
}
}
if((count+1)==(x.length())){
System.out.println("Acceptable");
}
else{
System.out.println("Not Acceptable");
}
}
else{
System.out.println("Not Acceptable..");
}
}
请尽可能简单地帮助我。正如我在上面的评论中提到的,它给了我一个错误。我知道它在说什么,但我不知道如何将字符串转换为数组,以便我可以检查用户给出的每个字符。 实际上,这段代码是用C++编写的。我刚刚将其转换为Java语言。
最佳答案
if(x[0]=="a"|| x[0]=="b")
可以更改为:
if(x.startsWith("a") || x.startsWith("b"))
和
str+=x[i];
可以更改为:
str+=x.charAt(i);
最后:
if(str=="ab" || str=="ba")
应改为
if(str.equals("ab") || str.equals("ba"))
关于java - java中如何将字符串转换为字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46539261/