Question

Write a Java program that reads a series of strings from a user until STOP is...

Write a Java program that reads a series of strings from a user until STOP is entered. Each input consists of information about one student at the ABC Professional School. The input consists of the student’s last name (the first 15 characters with extra blanks on the right if the name is not 15 characters long), the student’s number (the next 4 characters, all digits, a number between 1000 and 5999), and the student’s program of study (one character, either B for Business, C for Computing, L for Law, N for Nursing or T for Tourism). This information must be read in as one string, and broken into the individual pieces. As the code tries to break the string into its parts, exceptions may be thrown. Some are Java exceptions (which ones?) and some are programmer created exceptions (which ones?) Catch these in main and print out a description of the problem encountered. Be specific. For example rather than outputting, “invalid student number” state that “6789 is an invalid student number”. Input a series of these strings, so that all possible exceptions are tested, then input STOP when you are done.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.Scanner;
public class MyClass {
public static void main(String args[]) {
  
Scanner sc=new Scanner(System.in);
String inp,num;
char programm;
while(true){
inp=sc.nextLine();
if(inp.equals("STOP")==true){
break;
}
if(inp.length()==20){
if(inp.charAt(14)==' '){
System.out.println("Name : "+inp.substring(0,14));
}
else{
System.out.println("Name : "+inp.substring(0,15));
}
num=inp.substring(15,19);
try{
int num1=Integer.parseInt(num);
if(num1>=1000 && num1<=6000){
System.out.println("Number : "+num);
}
else{
System.out.println(num + "is an invalid student number");
continue;
}
}
catch(Exception e){
System.out.println(num+"is an invalid student(not an integer) number");
continue;
}
programm=inp.charAt(19);
if(programm=='B'){
System.out.println("Programm : Business");
}
else if(programm=='C'){
System.out.println("Programm : Computing");
}
else if(programm=='L'){
System.out.println("Programm : Law");
}
else if(programm=='N'){
System.out.println("Programm : Nursing");
}
else if(programm=='T'){
System.out.println("Programm : Tourism");
}
else{
System.out.println("Invalid Programm");
}
}
else{
String split[]=inp.split(" ");
if(split.length==2){
System.out.println("Name : "+split[0]);
if(split[1].length()==5){
num=inp.substring(0,5);
try{
int num1=Integer.parseInt(num);
if(num1>=1000 && num1<=6000){
System.out.println("Number : "+num);
}
else{
System.out.println(num + "is an invalid student number");
continue;
}
}
catch(Exception e){
System.out.println(num+"is an invalid student(not an integer) number");
continue;
}
programm=inp.charAt(5);
if(programm=='B'){
System.out.println("Programm : Business");
}
else if(programm=='C'){
System.out.println("Programm : Computing");
}
else if(programm=='L'){
System.out.println("Programm : Law");
}
else if(programm=='N'){
System.out.println("Programm : Nursing");
}
else if(programm=='T'){
System.out.println("Programm : Tourism");
}
else{
System.out.println("Invalid Programm");
}   
}
else{
System.out.println("Invalid Input");   
}
}
else{
System.out.println("Invalid Input");
}
}
}
  
}
}

Output:

Add a comment
Know the answer?
Add Answer to:
Write a Java program that reads a series of strings from a user until STOP is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

  • Write a program that uses String method regionMatches to compare two strings input by the user....

    Write a program that uses String method regionMatches to compare two strings input by the user. The program should prompt the user to enter two strings, the starting index in the first string, the starting index in the second string, and the number of characters to be compared. The program should print whether or not the strings are equal, (Ignore the case of the characters during comparison.) 四SAMPLE RUN #1: java StringCompare Highlight: None D Show Highlighted Only Interactive Session...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • Need Java help: 1. Create a Java program that accepts input String input from a user...

    Need Java help: 1. Create a Java program that accepts input String input from a user of first name. 2. Create a Java program that accepts input String input from a user of last name. 3. Concatenate the Strings in a full name variable and print to the console.

  • JAVA STRINGS AND CHARACTERS - USE SIMPLE CODING 1. Write a program that takes a string...

    JAVA STRINGS AND CHARACTERS - USE SIMPLE CODING 1. Write a program that takes a string of someone's full name and prints it out last name first. You may use the available string manipulation methods. Example: "George Washington" "Washington, George" 2. Write a program that will take a string of someone's full name, and break it into separate strings of first name and last name, as well as capitalize the first letter of each. Example: "joseph smith", "Joseph" "Smith"

  • Design and implement a Java program (name it PasswordTest) that accepts a string from the user...

    Design and implement a Java program (name it PasswordTest) that accepts a string from the user and evaluates it as a password, giving it a valid or invalid verdict A good password will be at least 8 characters and includes at least one lower-case letter, at least one upper-case letter, at least one digit, and at least one character that is neither a letter nor a digit. Your program will need to check each character in the string in order...

  • Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size...

    Task Algorithms: Pattern Matching (in java) Write a program that gets two strings from user, size and pattern, and checks if pattern exists inside size, if it exists then program returns index of first character of pattern inside size, otherwise it returns -1. The method should not use built-in methods such as indexOf , find, etc. Only charAt and length are allowed to use. Analyze the time complexity of your algorithm. Your solution is not allowed to be> = O...

  • Could you guys write an efficient java program to implement BST. Your java program should read...

    Could you guys write an efficient java program to implement BST. Your java program should read words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in xxxxx5.java file (5th java project), where xxxxx is the first 5 characters of your last name. To compile: javac xxxxx5.java To execute: java xxxxx5 < any data file name Your main method should...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT