Solved

Saving and numbering individual letters in a field.

Posted on 2004-09-08
11
237 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 56
couple of eclipse 5 36
hibernate insert example 13 29
hibernate example issues from command prompt 10 41
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

773 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