SlideShare a Scribd company logo
2.7 LAB: Exception handling to detect input String vs. Integer The given program reads a list of
single-word first names and ages (ending with -1), and outputs that list with the age incremented.
The program fails and throws an exception if the second input on a line is a String rather than an
Integer. At FIXME in the code, add a try/catch statement to catch
java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua 21
Mary Beth 19 Stu 33 -1 then the output is: Lee 19 Lua 22 Mary 0 Stu 34 LAB ACTIVITY 2.7.1:
LAB: Exception handling to detect input String vs. Integer
Java Code:
import java.util.Scanner;
import java.util.InputMismatchException;
public class NameAgeChecker {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
String inputName;
int age;
inputName = scnr.next();
while (!inputName.equals("-1")) {
// FIXME: The following line will throw an InputMismatchException.
// Insert a try/catch statement to catch the exception.
try{
age = scnr.nextInt();
System.out.println(inputName + " " + (age + 1));
}
catch (InputMismatchException e) {
scnr.nextLine();
}
inputName = scnr.next();
}
}
}

More Related Content

Similar to 2.7 LAB Exception handling to detect input String vs. Integer The g.pdf

OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETOOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
jinaldesailive
 
Project presentation
Project presentationProject presentation
Project presentationTrung Dang
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
Geetha Manohar
 
What is an exception in java?
What is an exception in java?What is an exception in java?
What is an exception in java?
Pramod Yadav
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
Max Kleiner
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
TekobashiCarlo
 
Exception handling
Exception handlingException handling
Exception handling
Raja Sekhar
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
Maýur Chourasiya
 

Similar to 2.7 LAB Exception handling to detect input String vs. Integer The g.pdf (10)

OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETOOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
 
Project presentation
Project presentationProject presentation
Project presentation
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
Java API, Exceptions and IO
Java API, Exceptions and IOJava API, Exceptions and IO
Java API, Exceptions and IO
 
UNIT 2.pptx
UNIT 2.pptxUNIT 2.pptx
UNIT 2.pptx
 
What is an exception in java?
What is an exception in java?What is an exception in java?
What is an exception in java?
 
Maxbox starter
Maxbox starterMaxbox starter
Maxbox starter
 
java intro.pptx.pdf
java intro.pptx.pdfjava intro.pptx.pdf
java intro.pptx.pdf
 
Exception handling
Exception handlingException handling
Exception handling
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 

More from chughakshit42

2.1 Plan the resource requirements you would require for the project.pdf
2.1 Plan the resource requirements you would require for the project.pdf2.1 Plan the resource requirements you would require for the project.pdf
2.1 Plan the resource requirements you would require for the project.pdf
chughakshit42
 
2.Define the following terms with respect to Measurement System An.pdf
2.Define the following terms with respect to Measurement System An.pdf2.Define the following terms with respect to Measurement System An.pdf
2.Define the following terms with respect to Measurement System An.pdf
chughakshit42
 
2. What code would you use to pick a random number and then let the .pdf
2. What code would you use to pick a random number and then let the .pdf2. What code would you use to pick a random number and then let the .pdf
2. What code would you use to pick a random number and then let the .pdf
chughakshit42
 
2. Two intersecting shear zones have the following attitudes N80E7.pdf
2. Two intersecting shear zones have the following attitudes N80E7.pdf2. Two intersecting shear zones have the following attitudes N80E7.pdf
2. Two intersecting shear zones have the following attitudes N80E7.pdf
chughakshit42
 
2. the string.xml file is located in the_________resource folder. .pdf
2. the string.xml file is located in the_________resource folder. .pdf2. the string.xml file is located in the_________resource folder. .pdf
2. the string.xml file is located in the_________resource folder. .pdf
chughakshit42
 
2. Referring the following information, prepare a proper list of refer.pdf
2. Referring the following information, prepare a proper list of refer.pdf2. Referring the following information, prepare a proper list of refer.pdf
2. Referring the following information, prepare a proper list of refer.pdf
chughakshit42
 
2. List and describe 2 different approaches to interviewing for hiri.pdf
2. List and describe 2 different approaches to interviewing for hiri.pdf2. List and describe 2 different approaches to interviewing for hiri.pdf
2. List and describe 2 different approaches to interviewing for hiri.pdf
chughakshit42
 
2. LightValue enumWrite a simple Enum named LightValue, which list.pdf
2. LightValue enumWrite a simple Enum named LightValue, which list.pdf2. LightValue enumWrite a simple Enum named LightValue, which list.pdf
2. LightValue enumWrite a simple Enum named LightValue, which list.pdf
chughakshit42
 
2. Gravitropism Gravitropism is growth in response to gravity. It is.pdf
2. Gravitropism Gravitropism is growth in response to gravity. It is.pdf2. Gravitropism Gravitropism is growth in response to gravity. It is.pdf
2. Gravitropism Gravitropism is growth in response to gravity. It is.pdf
chughakshit42
 
2. El 2 de abril, Granger Sales decide establecer un fondo de caja c.pdf
2. El 2 de abril, Granger Sales decide establecer un fondo de caja c.pdf2. El 2 de abril, Granger Sales decide establecer un fondo de caja c.pdf
2. El 2 de abril, Granger Sales decide establecer un fondo de caja c.pdf
chughakshit42
 
2. Genel olarak, bir irket kendi sahibine aittir. A. y�neticiler..pdf
2. Genel olarak, bir irket kendi sahibine aittir. A. y�neticiler..pdf2. Genel olarak, bir irket kendi sahibine aittir. A. y�neticiler..pdf
2. Genel olarak, bir irket kendi sahibine aittir. A. y�neticiler..pdf
chughakshit42
 
2. Endor Incorporated has just paid a dividend of $4.49. The firms .pdf
2. Endor Incorporated has just paid a dividend of $4.49. The firms .pdf2. Endor Incorporated has just paid a dividend of $4.49. The firms .pdf
2. Endor Incorporated has just paid a dividend of $4.49. The firms .pdf
chughakshit42
 
2. Baroreceptor reflex is an example of pressure reflex autonom.pdf
2.  Baroreceptor reflex is an example of pressure reflex autonom.pdf2.  Baroreceptor reflex is an example of pressure reflex autonom.pdf
2. Baroreceptor reflex is an example of pressure reflex autonom.pdf
chughakshit42
 
2. Draw the MENU entity as a supertype of the PROMOTIONAL, REGULAR, .pdf
2. Draw the MENU entity as a supertype of the PROMOTIONAL, REGULAR, .pdf2. Draw the MENU entity as a supertype of the PROMOTIONAL, REGULAR, .pdf
2. Draw the MENU entity as a supertype of the PROMOTIONAL, REGULAR, .pdf
chughakshit42
 
2. Define the following terms, provide an example of each, and indic.pdf
2. Define the following terms, provide an example of each, and indic.pdf2. Define the following terms, provide an example of each, and indic.pdf
2. Define the following terms, provide an example of each, and indic.pdf
chughakshit42
 
1What did you do best in your undergraduate studies Have you ever.pdf
1What did you do best in your undergraduate studies Have you ever.pdf1What did you do best in your undergraduate studies Have you ever.pdf
1What did you do best in your undergraduate studies Have you ever.pdf
chughakshit42
 
2. All the statements below are incorrect statements. Briefly explai.pdf
2. All the statements below are incorrect statements. Briefly explai.pdf2. All the statements below are incorrect statements. Briefly explai.pdf
2. All the statements below are incorrect statements. Briefly explai.pdf
chughakshit42
 
