| | |
Need help! (Simple programs...)
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
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.
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.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Jun 2007
Posts: 59
Reputation:
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:
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.
Java Syntax (Toggle Plain Text)
int anIntegerValue = Integer.parseInt(inputString.substring(4, 7));
•
•
•
•
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:
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.Java Syntax (Toggle Plain Text)
int anIntegerValue = Integer.parseInt(inputString.substring(4, 7));
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.
•
•
Join Date: Sep 2008
Posts: 1,580
Reputation:
Solved Threads: 199
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 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
•
•
Join Date: Oct 2008
Posts: 37
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- How to create image maps that look so nice... (Graphics and Multimedia)
- Installing Basic Programs (*nix Software)
- Please help me to convert a simple C++ program into an object-oriented one. (C++)
- some simple problems... (C++)
- need help on simple turbo C program.. thx (C)
- Compiling Programs (C++)
- simple program tokenizer problem (Java)
- Any body who's "tired" of "trying" programs to remove anything... (Viruses, Spyware and other Nasties)
- Using time in programs (C++)
- slow computer (Windows NT / 2000 / XP)
Other Threads in the Java Forum
- Previous Thread: ArrayList
- Next Thread: Hi .Facing problem while displaying LinkedList (API) element using Iterator
| Thread Tools | Search this Thread |
3d 6 @param affinetransform android api applet application arc array arrays automation binary bluetooth bold byte c++ chat class client code color compare component coordinates database detection doctype eclipse educational error file fractal froglogic game givemetehcodez graphics gui guitesting helpwithhomework html ide ideas image ingres input integer internet intersect j2me java java.xls javaexcel javaprojects jni jpanel jtextarea julia keytool keyword linux list loop map method methods mobile netbeans newbie nextline object pong print problem producer program programming project projectideas read recursion recursive replaysolutions rim scanner sell server set size sms sort sql string swing terminal threads tree web websites windows






