954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help! (Simple programs...)

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.

IMtheBESTatJAVA
Light Poster
36 posts since Oct 2008
Reputation Points: -1
Solved Threads: 0
 

Read it as the string and then parse it to integer

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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?)

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

Chaster
Junior Poster in Training
68 posts since Jun 2007
Reputation Points: 12
Solved Threads: 3
 

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.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

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.

IMtheBESTatJAVA
Light Poster
36 posts since Oct 2008
Reputation Points: -1
Solved Threads: 0
 

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!

IMtheBESTatJAVA
Light Poster
36 posts since Oct 2008
Reputation Points: -1
Solved Threads: 0
 

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

,Thank you

camcamcam
Newbie Poster
1 post since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You