943,692 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2816
  • Java RSS
Oct 16th, 2008
0

Need help! (Simple programs...)

Expand Post »
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.
Similar Threads
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Oct 16th, 2008
-1

Re: Need help! (Simple programs...)

Read it as the string and then parse it to integer
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 871
Code tags enforcer
peter_budo is offline Offline
6,654 posts
since Dec 2004
Oct 16th, 2008
0

Re: Need help! (Simple programs...)

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?)
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 838
Posting Genius
Ezzaral is offline Offline
6,757 posts
since May 2007
Oct 18th, 2008
0

Re: Need help! (Simple programs...)

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:
Java Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 12
Solved Threads: 3
Junior Poster in Training
Chaster is offline Offline
68 posts
since Jun 2007
Oct 18th, 2008
0

Re: Need help! (Simple programs...)

Click to Expand / Collapse  Quote originally posted by Chaster ...
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:
Java Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007
Oct 18th, 2008
0

Re: Need help! (Simple programs...)

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
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Oct 18th, 2008
0

Re: Need help! (Simple programs...)

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
Reputation Points: 919
Solved Threads: 354
Nearly a Posting Maven
stultuske is offline Offline
2,487 posts
since Jan 2007
Oct 20th, 2008
-1

Re: Need help! (Simple programs...)

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...

Java Syntax (Toggle Plain Text)
  1. public class CarRental
  2. {
  3. public static void main(String[] args)
  4. {
  5. char fin;
  6. int one, two, three, sum, rem, num;
  7.  
  8. ConsoleIO console = new ConsoleIO();
  9. System.out.println("Enter your car's make here ---> ");
  10.  
  11. System.out.println("Enter your car's model here ---> ");
  12.  
  13. System.out.println("Enter your car's plate number here ---> ");
  14. String lix = console.readLine();
  15. num = console.readInt();
  16. char first = lix.charAt(0);
  17. char second = lix.charAt(1);
  18. char third = lix.charAt(2);
  19. one = first;
  20. two = second;
  21. three = third;
  22.  
  23. sum = one + two + three + num;
  24. rem = (sum % 26) + 64;
  25. rem = (char)rem;
  26.  
  27. System.out.println("Your car's rental code is " + rem + sum);
  28. }
  29. }

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.
Last edited by peter_budo; Nov 30th, 2010 at 5:27 am. Reason: Adding code tags to old post
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Oct 22nd, 2008
0

Re: Need help! (Simple programs...)

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!
Reputation Points: -1
Solved Threads: 0
Light Poster
IMtheBESTatJAVA is offline Offline
36 posts
since Oct 2008
Nov 29th, 2010
0
Re: Need help! (Simple programs...)
Could you please post the code you got to work im having trouble as well.

,Thank you
Reputation Points: 10
Solved Threads: 0
Newbie Poster
camcamcam is offline Offline
1 posts
since Nov 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Java Forum Timeline: ShoppingCartSystem
Next Thread in Java Forum Timeline: pls i want to know how to read xml file remotely using java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC