Saving and numbering individual letters in a field.

I have this homework where I have to input a license plate which is supposedly three letters followed by three numbers, I have to store the three letters and then convert them to their ASC2 values, butIi don't know how to input it because my workbook only has readline, readtolken, readInt, and readBoolean, and so I don't know how to individually store the letters so I can convert them because in the end  have to add those numbers up, basically I am using the consoleIO class.

Here's what I have written, but it doesn't work at all:

import chn.util.*;
import apcslib.*;
public class CarRentalCh4
{
      public static void main(String[] args)
      {
            ConsoleIO console=new ConsoleIO();
            int number;
            char letter1, letter2, letter3;
            System.out.print("Enter license plate ");
            letter1=console.readInt();
            letter2=console.readInt();
            letter3=console.readInt();
            number= console.readInt();
            System.out.println(number);
      }
            
}

This assignment is due tommorow, so big points.
maxishereAsked:
Who is Participating?
 
suprapto45Commented:
Hello,

Yes, just use the getChars() and you will get each of the individual character into char[]. Then you can simply move each of the value in the char[] into the int number, char letter1, letter2, letter3;

Mmmm....so you can have something like this.

char[] indi = new char[4];

indi = console.readLine().getChars();

number = char[0];
letter1 = char[1];
letter2 = char[2];
letter3 = char[3];
       
I hope that helps
0
 
ZylochCommented:
Hey,

I'm not an expert on Java, but I will tell you that answering homework is not allowed on this site. We can probably point you in the right direction, but we can't disclose the answer to you.


0
 
armoghanCommented:
I think assigments are not allowed in EE
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
objectsCommented:
How about using readLine() to get a String, and then using the String classes charAt() method to access the individual characters.
0
 
tomboshellCommented:
Here is a big tip.  Take a weekend (I know it is hard to break away from the parties) and familiarize yourself with the javadocs and tutorials.  Go through some of the tutorials.  It will help you.  Trust me, computer programming requires time.  But if you spend the time to really learn the basics then homework assignments like that will become so easy that you should be almost insulted at getting such an easy assignment.  

Seriously, take the time now to learn the basics and become familiar enough with where in the documentation to look to find answers or you WILL be swamped later.  

http://java.sun.com/j2se/1.4.2/docs/index.html  if you are using the 1.4.2 J2SE that is there are others for the other versions
http://java.sun.com/reference/api/index.html
http://java.sun.com/j2se/learning/tutorial/index.html
http://java.sun.com/docs/books/tutorial/index.html
0
 
RuadRauFlessaCommented:
String.getChars() is your answer

You get an array of characters and you could from there on do with them what you want.

If you still want the numerical stuff to go into various int variables then take a look at the java.util.Integer.parse() method it is static so you don't need to create an instance of the Integer class for it to work.

Anyway ppl it looks as if the guy actually tried something so no harm in that is there.
0
 
RuadRauFlessaCommented:
Also letter1=console.readInt(); won't really work in your situation since you actually want byte values to cast into your char's. If you read an integer and then parse it into a char you will loose the msb part of the value and thus have incorrect stuff.

Read a single line with console.readln() or something else you wish to use and then break it up and paste it back together as you need it to be. If you read the whole string and it looks like this:

ABC123

then you simply break it into an array of characters with String.getChars() and you get an array that looks like this:

{'A','B','C','1','2','3'}

You can then just copy the values from array[0..2] to your letter1, letter2 and letter3. then if you want the last three numerical characters to appear in a single int value simply concatenare the last three elements of the array[3..5] together and parse them with java.util.Integer.parse(String) into and intiger value.
0
 
maxishereAuthor Commented:
aye, thanks suprapto, that did it.

i usually wouldnt ask for help on homework, but the book just... didn't say how.

i think it was published before java was invented or something. not a quality book.
0
 
ZylochCommented:
I've read those sorts of books before. They do it so you flunk and have to pay to take the course over.
0
 
suprapto45Commented:
You are welcome. Glad to help you :)
0
 
maxishereAuthor Commented:
ah, but this is good ole fashioned free public education.

they just do it because they don't want to spend money on a decent book.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.