Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Saving and numbering individual letters in a field.

Posted on 2004-09-08
11
Medium Priority
?
242 Views
Last Modified: 2008-03-03
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.
0
Comment
Question by:maxishere
  • 2
  • 2
  • 2
  • +4
11 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 12013491
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
 
LVL 18

Expert Comment

by:armoghan
ID: 12013496
I think assigments are not allowed in EE
0
 
LVL 92

Expert Comment

by:objects
ID: 12013498
How about using readLine() to get a String, and then using the String classes charAt() method to access the individual characters.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:tomboshell
ID: 12013782
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
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12016471
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
 
LVL 3

Expert Comment

by:RuadRauFlessa
ID: 12016700
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
 
LVL 16

Accepted Solution

by:
suprapto45 earned 2000 total points
ID: 12018574
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
 

Author Comment

by:maxishere
ID: 12023635
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
 
LVL 36

Expert Comment

by:Zyloch
ID: 12023651
I've read those sorts of books before. They do it so you flunk and have to pay to take the course over.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12029149
You are welcome. Glad to help you :)
0
 

Author Comment

by:maxishere
ID: 12031605
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

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question