tags:

views:

262

answers:

2

I've been set an assignment which requires me to capture the input of 5 strings, convert them to uppercase, output them as a string, convert them to their Unicode integers (using the getNumericValue method) and then manipulate the integers using some basic operators.

I get the first part but I am having trouble with the following:

  1. Using the getNumericValue to convert my single character literal strings into their Unicode integer counterparts.

  2. Being able to assign these ints to variables so I can further process s them with operators, all the examples I have seen have been simple printing out the number and not assigning it to a variable, since I am a Java noob the syntax is still a little confusing for me.

My code is here

If there is a cleaner way of doing what I want please suggest so but without the use of arrays or loops.

A: 

I don't understand why you don't want to do this without arrays or loops.

You can get the unicode values (as ints) making up the string via String.codePointAt(), or get the characters via charAt() followed by a getNumericValue() for each character. But regardless, you're going to have to iterate over the set of characters in the string via a loop, or perhaps recursion.

Brian Agnew
Well, no... one could do it by recurring on the "first" char and the "rest" of the chars, if one doesn't mind blowing the stack. :)
Jonathan Feinberg
The reason I can't use loops or arrays is simply because it hasn't been covered on my course and I would be penalised as a result.I ended up using CharAt() followed by getNumericValue() which worked perfectly. Thanks for your answer.
Anarchist
Loops are *so* fundamental that I can't believe you'd be penalised for using them - unless he wants you to use Jonathan's suggestion of recursion
Brian Agnew
@Jonathan - point taken. Edited appropriately
Brian Agnew
@Unknown - I cannot imagine a TA marking you down for using a language construct or technique that you haven't yet been taught ... UNLESS the question explicitly stated that you shouldn't. Marking someone down for demonstrating that they are ahead of the class would be totally counter-productive.
Stephen C
A: 

Yes, sounds like op needs to continue researching avenues of learning.

// difficult to code without interfering with 
// instructor's prerogative

char ( array ) = String.getchars();

// Now what, unroll the loop?

...

As noted by Brian, loops are fundamental. I cannot imagine getChars being assigned before simple array techniques.

Nicholas Jordan