This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Compile time error

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why am I getting an error for this code? Attached is the instructions for the program.

}

The error says:

Elfish.java:5: error: <identifier> expected
  public static boolean elfish(str)
                                            ^
1 error
elfih.PNG
[Thumbnail for elfih.PNG]
c.PNG
[Thumbnail for c.PNG]
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I also need instance variables and a constructor?
 
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:Do I also need instance variables and a constructor?


Not mandatory.
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I fix these two errors?
Elfish.java:8: error: array required, but String found
  String checkLetter = str[str.length() - 1];
                                      ^
Elfish.java:34: error: cannot find symbol
  return elfish(str.slice(0, str.length() - 1));
                          ^
 symbol:   method slice(int,int)
 location: variable str of type String
2 errors
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:Why do we need a driver class if we can just write a main method in this class? Because our teacher wants us to write a driver class to test if both methods works. So does that mean I don't need to include a main method in this class?


Your teacher suggesting good. That is mainly for the responsibilities separation.

Similarly as in a car. There is a driver who controls everything, and then there are bunch of things, one of which is music/radio player for instance. Driver simply just use it, maybe tune to a different station, maybe put some extra volume, might be turn off or on - in simple words, controls it, but most importantly, it knows little to nothing about its internals how it does the things. For driver is important to know its abilities so it could control in a reasonable way.

