
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
New JAVA code can only be added between lines 27 and 29. As seen in image.

Transcribed Image Text:**Handling File Input and Output in Java**
This tutorial demonstrates how to read from an input file and write to an output file using Java. The process involves reading floating-point numbers from a specified input file, processing them, and writing the results to an output file.
### Code Overview
1. **Imports**:
- `java.util.Scanner`: For scanning input from the user.
- `java.io.FileOutputStream` and `java.io.PrintWriter`: For writing to a file.
2. **Main Class and Method**:
- The class `WriteLastWidthFile` contains the `main` method. A `Scanner` object is used to read from `System.in`.
- The input file name is read as a string from the user.
- A `PrintWriter` is used to write to the output file.
3. **Variables**:
- `widthDataFile`: A string representing the file name.
- `numValuesRead`: An integer counting the number of valid inputs read.
- `widthLast`: A double to store the last valid width value read.
4. **Workflow**:
- Attempt to open the specified file for writing within a `try` block.
- Read two floating-point numbers from the input.
- Write these numbers to the output file.
- In the `finally` block, the following tasks are executed:
1. Write "Number of input values read:", followed by the count.
2. If there are valid inputs, write the last value.
3. Close the file to ensure data is saved.
5. **Examples**:
- If the input from `width.txt` is valid (e.g., `12.0 11.0`), the output file will contain:
```
12.0
11.0
Number of input values read: 2
Last value is: 11.0
```
- If there is invalid input (e.g., `invalid 11.0`), it will contain:
```
Number of input values read: 0
```
- If the input file path is incorrect (e.g., `/nowhere.txt`), the output will display an error message.
6. **Note**:
- Always close the file output stream to prevent data loss.
This example illustrates basic file handling operations in Java, including reading input

