Question

In: Computer Science

This week we really want to learn about a file I/O in Java. In Java: 1)...

This week we really want to learn about a file I/O in Java.

In Java:

1) Create a plain empty text file named contacts.

2) Populate the text file with a person's name and account number on each line(Joe Doe 123456789). Create 10 accounts.

3) Store that file in the same location as your project

4) Write a program that will find that file and load it into the computers memory.

5) Read each line of the file and print the associated name but NOT the account number (remember the account number is 9 characters fixed)

Solutions

Expert Solution

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {
        String fileName = "contacts.txt";
        Random random = new Random();
        PrintWriter printWriter = new PrintWriter(fileName);
        for (int i=1; i<=10; i++) {
            String name = "Name" + i;
            long accountNumber = (long) (random.nextDouble() * 1000000000);
            printWriter.write(name + " " + accountNumber + "\n");
        }
        printWriter.close();

        Scanner sc = new Scanner(new File(fileName));
        System.out.println("The names stored in the file are: ");
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String name = line.split(" ")[0];
            System.out.println(name);
        }
    }
}

OUTPUT


Related Solutions

This week we really want to learn about a file I/O in Java. In Java: 1)...
This week we really want to learn about a file I/O in Java. In Java: 1) Create a plain empty text file named contacts. 2) Populate the text file with a person's name and account number on each line(Joe Doe 123456789). Create 10 accounts. 3) Store that file in the same location as your project 4) Write a program that will find that file and load it into the computers memory. 5) Read each line of the file and print...
This week we will learn about the "They Say I Say" thesis statement. You will use...
This week we will learn about the "They Say I Say" thesis statement. You will use this type of thesis statement in your essay writing. This type of statement allows you to establish a place within the conversation of the subject rather then simply just present your view. It requires that you combine your prior knowledge with other's views and can established a thoughtful, educated and credible argument. You have been preparing for this with your prior active reading and...
This week, we learn about regression analysis and regression “models’.
This week, we learn about regression analysis and regression “models’. Discuss the role of regression analysis in business by using examples of how these models might work to make predictions. In your discussion, consider the various components of the output and how it might be of value to understanding the data.
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O:...
5) File I/O A) Compare and contrast InputStream/OutputStream based file I/O to Scanner/Printwriter based file I/O: B) Discuss Serialization, what is it? What are the processes and features? What is the function of the keyword Transient?
From the reading this week, you will learn about the basic model that we use to...
From the reading this week, you will learn about the basic model that we use to explain the entire economy, aggregate demand-aggregate supply. All of the macroeconomic material is related to this model: GDP, inflation, unemployment. In chapter 12, you’ll read about how fiscal policy can affect the economy. The fiscal policy tools that you read about in this chapter are meant to adjust the aggregate demand side of the economy. But there is a debate about whether this is...
Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3....
Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3. a. The class must be named LinkedList. b. The class must provide the methods listed above for constructing, accessing, and manipulating LinkedList objects for a generic type. c. Other than for testing purposes, the LinkedList class should do no input or output. d. The package must enable the provided tests. 2. Test this class using JUnit. a. A suite of JUnit tests have been...
Java Programming Project 6: File I/O Purpose: To practice reading from as well as writing to...
Java Programming Project 6: File I/O Purpose: To practice reading from as well as writing to text files with the help of Scanner class methods, and PrintStream class methods. You will also learn to implement some simple Exception Handling. Carefully examine and follow ALL the program specifications. Take a look at the PPT slides for Chapter 7 File I/O for examples that will help with this program. Hotel Expense Recording Keeping: A hotel bookkeeper enters client hotel expenses in a...
Question 1: As an Agile Professional, I want to learn the purpose of Iteration, so that I...
Question 1: As an Agile Professional, I want to learn the purpose of Iteration, so that I can help prepare my team to begin building our product.  Question 2:  As an Agile Professional, I want to learn the practice of requirements elaboration so that I can better specify acceptance criteria for stories.  Question 3: As an Agile Professional, I want to learn the practice of backlog grooming so that I can help my team to maintain and prioritize our story backlog.  Question 4:...
In our chapter reading this week, we learn more about setting up a sample company in...
In our chapter reading this week, we learn more about setting up a sample company in QuickBooks Online. We explore how to customize a chart of accounts, explore lists, and navigate the various areas for customers, vendors and employees. Which feature do you like most so far in your navigation of QuickBooks Online?
This week we learn about the Statement of Cash Flows (Chapter 14). This week's discussion questions...
This week we learn about the Statement of Cash Flows (Chapter 14). This week's discussion questions are as follows: 1). What is the reporting purpose of the statement of cash flows? Why is this important to investors? 2). Summarize the three types of cash flow activities, and describe at least 2 types of cash flows in EACH type of activity 3). Is depreciation a source of cash flow? Why or why not?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT