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

Java Code:

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

public class Main {

   public static void main(String[] args) throws FileNotFoundException {
       //creating a scanner
       Scanner sc = new Scanner(new File("contacts.txt"));
       //while there is lines in the file
       while(sc.hasNext()) {
           //get the line
           String line = sc.nextLine();
           //print the part of the line that has the name
           System.out.println(line.substring(0, line.length()-9));
       }

   }

}

Sample Output:

Joe Doe
Test1
Test2
Test3
Test4 LastName
Test5
Test6
Test7
Test8
Test9

contacts.txt

Joe Doe 123456789
Test1 546738219
Test2 673821123
Test3 821123456
Test4 LastName 123456175
Test5 456175895
Test6 175895164
Test7 895164841
Test8 164841724
Test9 841724473


Related Solutions

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...
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?
This week, we learn all about physical environments for infants and toddlers. What are some challenges...
This week, we learn all about physical environments for infants and toddlers. What are some challenges that can arise when designing a developmentally appropriate space for infants and toddlers? How might caregivers overcome such obstacles?  What advice can you offer your peers via Discussion Board this week?
I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1....
I NEED IT PRINT IN THIS FORM JAVA How is a Java source file executed? 1. It is compiled and runs natively 2. It is interpreted but not compiled 3. It is compiled into byte code and executed by the Java Virtual Machine 4. It is magic Please enter your answer 1 Sorry that answer is not correct. The correct answer is 3 Correct output: How is a Java source file executed? 1. It is compiled and runs natively 2....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT