Need help! (Simple programs...)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

Need help! (Simple programs...)

 
0
  #1
Oct 16th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,242
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 491
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

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

 
-1
  #2
Oct 16th, 2008
Read it as the string and then parse it to integer
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,503
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 521
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

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

 
0
  #3
Oct 16th, 2008
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?)
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 59
Reputation: Chaster is an unknown quantity at this point 
Solved Threads: 3
Chaster Chaster is offline Offline
Junior Poster in Training

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

 
0
  #4
Oct 18th, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

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

 
0
  #5
Oct 18th, 2008
Originally Posted by Chaster View Post
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,629
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

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

 
0
  #6
Oct 18th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

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

 
0
  #7
Oct 18th, 2008
Originally Posted by BestJewSinceJC View Post
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

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

 
-1
  #8
Oct 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 37
Reputation: IMtheBESTatJAVA has a little shameless behaviour in the past 
Solved Threads: 0
IMtheBESTatJAVA IMtheBESTatJAVA is offline Offline
Light Poster

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

 
0
  #9
Oct 22nd, 2008
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC