Problem 2 * Write and run a java program that prompts the user to enter the 8 di
ID: 3891000 • Letter: P
Question
Problem 2
* Write and run a java program that prompts the user to enter the 8 digit alpha-numeric code for
a CSET course offered at the University of Toledo, extracts the first 4 characters,
and the last 4 characters as separate strings, and prints the complete course name.
A sample run should look like this:
Enter an 8-digit alphanumeric course name: CSET1200
You entered: CSET1200
The first 4 characters are: CSET
The last 4 characters are: 1200
I already have most of the code already wrote. I'm stuck on a few parts if you could help me and explain why these are errors that would be great.
package alpha.numeric.code; import java . util . Scanner; public class AlphaNumericcode public static void ma in (String[] args) { Scanner input -new Scanner (System.in): I/ Creating the Scanner object int WordLength; String first = " ", last = " ", word " "; System.out.printin ("Enter an 8 digit alpha numeric code "); String su -scan.next) WordLengthsu.length first su . substring (0, Ma th . min (su.length ( ), 4)); = System.out.println ("The first 4 characters are "+ first) last = su.substring (4, Math.m1n(su.length ( ), wordLength)); System.out.println ("The last 4 characters are: "+ last) System.out.println ("The complete coursename is: " first last): alpha.numeric.code.AlphaNumericCode > main> su >Explanation / Answer
Hello,
I analyzed the code. As per the specifications, you are doing right.
The error is on line 21. 'scan' is the variable you're using to get input from the user.
But on line 14, you've declared 'input' as the Scanner object.
So, instead of 'scan', you should use 'input' to get user input because 'scan' is not defined in the program.
You can refer to the underlined 'input' word. This color underline says variable 'input' is declared but it is not used throughout the program.
So, it should be like...
input.next()
Rest of the things are correct. I just wish to add something that will help you in further coding also.. like a piece of advice..
On line 17, you are defining String variable value as " ". Here, you are putting a space in between "".
Later on these variables' value is assigned. So, there's no need to set a space as a value.
So, you can define it as ""
Or, you can just leave it as it is like String first because in Java, the variables are automatically initialized to the default values. For string, the default value is null i.e. ""
Do comment if there is any query. I'll surely address it. Thank you. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.