0

here is a code i wrote in java, I'm still a beginner in a java so if this is a noob mistake please rectify it and also provide alternative solutions for reading the string and converting it to char array

import java.util.Scanner;
import java.io.*;

class TestClass {
public static void main(String args[] ) throws Exception {
   Scanner s = new Scanner(System.in);
   String str = s.nextLine();
   char[] c = str.toCharArray(str);
   int x=0,y=0;
   for(int i=0;i<=str.length;i++)
   {
      if(c[i]=='L')
        {
            x=x+1;
        }
    else if(c[i]=='R')
        {
            x=x-1;
        }
        else if(c[i]=='U')
        {
            y=y-1;
        }
        else if(c[i]=='D')
        {
            y=y-1;
        }
   }
    System.out.println(x+""+y);
}
}

I'm getting the following error

10: error: method toCharArray in class String cannot be applied to given types;

no arguments String actual and formal argument lists differ in length 12: error: cannot find symbol

variable length variable str of type String

1
  • 1
    it's str.length() not str.length Commented Jan 26, 2016 at 3:49

3 Answers 3

2

You put an argument in a function that doesn't take any:

char[] c = str.toCharArray(str);

Just use

char[] c = str.toCharArray();

For more info, see the String documentation

Sign up to request clarification or add additional context in comments.

Comments

0

Method str.toCharArray(str); doesn't exist. toCharArray takes no arguments. Replace with str.toCharArray()

Comments

0

Combining answers (I don't take credit for these)

You put an argument in a function that doesn't take any:

char[] c = str.toCharArray(str);

Just use

char[] c = str.toCharArray();

-by Arc676

And also note that

it's str.length() not str.length

-by TomN

Both are correct answers

Also, I noticed that you have (I do take credit for these)

for(int i=0;i<=str.length;i++)

It should be '<', not '<=' to avoid an out of bounds exception

for(int i=0;i<str.length();i++)

Also, just a tip:

System.out.println(x+""+y);

I think you missed putting a space in those quotation marks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.