14. ila 16. y�zyllarda, talyan bankaclar finansal yeniliklerde lid.pdf
14. ila 16. y�zyllarda, talyan bankaclar finansal yeniliklerde lid.pdf14. ila 16. y�zyllarda, talyan bankaclar finansal yeniliklerde lid.pdf
14. ila 16. y�zyllarda, talyan bankaclar finansal yeniliklerde lid.pdf
chughakshit42
 
15. Compare and contrast several characteristics of a growth stock a.pdf
15. Compare and contrast several characteristics of a growth stock a.pdf15. Compare and contrast several characteristics of a growth stock a.pdf
15. Compare and contrast several characteristics of a growth stock a.pdf
chughakshit42
 
12. Ethical standard plays an important role in research. Which of t.pdf
12. Ethical standard plays an important role in research. Which of t.pdf12. Ethical standard plays an important role in research. Which of t.pdf
12. Ethical standard plays an important role in research. Which of t.pdf
chughakshit42
 

More from chughakshit42 (20)

2.1 Plan the resource requirements you would require for the project.pdf
2.1 Plan the resource requirements you would require for the project.pdf2.1 Plan the resource requirements you would require for the project.pdf
2.1 Plan the resource requirements you would require for the project.pdf
 
2.Define the following terms with respect to Measurement System An.pdf
2.Define the following terms with respect to Measurement System An.pdf2.Define the following terms with respect to Measurement System An.pdf
2.Define the following terms with respect to Measurement System An.pdf
 
2. What code would you use to pick a random number and then let the .pdf
2. What code would you use to pick a random number and then let the .pdf2. What code would you use to pick a random number and then let the .pdf
2. What code would you use to pick a random number and then let the .pdf
 
2. Two intersecting shear zones have the following attitudes N80E7.pdf
2. Two intersecting shear zones have the following attitudes N80E7.pdf2. Two intersecting shear zones have the following attitudes N80E7.pdf
2. Two intersecting shear zones have the following attitudes N80E7.pdf
 
2. the string.xml file is located in the_________resource folder. .pdf
2. the string.xml file is located in the_________resource folder. .pdf2. the string.xml file is located in the_________resource folder. .pdf
2. the string.xml file is located in the_________resource folder. .pdf
 
2. Referring the following information, prepare a proper list of refer.pdf
2. Referring the following information, prepare a proper list of refer.pdf2. Referring the following information, prepare a proper list of refer.pdf
2. Referring the following information, prepare a proper list of refer.pdf
 
2. List and describe 2 different approaches to interviewing for hiri.pdf
2. List and describe 2 different approaches to interviewing for hiri.pdf2. List and describe 2 different approaches to interviewing for hiri.pdf
2. List and describe 2 different approaches to interviewing for hiri.pdf
 
2. LightValue enumWrite a simple Enum named LightValue, which list.pdf
2. LightValue enumWrite a simple Enum named LightValue, which list.pdf2. LightValue enumWrite a simple Enum named LightValue, which list.pdf
2. LightValue enumWrite a simple Enum named LightValue, which list.pdf
 
2. Gravitropism Gravitropism is growth in response to gravity. It is.pdf
2. Gravitropism Gravitropism is growth in response to gravity. It is.pdf2. Gravitropism Gravitropism is growth in response to gravity. It is.pdf
2. Gravitropism Gravitropism is growth in response to gravity. It is.pdf
 
2. El 2 de abril, Granger Sales decide establecer un fondo de caja c.pdf
2. El 2 de abril, Granger Sales decide establecer un fondo de caja c.pdf2. El 2 de abril, Granger Sales decide establecer un fondo de caja c.pdf
2. El 2 de abril, Granger Sales decide establecer un fondo de caja c.pdf
 
2. Genel olarak, bir irket kendi sahibine aittir. A. y�neticiler..pdf
2. Genel olarak, bir irket kendi sahibine aittir. A. y�neticiler..pdf2. Genel olarak, bir irket kendi sahibine aittir. A. y�neticiler..pdf
2. Genel olarak, bir irket kendi sahibine aittir. A. y�neticiler..pdf
 
