• Post Reply Bookmark Topic Watch Topic
  • New Topic

Char to Integer  RSS feed

 
Molayo Decker
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am storing numbers in my ArrayList

 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about
myList.set(0, +'F');
instead of
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: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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ł.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:
I have never seen + operator used in such way.


+1 For creativity
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???‍???
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:+1 For creativity

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

I'll get my coat.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!