A state's Department of Motor Vehicles needs a program to generate license plate numbers. A license plate number consists of three letters followed by three digits, as in CBJ523. (So "number" is a bit inaccurate, but that's the standard word used for license plates). The plate numbers are given out in sequence, so given the current number, the program should output the next number. If the input is CBJ523, the output should be CBJ524. If the input is CBJ999, the output should be CBK000. For the last number ZZZ999, the next is AAA000.
Hints:
Treat each character individually.
Initially, don't try to create an "elegant" solution. Just consider each of the six places one at a time, starting from the right.
For each place, if less than the max for that place ('9' for digits, 'Z' for letters), just increment that place. (Note that you can just type c = c + 1 to get the next higher character for a digit like '5' or for a letter like 'K').
If a place is at the max, set it with the '0' or 'A', and then set a boolean variable to true to indicate a "carry" is needed. (If a carry isn't needed, set to false).
With the above process, you'll have 6 separate if-else statements.
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
/* Provide your code here. */
CODE: (compiled in bluej)
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter the license plate number: ");
String l=scnr.nextLine();
boolean car=false; //To keep a track of if carry is required.
if(l.charAt(5)<'9') //If the rightmost digit is less than
9.
{
char c=l.charAt(5); //extract the digit.
c=(char)(c+1); //increment it by one. char is used for type
casting.
l=l.substring(0,5)+c; //add the new character to the remaining
string.
}
else
{
car=true; //setting carry to true, so that the next character is
checked and incremented.
l=l.substring(0,5)+'0'; //setting the value to 0.
}
if(car==true && l.charAt(4)<'9') //we check this only if
car==true,
{
char c=l.charAt(4);
c=(char)(c+1);
l=l.substring(0,4)+c+l.substring(5);
car=false; //if we have incremented the value,we set it to false,
so that the others are not checked.
}
else if(car==true) //if the value is equal to 9, we need to set
this value to 0 as well and move to the next.
{
l=l.substring(0,4)+'0'+l.substring(5); //take the first 4
characters, add a 0, then take the rest of the characters.
}
//The same process is repeated for all. if the character is less
than 9 or Z, we increment it and set car to false, else we set it
to 0 or A
//and check for the next character or number.
if(car==true && l.charAt(3)<'9')
{
char c=l.charAt(3);
c=(char)(c+1);
l=l.substring(0,3)+c+l.substring(4);
car=false;
}
else if(car==true)
{
l=l.substring(0,3)+'0'+l.substring(4);
}
if(car==true && l.charAt(2)<'Z')
{
char c=l.charAt(2);
c=(char)(c+1);
l=l.substring(0,2)+c+l.substring(3);
car=false;
}
else if(car==true)
{
l=l.substring(0,2)+'A'+l.substring(3);
}
if(car==true && l.charAt(1)<'Z')
{
char c=l.charAt(1);
c=(char)(c+1);
l=l.substring(0,1)+c+l.substring(2);
car=false;
}
else if(car==true)
{
l=l.substring(0,1)+'A'+l.substring(2);
}
if(car==true && l.charAt(0)<'Z')
{
char c=l.charAt(0);
c=(char)(c+1);
l=l.substring(0,0)+c+l.substring(1);
car=false;
}
else if(car==true)
{
l=l.substring(0,0)+'A'+l.substring(1);
}
System.out.println("The new license number will be: "+l);
}
}
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Enter the license plate number: ");
String l=scnr.nextLine();
boolean car=false; //To keep a track of if carry is required.
if(l.charAt(5)<'9') //If the rightmost digit is less than 9.
{
char c=l.charAt(5); //extract the digit.
c=(char)(c+1); //increment it by one. char is used for type casting.
l=l.substring(0,5)+c; //add the new character to the remaining string.
}
else
{
car=true; //setting carry to true, so that the next character is checked and incremented.
l=l.substring(0,5)+'0'; //setting the value to 0.
}
if(car==true && l.charAt(4)<'9') //we check this only if car==true,
{
char c=l.charAt(4);
c=(char)(c+1);
l=l.substring(0,4)+c+l.substring(5);
car=false; //if we have incremented the value,we set it to false, so that the others are not checked.
}
else if(car==true) //if the value is equal to 9, we need to set this value to 0 as well and move to the next.
{
l=l.substring(0,4)+'0'+l.substring(5); //take the first 4 characters, add a 0, then take the rest of the characters.
}
//The same process is repeated for all. if the character is less than 9 or Z, we increment it and set car to false, else we set it to 0 or A
//and check for the next character or number.
if(car==true && l.charAt(3)<'9')
{
char c=l.charAt(3);
c=(char)(c+1);
l=l.substring(0,3)+c+l.substring(4);
car=false;
}
else if(car==true)
{
l=l.substring(0,3)+'0'+l.substring(4);
}
if(car==true && l.charAt(2)<'Z')
{
char c=l.charAt(2);
c=(char)(c+1);
l=l.substring(0,2)+c+l.substring(3);
car=false;
}
else if(car==true)
{
l=l.substring(0,2)+'A'+l.substring(3);
}
if(car==true && l.charAt(1)<'Z')
{
char c=l.charAt(1);
c=(char)(c+1);
l=l.substring(0,1)+c+l.substring(2);
car=false;
}
else if(car==true)
{
l=l.substring(0,1)+'A'+l.substring(2);
}
if(car==true && l.charAt(0)<'Z')
{
char c=l.charAt(0);
c=(char)(c+1);
l=l.substring(0,0)+c+l.substring(1);
car=false;
}
else if(car==true)
{
l=l.substring(0,0)+'A'+l.substring(1);
}
System.out.println("The new license number will be: "+l);
}
}
OUTPUT:
Enter the license plate number:
ZZZ999
The new license number will be: AAA000
Enter the license plate number:
CBJ999
The new license number will be: CBK000
You can check for your own outputs as well. If you have any doubts or require any clarifications just leave a comment and i will answer it as soon as possible. Thank you :)
Get Answers For Free
Most questions answered within 1 hours.