I've recently taken up an AP Computer Science class online for Maryland State education at my high school, and was wondering if I could receive help on a few of the projects. Most of them are simple like programs I made in Visual Basic for my first programming class with simple algorithms. There are a lot of projects given at a fast pace, and we do not have any instructor, but rather just some notes given to us online. Most of the time I have to look things up on the web to figure out how to complete the program.

At the moment I'm working on a Car Rental program that takes the input of the make and model, which are irrelevent to the algorithm, and the license plate number of a rental car. It then uses a formula to calculate the rental code as follows:

Example license plate input: CPR 607

1. Take each char from the input seperately and determine its ASCII value. (C = 67, P = 80, R = 82)
2. Add the ASCII values together and % by 26. (67 + 80 + 82 = 229 ... 229 % 26 = 4)
3. Find the letter position of that number in the alphabet (i.e. A = 0, B = 1 , etc...).
4. Take the sum of the ASCII values and add it to the integer in the input (229 + 607 = 836).
5. Take the letter you found and add the sum of the integers together (E836)

The primary problem I have with this program is step 4, as I do not know how to read the integer after a white space. I know the use of readToken() is usually for this, but it isn't for integers, and readInt() wasn't found. If anyone could help me with this it would be greatly appreciated.

I'd try to give an answer, but as a "beginner here at this ho-dunk little forum" I doubt I can offer any insight on the complexities of "REAL Java programming like the pros"...

(Is Princeton giving credit for Maryland State AP coursework now?)

Well, if you know for sure that the license plate has the format "LLL NNN" (I mean letter by L and number by N), then you can get the integer value like this:

int anIntegerValue = Integer.parseInt(inputString.substring(4, 7));

provided that inputString is a variable of type String, which holds the whole license number. If the license plate may have different types, then just forget about what I've written here.

Well, if you know for sure that the license plate has the format "LLL NNN" (I mean letter by L and number by N), then you can get the integer value like this:

int anIntegerValue = Integer.parseInt(inputString.substring(4, 7));

provided that inputString is a variable of type String, which holds the whole license number. If the license plate may have different types, then just forget about what I've written here.

as far as I can see, this offers in no way any help, for two reasons:
1. he hasn't shown anything from himself yet, so giving him code won't help him learn
2. maybe you haven't read the entire question, but as far as I can see, this answers none of his questions, since he is not asking how to put the number-part in an integer.

Step one: Read the whole string in all at once. This can be accomplished in a lot of different ways. If you're getting it from a file, you could use Scanner input = new Scanner(new FileInputStream(yourFile.txt))); Otherwise, if you're reading it from the keyboard, Scanner input = new Scanner(System.in);
Step two: Read in the entire String using String wholeLicense = input.nextLine();
Step three: (There are a number of ways to do this). Make a for loop that goes through every character from the String, then read it into a new String IF the character isn't whitespace. You can figure out if the character is whitespace by making a new Character object, then using the isWhitespace method.


Now you have a String with all the chars you need but without whitespace

Step one: Read the whole string in all at once. This can be accomplished in a lot of different ways. If you're getting it from a file, you could use Scanner input = new Scanner(new FileInputStream(yourFile.txt))); Otherwise, if you're reading it from the keyboard, Scanner input = new Scanner(System.in);
Step two: Read in the entire String using String wholeLicense = input.nextLine();
Step three: (There are a number of ways to do this). Make a for loop that goes through every character from the String, then read it into a new String IF the character isn't whitespace. You can figure out if the character is whitespace by making a new Character object, then using the isWhitespace method.


Now you have a String with all the chars you need but without whitespace

well.. almost the same remark as my previous post, the second remark that is

The introduction I gave about me attending Princeton was supposed to be a joke. My colleagues and myself wanted to admire the ridiculous (if any) responses to obvious inconsistency, but as far as these posts go, it is in all seriousness. I hadn't been able to access the internet lately, so I wasn't able to update this thread, but here is a look at the code that I have thus far...

public class CarRental
{
  public static void main(String[] args)
  {
      char fin;
      int one, two, three, sum, rem, num;
      
      ConsoleIO console = new ConsoleIO();
      System.out.println("Enter your car's make here ---> ");

      System.out.println("Enter your car's model here ---> ");

      System.out.println("Enter your car's plate number here ---> ");
      String lix = console.readLine();
      num = console.readInt();
      char first = lix.charAt(0);
      char second = lix.charAt(1);
      char third = lix.charAt(2);
      one = first;
      two = second;
      three = third;
      
      sum = one + two + three + num;
      rem = (sum % 26) + 64;
      rem = (char)rem;
      
      System.out.println("Your car's rental code is " + rem + sum);
    }
}

This was the code I had before I posted here, and as you can see, I tried reading it as an integer without any luck. The integer num is the 3-digit integer after the 3 characters and whitespace. I've been limited to few resources and am without any instructor for this course (for the most part), so all help is appreciated.

Edited 6 Years Ago by peter_budo: Adding code tags to old post

Comments
No code tags, shame

Okay I finally got it working and I'm awarding Chaster reputation for solving it. His line of code was the one that solved my program problem. Thanks!

Could you please post the code you got to work im having trouble as well.

,Thank you

This question has already been answered. Start a new discussion instead.