2. Endor Incorporated has just paid a dividend of $4.49. The firms .pdf
2. Endor Incorporated has just paid a dividend of $4.49. The firms .pdf2. Endor Incorporated has just paid a dividend of $4.49. The firms .pdf
2. Endor Incorporated has just paid a dividend of $4.49. The firms .pdf
 
2. Baroreceptor reflex is an example of pressure reflex autonom.pdf
2.  Baroreceptor reflex is an example of pressure reflex autonom.pdf2.  Baroreceptor reflex is an example of pressure reflex autonom.pdf
2. Baroreceptor reflex is an example of pressure reflex autonom.pdf
 
2. Draw the MENU entity as a supertype of the PROMOTIONAL, REGULAR, .pdf
2. Draw the MENU entity as a supertype of the PROMOTIONAL, REGULAR, .pdf2. Draw the MENU entity as a supertype of the PROMOTIONAL, REGULAR, .pdf
2. Draw the MENU entity as a supertype of the PROMOTIONAL, REGULAR, .pdf
 
2. Define the following terms, provide an example of each, and indic.pdf
2. Define the following terms, provide an example of each, and indic.pdf2. Define the following terms, provide an example of each, and indic.pdf
2. Define the following terms, provide an example of each, and indic.pdf
 
1What did you do best in your undergraduate studies Have you ever.pdf
1What did you do best in your undergraduate studies Have you ever.pdf1What did you do best in your undergraduate studies Have you ever.pdf
1What did you do best in your undergraduate studies Have you ever.pdf
 
2. All the statements below are incorrect statements. Briefly explai.pdf
2. All the statements below are incorrect statements. Briefly explai.pdf2. All the statements below are incorrect statements. Briefly explai.pdf
2. All the statements below are incorrect statements. Briefly explai.pdf
 
14. ila 16. y�zyllarda, talyan bankaclar finansal yeniliklerde lid.pdf
14. ila 16. y�zyllarda, talyan bankaclar finansal yeniliklerde lid.pdf14. ila 16. y�zyllarda, talyan bankaclar finansal yeniliklerde lid.pdf
14. ila 16. y�zyllarda, talyan bankaclar finansal yeniliklerde lid.pdf
 
15. Compare and contrast several characteristics of a growth stock a.pdf
15. Compare and contrast several characteristics of a growth stock a.pdf15. Compare and contrast several characteristics of a growth stock a.pdf
15. Compare and contrast several characteristics of a growth stock a.pdf
 
12. Ethical standard plays an important role in research. Which of t.pdf
12. Ethical standard plays an important role in research. Which of t.pdf12. Ethical standard plays an important role in research. Which of t.pdf
12. Ethical standard plays an important role in research. Which of t.pdf
 

Recently uploaded

ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
Excellence Foundation for South Sudan
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 

Recently uploaded (20)

ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Introduction to Quality Improvement Essentials
Introduction to Quality Improvement EssentialsIntroduction to Quality Improvement Essentials
Introduction to Quality Improvement Essentials
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 

2.7 LAB Exception handling to detect input String vs. Integer The g.pdf

  • 1. 2.7 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua 21 Mary Beth 19 Stu 33 -1 then the output is: Lee 19 Lua 22 Mary 0 Stu 34 LAB ACTIVITY 2.7.1: LAB: Exception handling to detect input String vs. Integer Java Code: import java.util.Scanner; import java.util.InputMismatchException; public class NameAgeChecker { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputName; int age; inputName = scnr.next(); while (!inputName.equals("-1")) { // FIXME: The following line will throw an InputMismatchException. // Insert a try/catch statement to catch the exception. try{ age = scnr.nextInt(); System.out.println(inputName + " " + (age + 1)); } catch (InputMismatchException e) { scnr.nextLine(); } inputName = scnr.next(); } } }