Solved

Saving and numbering individual letters in a field.

Posted on 2004-09-08
11
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

617 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