So similarly your teacher suggesting to you (at least I think that's what it is). To create a new class where would be main method included, so you could create objects (where appropriate) or use methods as from utility class (meaning call them prefixing with class name without creating objects), and so you could have all the logic of your application flow laid down.
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Ana Yo wrote:Why do we need a driver class if we can just write a main method in this class? Because our teacher wants us to write a driver class to test if both methods works. So does that mean I don't need to include a main method in this class?


Your teacher suggesting good. That is mainly for the responsibilities separation.

Similarly as in a car. There is a driver who controls everything, and then there are bunch of things, one of which is music/radio player for instance. Driver simply just use it, maybe tune to a different station, maybe put some extra volume, might be turn off or on - in simple words, controls it, but most importantly, it knows little to nothing about its internals how it does the things. For driver is important to know its abilities so it could control in a reasonable way.

So similarly your teacher suggesting to you (at least I think that's what it is). To create a new class where would be main method included, so you could create objects (where appropriate) or use methods as from utility class (meaning call them prefixing with class name without creating objects), and so you could have all the logic of your application flow laid down.


If I were to contain objects does that mean I need to make constructors and accessors and modifiers? Or is the code I have good enough?
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have declared str as String:
while later in the same method you are trying to use it as if it were an array. That is not allowed.
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:You have declared str as String:
while later in the same method you are trying to use it as if it were an array. That is not allowed.



I fixed it, but how do you write the recursive call? Because I got an error there.
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:If I were to contain objects does that mean I need to make constructors and accessors and modifiers? Or is the code I have good enough?


Objects usually do have explicitly defined constructors, regardless, all classes have implicit ones, default no-args constructors which are invoked upon class instantiation.

In your particular case, your class looks like an Utility class, where there are no fields (instance variables), instance methods, just functions so to speak, so such classes better to have private constructors, just to ensure nobody instantiates them by mistake; Such method are being called via Class name, i.e.: Elfish.xIsh(...) as opposed to:

Whether it is right what you have or not - I don't know. That depends a lot on what was asked to do, which I don't understand at the moment.
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

str[str.length() - 1];


Maybe instead of this you meant to have: elfish[str.length() - 1];. I'm just guessing basically now with the information I have at hand.
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instructions are attached and revised code is on the top.
 
Carey Brown
Saloon Keeper
Posts: 6423
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:The instructions are attached and revised code is on the top.


The instructions say the method takes a 'word'. You have '[str.length() - 1]' which is not a word but an array.
 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:The instructions are attached and revised code is on the top.


Please don't modify your original post for the purpose of updating code/fixing it as per our comments. Editing feature isn't meant to be for that, at least not in this context. Mainly because the repliers posts start looking nonsense over the time - looks like we mention things which aren't there.

I have reverted your post to its initial version. If you want to show an updated code, you can do so by simply re-posting it once again, just modified this time.
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Revised Code:
There are two classes:
How do I do the recursive call for elfish(String str) since I got an error that I cannot use slice?




 
Liutauras Vilda
Marshal
Posts: 7271
492
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:How do I do the recursive call for elfish(String str) since I got an error that I cannot use slice?


Does String class has slice() method? I think it has substring() and it perhaps is what you actually need. At least you use that in your other method.
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I fix these errors for the Ish class? aka the driver class
Ish.java:14: error: cannot find symbol
     boolean isxIsh = xIsh(s5, s1);
                      ^
 symbol:   method xIsh(String,String)
 location: class Ish
Ish.java:15: error: cannot find symbol
     boolean isxIsh2 = xIsh(s1,s5);
                       ^
 symbol:   method xIsh(String,String)
 location: class Ish
Ish.java:16: error: cannot find symbol
     boolean isxIsh3 = xIsh(s6,s1);
                       ^
 symbol:   method xIsh(String,String)
 location: class Ish
Ish.java:17: error: cannot find symbol
     boolean isElfish1 = elfish(s1);
                         ^
 symbol:   method elfish(String)
 location: class Ish
Ish.java:18: error: cannot find symbol
     boolean isElfish2 = elfish(s2);
                         ^
 symbol:   method elfish(String)
 location: class Ish
5 errors
 
Carey Brown
Saloon Keeper
Posts: 6423
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not clear why you think you need recursion and substring in xIsh(). Don't you need to be comparing characters of the strings vs the strings or parts of the strings themselves?
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:I'm not clear why you think you need recursion and substring in xIsh(). Don't you need to be comparing characters of the strings vs the strings or parts of the strings themselves?


Our teacher requires us to use recursion in both methods to practice recursion.
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you but our teacher requires us to use recursion :/
 
Carey Brown
Saloon Keeper
Posts: 6423
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I were to make one class instead. Why did I get this error?

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end 9, length 2
at java.base/java.lang.String.checkBoundsBeginEnd(String.java:3410)
at java.base/java.lang.String.substring(String.java:1883)
at Elfish.xIsh(Elfish.java:50)
at Elfish.main(Elfish.java:69)
 
Carey Brown
Saloon Keeper
Posts: 6423
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Yo wrote:I agree with you but our teacher requires us to use recursion :/


There's nothing in the requirements stating that.

elfish( String word ) could be be implemented as
return xIsh( "elf", word );
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Ana Yo wrote:I agree with you but our teacher requires us to use recursion :/


There's nothing in the requirements stating that.

elfish( String word ) could be be implemented as
return xIsh( "elf", word );


That is a really good tip! How do I fix my error?
 
Carey Brown
Saloon Keeper
Posts: 6423
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ana Smith wrote:How do I fix my error?


If you mean the error in your recursive approach, I don't know. I can't follow what you are trying to do. I was able to implement it in 22 lines of non-recursive code. I'm not really sure what recursion is supposed to buy you here.
 
Ana Smith
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

Ana Smith wrote:How do I fix my error?


If you mean the error in your recursive approach, I don't know. I can't follow what you are trying to do. I was able to implement it in 22 lines of non-recursive code. I'm not really sure what recursion is supposed to buy you here.


The string index out of bounds error.
 
Carey Brown
Saloon Keeper
Posts: 6423
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you substitute
     if (s1.substring(0, length2).equals(s2))
You get
     if ("hi".substring(0, "whiteleaf".length).equals("whiteleaf"))
or
     if ("hi".substring(0, 9).equals("whiteleaf"))
If fails because '9' is greater than the length of "hi".

 
Carey Brown
Saloon Keeper
Posts: 6423
61
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Hey! Wanna see my flashlight? It looks like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!