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
• Ron McLeod
• Liutauras Vilda
• Paul Clapham
• paul wheaton
Sheriffs:
• Tim Cooke
• Devaka Cooray
• Rob Spoor
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Tim Moores
• Carey Brown
• Mikalai Zaikin
Bartenders:

# Char to Integer

Ranch Hand
Posts: 48
• Number of slices to send:
Optional 'thank-you' note:
Hi
Am try to convert a Char Char A = 'F'; to an Integer without Changing 'F' to a numeric value. Then Set it at something like list.set(4, A). Is this possible?

Bartender
Posts: 2236
63
• Number of slices to send:
Optional 'thank-you' note:

Molayo Decker wrote:Hi
Am try to convert a Char Char A = 'F'; to an Integer without Changing 'F' to a numeric value. Then Set it at something like list.set(4, A). Is this possible?

What do you mean by this? An integer IS a numerical value. By the way, so is char.

Molayo Decker
Ranch Hand
Posts: 48
• Number of slices to send:
Optional 'thank-you' note:
Am writing a code to replace all numbers in an array that are divisible by another number to be replaced by F or B

So if 3 is divisible by 3 replace 3 in the array {1,2,F,4,5,6,7}. Am trying to set 3 to F by using the set method but it only takes two integers or two strings. set (int, String)

Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
You mean the terms array and list. If you're using java.util.List, what type of object are you storing in it?

Molayo Decker
Ranch Hand
Posts: 48
• Number of slices to send:
Optional 'thank-you' note:
I am storing numbers in my ArrayList

Marshal
Posts: 78659
374
• Number of slices to send:
Optional 'thank-you' note:

Molayo Decker wrote:. . .

Ouch! What dreadful variable names. Try
myList.set(0, (int)'F');
If you have a List<Integer>, you can put ints into it and they will undergo boxing conversion. You cannot (I think) put chars directly because they cannot undergo widening (=cast to int) and boxing conversion together. If you cast it to an int, then you only need boxing conversion and that can be done automatically.

Paweł Baczyński
Bartender
Posts: 2236
63
• 2
• Number of slices to send:
Optional 'thank-you' note:
myList.set(0, +'F');
myList.set(0, (int)'F');

I have never seen + operator used in such way.
I don't know if the confusion is worth saving few keystrokes.

What do you think?

Campbell Ritchie
Marshal
Posts: 78659
374
• Number of slices to send:
Optional 'thank-you' note:
Now that is a first. I have never seen a possible use for the unary plus operator but I think you have got one there. Well done Paweł.

Bartender
Posts: 2910
150
• Number of slices to send:
Optional 'thank-you' note:

Paweł Baczyński wrote:
I have never seen + operator used in such way.

+1 For creativity

Campbell Ritchie
Marshal
Posts: 78659
374
• Number of slices to send:
Optional 'thank-you' note:
Since this isn't the “beginning” forum, at least we can be confident that everybody reading it knows what the unary plus operator does.

Can't we???‍???

Java Cowboy
Posts: 16084
88
• Number of slices to send:
Optional 'thank-you' note:
Note that a character such as 'F' is not an Integer, so you cannot store it in a list that can only hold Integer objects.

Sheriff
Posts: 5549
326
• Number of slices to send:
Optional 'thank-you' note:

salvin francis wrote:+1 For creativity

49 for creativity .... .... +'1' .... get it? .... get it?

I'll get my coat.