• 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Stuck on porting small segment of JavaScript to java  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm fairly good at Java, but somewhat at a loss when the some of the JavaScript below is mentioned. I've attempted to port the below code, but I'm finding it somewhat difficult. Any help would be great :-)

Parts of JavaScript I'm stuck at porting:

1. I'm stuck on the return statement on the 3rd line.
2. Similarly the lines utilizing "charCodeAt", as I keep getting error "Cannot invoke charCodeAt(int) on the primitive type char"
3. Also on the second to last line, I keep getting the error "Cannot invoke padLZ(int) on the primitive type int".

Link to original javascript

http://www.movable-type.co.uk/scripts/latlong-gridref-v1.html

Original JavaScript


Attempted Port

 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marc Tone wrote: 1. I'm stuck on the return statement on the 3rd line.
2. Similarly the lines utilizing "charCodeAt", as I keep getting error "Cannot invoke charCodeAt(int) on the primitive type char"
3. Also on the second to last line, I keep getting the error "Cannot invoke padLZ(int) on the primitive type int".


1)
In the javascript (which I know nothing about), the 3rd line is

no return statement there.

update: if you mean line 6 of what you posted:

it looks to me like is is returning a literal string containing two question marks.

if you were referring to your java line five:

part of the problem is that your method is defined as "void", so you can't return ANYTHING.

2)charCodeAt is a method of a String. "l1+'A'." is a char - a primitive. You can't call methods on primitives, but only on objects.

3) e is an int. Again, you can't call methods on primitives. I'm not eve sure what "padLz" is - I don't think it is a java method at all.


for what it's worth..."l1" is a TERRIBLE name for a variable. It looks like "11" and "l1" and "1l" and "ll". VERY hard to tell apart:
 
Saloon Keeper
Posts: 9138
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marc, welcome to CodeRanch!

OsGridRef seems like a good candidate for for class. prototype is an object that is used as, well, the prototype of any newly created instances of OsGridRef. In Java, this is not necessary, because the class itself defines what instances look like.

Now, you can give the class OsGridRef a method toString(int digits) that also uses member fields easting and northing (e and n are derived from these fields, and don't have to be passed as method parameters).

Instead of doing all this work with floating point values, I would suggest using BigDecimal, because it already works with Strings under the hood (making calculations more exact), and will help in rounding the result to a specific precision.

l1+'A' yields a double value in Java, NOT a String. If you want to perform String concatenation, you'll probably want to use l1+"A" instead, and then the charAt() method.

I think it's a very bad idea to name the variables l1 and l2, because they're easily confused with the integer literals 11 and 12.

Also, you defined e and n as primitives, and then you're calling a method padLZ() on them? That's not how primitives work.
 
Marc Tone
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Hi Marc, welcome to CodeRanch!

OsGridRef seems like a good candidate for for class. prototype is an object that is used as, well, the prototype of any newly created instances of OsGridRef. In Java, this is not necessary, because the class itself defines what instances look like.

Now, you can give the class OsGridRef a method toString(int digits) that also uses member fields easting and northing (e and n are derived from these fields, and don't have to be passed as method parameters).

Instead of doing all this work with floating point values, I would suggest using BigDecimal, because it already works with Strings under the hood (making calculations more exact), and will help in rounding the result to a specific precision.

l1+'A' yields a double value in Java, NOT a String. If you want to perform String concatenation, you'll probably want to use l1+"A" instead, and then the charAt() method.

I think it's a very bad idea to name the variables l1 and l2, because they're easily confused with the integer literals 11 and 12.

Also, you defined e and n as primitives, and then you're calling a method padLZ() on them? That's not how primitives work.



Thanks for the advice :-)
I've corrected the quotation marks charCodeAt but now I'm getting an error around fromCharCode saying The method fromCharCode(double, double) is undefined for the type String.

Also you're right in saying, this isn't how primitives work, but I don't really understand what changes I'm supposed to make to fix it. In this case around e.padLz, I get the error Cannot invoke padLz(int) on the primitive type int. What changes would you suggest?
 
Author and ninkuma
Marshal
Posts: 66787
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem is that you are trying to do a literal translation rather than a semantic one. Rather than simply copying a line of JavaScript and hoping it does the same thing in Java, figure out what the line is actually doing in JavaScript and write (not copy) a line in Java that does the same thing.

In some cases, like checking for NaN, there is no equivalent, and no need for one. (An int in Java will always have a value).
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that Java and JavaScript are totally different languages, despite the common letters in their names (Java is to JavaScript as Ham is to Hamster). So, if a particular function such as fromCharCode exists in JavaScript, then that does not mean that it exists in Java in the same way.
 
Marc Tone
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've changed


to




But now I'm getting the error



around

fromCharCode



What should I do to fix?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66787
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I've already said: figure out what that JavaScript code is trying to do. Don't just copy methods and code.

Once you figure out what the JS code is doing, you can tell us, and we'll help you figure out how to do it in Java.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!