Transcribed Image Text:The image shows a section of Java code designed for reading and writing data. Below is the transcription of the code snippet:
```java
try {
fOutWriter = new PrintWriter(new FileOutputStream(widthDataFile));
for (numValuesRead = 0; numValuesRead < 2; ++numValuesRead) {
valueRead = scnr.nextDouble();
fOutWriter.println(valueRead);
widthLast = valueRead;
}
}
catch (Exception e) {
System.out.println("Error!");
}
/* Your code goes here */
```
### Code Explanation:
- **Lines 16-25 (Try Block):**
- A `PrintWriter` is instantiated with a `FileOutputStream` for `widthDataFile`. This setup is meant for writing data to a file.
- A `for` loop runs, intended to read two double values from an input source (`scnr` assumed to be a `Scanner`).
- Each value read is printed to the output file and stored in the variable `widthLast`.
- **Lines 26-29 (Catch Block):**
- Catches any `Exception` that might occur in the try block and prints "Error!" to the console, serving as basic error handling.
- **Line 28 (Comment):**
- Contains a comment placeholder where additional code can be added.
This snippet highlights file writing operations and control structures in Java, commonly used for managing data input and output efficiently.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 1 images

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
- Using comments within the code itself, can you provide an line by line explanation of the below JavaScript file? The file itself deals with WebGl and if that helps you. Please and thank you JavaScript file: function mat2() { var out = new Array(2); out[0] = new Array(2); out[1] = new Array(2); switch ( arguments.length ) { case 0: out[0][0]=out[3]=1.0; out[1]=out[2]=0.0; break; case 1: if(arguments[0].type == 'mat2') { out[0][0] = arguments[0][0][0]; out[0][1] = arguments[0][0][1]; out[1][0] = arguments[0][1][0]; out[1][1] = arguments[0][1][1]; break; } case 4: out[0][0] = arguments[0]; out[0][1] = arguments[1]; out[1][0] = arguments[2]; out[1][1] = arguments[3]; break; default: throw "mat2: wrong arguments"; } out.type = 'mat2'; return out; } //---------------------------------------------------------------------------- function mat3() { // v = _argumentsToArray( arguments ); var out = new Array(3); out[0] = new Array(3); out[1] = new Array(3); out[2] = new Array(3);…arrow_forwardWrite code in JavaScript by following the instructions given below: • A confirmation dialog box should be appeared right after the page load on the browser. When user press ok button from confirmation dialog box, a prompt dialog box will be shown with question "What is your institution name?" If user gives the answer "BIIT", an alert dialog box will be displayed in response with message "Right!" In case of any other answer, alert dialog box will be displayed in response with message "Wrong!" This page says This page says What is your institution name? Want to see the question to give answer? BIIT OK Cancel OK Cancel This page says Right! OKarrow_forwardI need help creating this page in Java using Java GUI. It's a map of 30 hexagons. The map should have 30 buttons respectively for each of the hexagons, with the numbers as it's name (Ex: button = new JButton ("2")).arrow_forward
- programming in an object-oriented style using Java Use the composite pattern to model your scene. It may be an image, a simulator, a movie, a gaming, or something else entirely.arrow_forwardPlease the code in Java eclipse. Please add comments for each instructions given in the image. I'll appreciate your help. And send the screenshoot of the output.arrow_forwardCreate a UML diagram to document the Cuboid class. You can model this class after the Rectangle class we covered in this chapter. It should have 3 data fields: width, length, height. It should have 3 accessors and 3 mutators. It should have a method that calculates the volume of the cuboid. Save your file in MS Word or PDF format. Hint: In MS Word, you can create a table of 1 column and 3 rows. What is a cuboid? You can consider a cuboid as a cube whose length, width, and height may not be equal to each other.arrow_forward
- Please see the snip for the detail. Note: The polygon have to rotate clockwise in same also counterclock wise have to rotate: here is my code it have issue it only rotate to clockwise but in same time have to rotate to the counterclockwise too but it not plesase fix it or write from new. my code: import turtle as t import math class CenteredPolygon: def __init__(self, n, size, center=(0, 0), pen=None, **kwargs): self.numsides = n self.size = size self.center = center self.corners = [] self.pen = pen if pen is not None else self.init_pen(**kwargs) self.draw() def init_pen(self, **kwargs): pen = t.Turtle() for key, value in kwargs.items(): try: f = getattr(pen, key) f(value) except Exception as e: print(e) return pen def set_corners(self) -> list: p = self.pen p.pu() p.goto(self.center) for i…arrow_forwardPlease help me with my assignment for computer science as I had all the dialog box pop up for me except for "Whew" what am I doing wrong? I am including with this message the coding block for the assignment. import java.awt.event.*;import javax.swing.*;public class MouseWhisperer extends JFrame implements MouseListener {MouseWhisperer() {super("COME CLOSER");setSize(300,100);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);addMouseListener(this);setVisible(true);}public void mouseClicked(MouseEvent e) { setTitle("OUCH"); }public void mousePressed(MouseEvent e) { setTitle("LET GO"); }public void mouseReleased(MouseEvent e) { setTitle("WHEW"); }public void mouseEntered(MouseEvent e) { setTitle("I SEE YOU"); }public void mouseExited(MouseEvent e) { setTitle("COME CLOSER"); }public static void main(String[] args) { new MouseWhisperer(); }} Please advice.arrow_forwardComputer Science Questionarrow_forward
- Full comments and a java language implementation are needed. Write some code so that your name is shown in red font inside a blue frame, then save the file.arrow_forwardChapter 18 Problem 38.PE Textbook: Introduction to Java programming and datastructure. 11th Edition Y. Daniel Liang Publisher: PEARSON ISBN: 9780134670942 This code is taken from your website does not work. package e38; import javafx.application.Application;import javafx.event.ActionEvent;import javafx.event.EventHandler;import javafx.scene.Scene;import javafx.scene.control.Button; import javafx.stage.Stage;import javafx.geometry.Pos;import javafx.scene.control.Label;import javafx.scene.control.TextField;import javafx.scene.control.Textfield;import javafx.scene.layout.BorderPane;import javafx.scene.layout.HBox;import javafx.scene.layout.Pane;import javafx.scene.shape.Line;import javafx.stage.Stage; public class E38 extends Application { @Override public void start(Stage primaryStage) { // Create a pane TreePane tp = new TreePane(); // Create a textfield TextField tfOrder = new TextField(); // Set the Depth…arrow_forward1. How to modify below Java OpenGL to add animation that causes the drawn point to grow and shrink, in a cycle. Hint: use the glPointSize() function, with a variable as the parameter.2. How to modify below Java OpenGL the result from no 1, so that the drawn rectangle (not point) will grow and shrink in a cycle. Possible try to move the rectangle in x-axis. import javax.swing.*;import static com.jogamp.opengl.GL4.*;import com.jogamp.opengl.*;import com.jogamp.opengl.awt.GLCanvas; public class Program2_2 extends JFrame implements GLEventListener { private GLCanvas myCanvas; private int renderingProgram; private int vao[] = new int[1]; public Program2_2() { setTitle("Chapter2 - Program2"); setSize(600, 400); setLocation(200, 200); myCanvas = new GLCanvas(); myCanvas.addGLEventListener(this); this.add(myCanvas); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } public void…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education

Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON

Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education