Solved

Saving and numbering individual letters in a field.

Posted on 2004-09-08
11
235 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 45
maven java path setting 5 50
json example 39 116
Groovy:unable to resolve class error 2 32
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now