muneeb_ahmed
muneeb_ahmed

Reputation: 370

How can I convert String into array of characters in java

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..");
    }
}

Please help me as simply as possible. It gives me an error as I mentioned in above comment. I know what it is saying, but I can't figure out how to convert a String into an array so I can check every single character given by a user. Actually, this code was written in C++. I just converted it into Java language.

Upvotes: 2

Views: 152

Answers (5)

SafalFrom2050
SafalFrom2050

Reputation: 727

Your "x" variable is of String type, not an array. To declare "x" as string array, you should use

String x[] = new String[n];  //here 'n' is number of elements you store in your 'x' array

Also if you don't know how many elements can be are to be included in array, that can also grow and shrink based on your requirement you can use "ArrayList " like this;

ArrayList<String> al=new ArrayList<String>();  
al.add("J"); //add 'J' as 1st element of 'al'
al.add("a");
al.add("v");
al.add("a");
System.out.println("element at 2nd position: "+al.get(2));  //get 'a'
al.remove(0)  //to remove 'J'
.............

Upvotes: 1

Payerl
Payerl

Reputation: 1090

If one checks the documentation

https://docs.oracle.com/javase/6/docs/api/java/lang/String.html you can find the method

x.charAt(0)

which is used in java instead of x[0].

Upvotes: 1

pleft
pleft

Reputation: 7905

if(x[0]=="a"|| x[0]=="b")

can be changed to:

if(x.startsWith("a") || x.startsWith("b"))

and

str+=x[i];

can be changed to:

str+=x.charAt(i);

and lastly:

 if(str=="ab" || str=="ba")

should be changed to

 if(str.equals("ab") || str.equals("ba"))

Upvotes: 3

Husayn Hakeem
Husayn Hakeem

Reputation: 4570

x is a String, you'd need to convert it to an array of chars, then compare each char with 'a' and 'b'. To do that replace this line of code if(x[0]=="a"|| x[0]=="b") by

char[] x_chars = x.toCharArray();
if (x_chars[0] == 'a' || x_chars[0] == 'b') {
   ...
}

Upvotes: 1

Naman
Naman

Reputation: 31878

You can access the first character of the string using charAt as follows -

x.charAt(0) == 'a'

since that would return the first character of the string (based of start index = 0)

Upvotes: 2

Related Questions