Using charAt method, won't add them as an Int, and wont print as string. Will explain better

大城市里の小女人 提交于 2019-12-20 07:17:40

问题


Alright, so here is my code:

import java.util.Scanner;

public class CarRental {

    public static String model;
    public static int letternum;
    public static String plate;
    public static String letter;
    public static int total;              
    public static String alphabet = "abcdefghijklmnopqrstuvwxyz";

    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);
        char three = plate.charAt(2);
        total = one + two + three;
        letternum = total % 24;

        char letter = alphabet.charAt(letternum);

        System.out.println("" + letter + total);

    }
}

What is going on is this, I'm trying to make it take my license plate input and take the characters at the places for 0, 1, and 2. Which in a license plate would be the three letters. Then, i'm trying to take their ASCII Values, add them all together and set them to the int "total". Then to find a Letter that is supposed to be in front of the total value, I find the remainder of the total by using % 6. Then it will take that value, and whatever number it is, say it is 4, it will take the 4th letter in the string "alphabet" and set that to a the char "letter". Then what it should do is print out the letter followed by the totals of the ASCII Value.

Here is an example of what is my input with expected outcome, followed by its ACTUAL outcome.

License Plate: CPR 607

Output: E836

My output with the exact same license plate is:

License Plate: CPR 607

n229

I'm not sure what I'm doing wrong, but my best clue is that the fact that it is a char, it is treating it like its ASCII Value, rather than its String value(which i'm actually trying to get)

If anyone could suggest some tips, it would be a great help. Not necessarily code I can just leech off of, but how I should go about doing this the right way!


回答1:


You want to take the second part of the string (with the three numbers) and add it to your total. You can take that value with:

Integer.parseInt(plate.split(" ")[1])




回答2:


Change these lines:

int one = (int) plate.charAt(0);
int two = (int) plate.charAt(1);
int three = (int) plate.charAt(2);

This will give you the actual ASCII values for the characters.

If you want something else, you'll have to subtract a constant from each of the values, as jonhopkins illustrated in his comment.

Subtract 64 to get A = 1, B = 2, etc.

I see your problem.

The algorithm is to take the ASCII values of the first 3 characters and add them to the number (the last 3 characters).

Also, you have to divide by 6 to get the letters A - E. You're dividing by 24.




回答3:


If you add the 229 value that you to the 607 in the license plate, you get the 836 number you say you're supposed to get, so it looks like your total variable is right, but you just need to add it to the number from the input.

The stuff everyone else is saying about shifting the ASCII values is for when you're determining the first character in the output.




回答4:


public static String model;
public static int letternum;
public static String plate;
public static String letter;
public static int total;              
public static String alphabet = "abcdefghijklmnopqrstuvwxyz";

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);
    char three = plate.charAt(2);
    total = Integer.parseInt(one) + Integer.parseInt(two) + Integer.parseInt(three);
    letternum = total % 24;

    char letter = alphabet.charAt(letternum);

    System.out.println("" + letter + total);

}

you forgot to cast it to integer



来源:https://stackoverflow.com/questions/13770492/using-charat-method-wont-add-them-as-an-int-and-wont-print-as-string-will-ex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!