Sully Brooks
Sully Brooks

Reputation: 425

charAt method is returning an integer. Need to convert to int

I'm creating a method that will take the given input and set the value to "plate". I then proceed to use the charAt method and try to take the characters(letters/String) input and set it to a new value. But i'm told to set it to a char value. When I run aprint statement with it, the output is just a bunch of integers, or in my case (for the code i'm going to show) it outputs "195" when I put the license plate as abc 123. Also, the model doesn't do anything(or atleast isnt supposed to). If anyone could tell me what exactly i'm doing wrong it would be a great help.

Here is my code so far:

import java.util.Scanner;

public class CarRental {

public static String model;
public static String plate;

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    System.out.println("Car Model:");
    model = input.nextLine();
    System.out.println("License Plate: ");
    plate = input.nextLine();

    char one = plate.charAt(0);
    char two = plate.charAt(1);

    System.out.println(two + one);

}

}

To make my issue clear, what I'm hoping to do is, assign the actual letters I type into the input to a separate value. I'm using the charAt method and it is returning integers.

if anyone could offer a suggestion it would help alot!

Thanks, Sully

Upvotes: 0

Views: 7624

Answers (2)

jlordo
jlordo

Reputation: 37843

the + operator treats 2 chars as ints, try

System.out.println("" + two + one);

Upvotes: 1

mikeswright49
mikeswright49

Reputation: 3416

You can just use

Character.toChars(X) 

Where x is your number to convert to char and then display said chars once they've been converted.

Upvotes: 0

Related Questions