• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

c++ to Java

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am a beginner in Java and I want to convert this code in c++ into Java..
I tried to convert this code but it has many errors..
I hope someone would help me about this problem..






This is my code in Java but it has many errors




 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Please post the exact error message & stack trace. If you're getting multiple, different errors, just post the first one & we'll work through them one at a time.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch. Please check your private messages for an administrative message from JavaRanch.

In Java, all your code must be inside a class. You can't just put a method, outside of a class, in a source file. So your source file should contain something like this:

Not just a main() method without a class.

Another, small point: Prefer using the primitive type boolean instead of the wrapper class Boolean, as you're doing in line 7. (Note, the difference in case, boolean and Boolean are not the same thing).

Also, never catch exceptions and do nothing with them, as you are doing in line 45:

If an exception happens in the code, you'll never know that something went wrong. At least print the stack trace of the exception:

 
Heart Meishen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my whole code..
But still lacking..



When I run this code.. These are the errors..

Exception in thread "main" java.lang.UnsupportedOperationException: Not yet implemented
at e.printStackTrace(e.java:13)
at mp2convert.main(mp2convert.java:61)
Java Result: 1


What should I do with this code? I don't know where should I put these codes...






 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It looks like the code your are showing us is not exactly the code you are compiling and running, because lines 51-52 still look wrong:

This will not compile. The e.printStackTrace(); must be between the braces:

The stack trace that you get says that the error happened in line 61 of your source file mp2convert.java, while the code you posted only has 57 lines.

UnsupportedOperationException is an exception thrown in the changeCharacter method of class StringFunctions. The author of this did this to indicate that the method is not yet implemented. You should remove the whole throw new UnsupportedOperationException ... statement and replace it with an appropriate implementation.

The last two snippets of code don't seem to make any sense.

Make sure you understand what all those statements, classes, methods, etc. mean and do before you try to assemble them in a program. Putting together stuff when you don't know what it means is not likely to result in a working program. You can find a very good set of tutorials on the basics of Java here: The Java Tutorials. Especially look at the chapters Learning the Java Language and Essential Java Classes there.
 
Marshal
Posts: 79979
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:. . . The author of this did this to indicate that the method is not yet implemented. . . .

Or that is a default body put there by NetBeans when you tell it to create the method. Beginners are usually better off not using IDEs until they are more experienced.
 
Heart Meishen
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry, I deleted the comments in netbeans thats why it only consist of 57 lines.. It's my fault, sorry..

Our professor haven't explain those codes yet that's why these codes are unfamiliar for me.
But still, I want to learn in advance..

Your right, I have to read and understand it all first..

Thank you very much for the help
 
Campbell Ritchie
Marshal
Posts: 79979
397
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A method consisting of "throw new UnsupportedOperationException("Method xxx not yet implemented"); is useful when you create lots of methods and then come back later to fill in their bodies. It ensures you find out if you call those methods by mistake before implementing anything; it is what that exception is intended for. But it isn't the sort of thing a beginner wants.

Never use {} after catch (...). You could suffer an Exception and not find out about it. For beginners, the most useful thing to put after catch(Exception e) is probably { e.printStackTrace(); } (with the appropriate indentation), because that allows you to find out where the Exception occurred.
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic