Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 12.11, Problem 12.11.8CP
Program Plan Intro
Scanner class:
The Scanner class helps to create the object for Scanner for reading the input strings from console and also inputs are delimited by whitespace characters.
The syntax to create an object for Scanner class using System.in is given below:
Scanner objName = new Scanner(System.in);
The syntax to create an object for Scanner class using input file is given below:
Scanner objName = new Scanner(new File(filename));
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
What will be output to the screen when the following code is executed:def PrintMe(x): try: return s * 5 except: print("Error has occured") finally: print("Doing some cleanup")
Java- Write a program that prints out the calls for a spaceship that is about to launch. Write "Ready?", then write the numbers 10 down to 1, and then write "Liftoff!". You must use a loop. Don't use scanner object.
// Prompt user for value to start
// Value must be between 1 and 20 inclusive
// At command line, count down to blastoff
// With a brief pause between each displayed value
import java.util.*;
public class DebugSix3
{
publicstaticvoidmain(String[] args)
{
Scanner input =newScanner(System.in);
String userNumString;
int userNum, val;
finalint MIN =1;
finalint MAX =20;
System.out.println("Enter a number between " + MIN + " and " + MAX + " inclusive");
userNumString = input.next();
userNum = Interger.parseInt(userNumString);
while(userNum <MIN|| userNum =MAX)
{
System.out.println("Number out of range" +
"\nEnter a number between "+MIN+" and "+MAX+" inclusive");
userNumString = input.next();
userNum = Integer.parse(userNumString);
}
for(val = userNum; val <0; --val)
{
System.out.print(val + " ");
for(int x =0; x <100000; ++x)
for(int y =0; y <10000; ++y);
// Adjust these numbers for faster or slower performance
}
System.out.println("Blastoff!");
}
}
Chapter 12 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 12.2 - Prob. 12.2.1CPCh. 12.2 - Prob. 12.2.2CPCh. 12.2 - Prob. 12.2.3CPCh. 12.2 - Prob. 12.2.4CPCh. 12.2 - Prob. 12.2.5CPCh. 12.2 - Show the output of the following code:Ch. 12.3 - Prob. 12.3.1CPCh. 12.3 - Prob. 12.3.2CPCh. 12.4 - Prob. 12.4.1CPCh. 12.4 - Prob. 12.4.2CP
Ch. 12.4 - Prob. 12.4.3CPCh. 12.4 - Prob. 12.4.4CPCh. 12.4 - Prob. 12.4.5CPCh. 12.4 - Prob. 12.4.6CPCh. 12.4 - What is displayed when running the following...Ch. 12.4 - Prob. 12.4.8CPCh. 12.4 - What does the method getMessage() do?Ch. 12.4 - What does the method printStackTrace() do?Ch. 12.4 - Prob. 12.4.11CPCh. 12.4 - Prob. 12.4.12CPCh. 12.5 - Prob. 12.5.1CPCh. 12.6 - Prob. 12.6.1CPCh. 12.7 - Prob. 12.7.1CPCh. 12.8 - Prob. 12.8.1CPCh. 12.9 - Prob. 12.9.1CPCh. 12.9 - Prob. 12.9.2CPCh. 12.10 - What is wrong about creating a File object using...Ch. 12.10 - How do you check whether a file already exists?...Ch. 12.10 - Can you use the File class for I/O? Does creating...Ch. 12.11 - Prob. 12.11.1CPCh. 12.11 - Prob. 12.11.2CPCh. 12.11 - Prob. 12.11.3CPCh. 12.11 - Prob. 12.11.4CPCh. 12.11 - What will happen if you attempt to create a...Ch. 12.11 - Prob. 12.11.6CPCh. 12.11 - Suppose you enter 45 57, 8 789, then press the...Ch. 12.11 - Prob. 12.11.8CPCh. 12.12 - How do you create a Scanner object for reading...Ch. 12.13 - Prob. 12.13.1CPCh. 12.13 - Simplify the code in lines 20-28 as follows: 1....Ch. 12 - Prob. 12.1PECh. 12 - (InputMismatchException) Write a program that...Ch. 12 - (ArrayIndexOutOfBoundsException) Write a program...Ch. 12 - (IllegalArgumentException) Modify the Loan class...Ch. 12 - (IllegalTriangleException) Programming Exercise...Ch. 12 - (NumberFormatException) Listing 6.8 implements the...Ch. 12 - Prob. 12.7PECh. 12 - Prob. 12.8PECh. 12 - Prob. 12.9PECh. 12 - Prob. 12.10PECh. 12 - Prob. 12.11PECh. 12 - (Reformat Java source code) Write a program that...Ch. 12 - (Count characters, words, and lines in a file)...Ch. 12 - (Process scores in a text file) Suppose a text...Ch. 12 - (Write/read data) Write a program to create a file...Ch. 12 - Prob. 12.16PECh. 12 - (Game: hangman) Rewrite Programming Exercise 7.35....Ch. 12 - Prob. 12.18PECh. 12 - (Count words) Write a program that counts the...Ch. 12 - Prob. 12.20PECh. 12 - (Data sorted?) Write a program that reads the...Ch. 12 - Prob. 12.22PECh. 12 - (Process scores in a text file on the Web) Suppose...Ch. 12 - (Create large dataset) Create a data file with...Ch. 12 - (Create a directory) Write a program that prompts...Ch. 12 - Prob. 12.26PECh. 12 - (Replace words) Suppose you have a lot of files in...Ch. 12 - (Rename files) Suppose you have a lot of files in...Ch. 12 - (Rename files) Suppose you have several files in a...Ch. 12 - (Occurrences of each letter) Write a program that...Ch. 12 - (Baby name popularity ranking) The popularity...Ch. 12 - (Ranking summary) Write a program that uses the...
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Does the following code cause a memory leak?int* pValue = new int;*pValue = 45;pValue = new int;delete pValue; options: True Falsearrow_forwardSuppose that the input is 39 21 15 60 150 120. What is the output of the following code? Assume all variables are properly declared. Show your trace. Scanner keyboard = new Scanner(System.in); // Create a Scanner object to read input (“Enter 6 integers: ”); // inform user to input 6 integers sum = 0; for(int b=1; b < 4; b++) { System.out.println(“Enter number ” +b + “:”); n = keyboard.nextInt(); sum += n; // sum = sum + n } System.out.println(“Sum is ” + sum + “, last input is ” + n + “ and b value is ” + b + “.”);arrow_forwardNEED HELP PLEASE! Write a Java program to read a text file (command line input for file name), process the text file and perform the following. Print the total number of words in the file. Print the total number of different words (case sensitive, meaning “We” and “we” are two different words) in the file. Print all words in ascending order (based on the ASCII code) without duplication. Write a pattern match method to find the location(s) of a specific word (a character string up to twelve characters, e.g. system). This method should return all line number(s) and location(s) of the word(s) found in the file. Print all line(s) with line number(s) of the file where the word is funds by invoking the method of 4). Under each output line indicate the location(s) of the first character of the matched word (refer to the output example below). Empty lines are lines, they also have their own unique line numbers. Repeat the pattern match (4 and 5 above) until user types in EINPUT. A word is…arrow_forward
- Find the errors in the following code:1. // This code contains ERRORS!// It adds two numbers entered by the user.int num1, num2;String input;char again;Scanner keyboard = new Scanner(System.in);while (again == 'y' || again == 'Y')System.out.print("Enter a number: ");num1 = keyboard.nextInt();System.out.print("Enter another number: ";num2 = keyboard.nextInt();System.out.println("Their sum is "+ (num1 + num2));System.out.println("Do you want to do this again? ");keyboard.nextLine(); // Consume remaining newlineinput = keyboard.nextLine();again = input.charAt(0);arrow_forward1. Please leave comments on the code below as explanations of what it does: def repeatChar(numRepeats, outputChar):'''output the outputChar numRepeats times'''for colNo in range(numRepeats):print(outputChar, end='')def Parrallelogram(side, char):for i in range(side):repeatChar(i+1, char)print()for i in range(side):repeatChar(i+1, ' ')repeatChar(side-i-1, char)print()def outlineParallelogram(side, char):for i in range(side):if (i < 1):repeatChar(1, char)elif(i == 1):repeatChar(2, char)else:repeatChar(1, char)repeatChar(i-1, ' ')repeatChar(1, char)print()for i in range(side-1):if (i == side-2):repeatChar(side-1, ' ')repeatChar(1, char)else:repeatChar(i+1, ' ')repeatChar(1, char)repeatChar(side-i-3, ' ')repeatChar(1, char)print()print('This program will show a parallelogram and its outline')number = int(input('How long do you want each side to be? '))character = input('Enter the character: ')Parrallelogram(number, character)outlineParallelogram(number, character)print()arrow_forwardChapter 4. PC #14. Line Numbers (page 265) Write a java program that asks the user for the name of a file. The program should display the contents of the file with each line preceded with a line number followed by a colon. The line numbering should start at 1. I should be easily type input1.txt and easily print out the output in Eclipse. Please enter the file name or type QUIT to exit:\nquitENTER Test Case 2 Please enter the file name or type QUIT to exit:\nbadfilename.txtENTERFile: badfilename.txt does not exist.\nPlease enter the file name again or type QUIT to exit:\nquitENTER Test Case 3 Please enter the file name or type QUIT to exit:\ninput1.txtENTER1: a\n2: b\n3: c\n4: d\narrow_forward
- RGB LED fading colors project. Write comments for the below C code explaining each line. Code is below ?? #define GREEN 5 #define BLUE 3 #define RED 6 void setup() { pinMode(RED, OUTPUT); pinMode(GREEN, OUTPUT); pinMode(BLUE, OUTPUT); digitalWrite(RED, HIGH); digitalWrite(GREEN, LOW); digitalWrite(BLUE, LOW); } int redValue; int greenValue; int blueValue; void loop() { for( int i=0; iarrow_forwardWrite a line of code that creates a Scanner object named frank to be used forobtaining keyboard input.arrow_forwardWhat will be output to the screen when the following code is executed:def PrintMe(x): try: return s * 5 except: print("Error has occured") finally: print("Doing some cleanup")message = "HUE stands for Hadoop User Experience"print(PrintMe(message))arrow_forward
- Write a program using while loop and for loops to print first 50 prime numbers in 5 lines, each line containing 10 numbers. 1. Create a program called CountCharacters.java. 2. At the top of your java file add the following documentation comments. Replace the italicized text with the appropriate content. /** @author Your Name CS 110 Section 012 Lab 9 Final Today's Date 3. Import the Scanner class and ask user to enter a line. Implement the while loop so that the user can input any number of lines and one line at a time. To stop entering lines, the user can write "quit. (Hint: Use the string method <str>.equals() in the while condition to stop the while loop OR a break keyword). 4. Implement the for loop inside the while loop to iterate through the line one character at a time to count and output the number of blank spaces, alphabetic characters, digit characters, and other characters in all the lines. You need to use different methods of character class like charAt(), isDigit(),…arrow_forwardConsider the following code snippet Scanner keyboard = new Scanner(System.in); String s = keyboard.next(); System.out.println(s); s = keyboard.next(); System.out.println(s); s = keyboard.nextLine(); System.out.println(s); s = keyboard.nextLine(); s = keyboard.nextLine(); Suppose that the user, when prompted, enters the following (hitting enter at the end of each line). one two three four end! second line last line:) Note that in the first line there is 1 space after one, 2 after two, 3 after three and 4 after four. What is displayed to the screen when running this code?arrow_forwards. Consider the statementScanner a = new Scanner(System.in); Here Scanner is the class name, a is the name ofobject, new keyword is used to allocate the memory and System.in is the input stream.Following methods of Scanner classare used in the program below :-1) nextInt to input an integer2) nextFloat to input a float3) nextLine to input a stringJava programming source codearrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTMicrosoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,