Question

I need help with my assignment. It is a java program. Please make it as simple...

I need help with my assignment. It is a java program. Please make it as simple as you can. Create an ArrayList with elements the objects of previous class. Using a simple loop populate the ArrayList with data from some arrays with data, or from console. Use a loop with iterator and write the information in a File using PrintWriter. Use this address for the file (c:\\result\\MyData.txt). Use the Scanner class to display the content of the file on console.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi, thanks for posting the question.

I have show as how you can write to a file using PrintWriter and use Scanner to read from a file and print the contents in the console.

Refer the screenshot given at the end :)

_____________________________________________________________________________________________

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

public class WriteAndRead {

    //from some arrays with data
    private static String[] cities = {"Washington", "New York", "California",
            "Denver", "Paris", "Log Angeles"};

    public static void main(String[] args) {
        //Create an ArrayList with elements the objects
        ArrayList<String> cityArrayList = new ArrayList<>();
        //Using a simple loop populate the ArrayList with data
        for (String city : cities) {
            cityArrayList.add(city);
        }
        //Use this address for the file (c:\result\MyData.txt).
        File file = new File("F:\result\MyData.txt");
        try {
            //Use a loop with iterator and write the information in a
            // File using PrintWriter.
            PrintWriter writer = new PrintWriter(file);
            for (String city : cityArrayList) {

                writer.write(city + "
");
            }
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        try {
            //Use the Scanner class to display the content of the file on console.
            Scanner scanner = new Scanner(file);
            while (scanner.hasNextLine()) {
                System.out.println(scanner.nextLine());
            }

            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


}

_____________________________________________________________________________________________

or (string city: cityarrayList 27 28 29 30 31 32 writer.rite( cityn) writer.close ) catch (FileNotFoundException e) e.print

Thank you, incase you have any problem do let me know !!

Add a comment
Know the answer?
Add Answer to:
I need help with my assignment. It is a java program. Please make it as simple...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with my java homework Let have the array: String[] names= {"Tom", "David","Joel","Steven","Dora"}; declare...

    I need help with my java homework Let have the array: String[] names= {"Tom", "David","Joel","Steven","Dora"}; declare a collection ArrayList and populate it with these names using a simple loop process the ArrayList associating to each name the word student, using for each loop use iterator to display the information

  • Please code this in java: The purpose of this assignment is to: Read double data in...

    Please code this in java: The purpose of this assignment is to: Read double data in to a TreeSet where each datum inserted is Math.abs(datum) (i.e., no negative values are inserted). NOTE: This will (i) sort the data placed into the TreeSet, and, (ii) remove duplicate values since no duplicates are allowed (i.e., duplicates will not be inserted). Create a list iterator. Iterate backwards through the TreeSet (i.e., from the largest value to the smallest) outputting all elements * -1.0....

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • JAVA Which of these statements does not match by using exception action in Java? 1. Exception...

    JAVA Which of these statements does not match by using exception action in Java? 1. Exception action makes it possible for the calling method to handle errors in called methods.    2. Exception action simplifies programming since incorrect reporting and handling can be located in catch blocks separately from the rest of the code.    3. Exception action increases the performance of programs. 4. Java separates exception action from common processes. Why create instances of File Class? - several alternatives...

  • Write a Java program in Eclipse that reads from a file, does some clean up, and...

    Write a Java program in Eclipse that reads from a file, does some clean up, and then writes the “cleaned” data to an output file. Create a class called FoodItem. This class should have the following: A field for the item’s description. A field for the item’s price. A field for the item’s expiration date. A constructor to initialize the item’s fields to specified values. Getters (Accessors) for each field. This class should implement the Comparable interface so that food...

  • ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes,...

    ONLY NEED THE DRIVER CLASS PLEASE!!! Domain & Comparator Classes: 0.) Design a hierarchy of classes, where the Media superclass has the artistName and the mediaName as the common attributes. Create a subclass called CDMedia that has the additional arrayList of String objects containing the songs per album. Create another subclass called DVDMedia that has the additional year the movie came out, and an arrayList of co-stars for the movie. 1.) The superclass Media will implement Comparable, and will define...

  • We are using blueJ for my java class, if you can help me id greatly appreciate...

    We are using blueJ for my java class, if you can help me id greatly appreciate it. Create a new Java class called AverageWeight. Create two arrays: an array to hold 3 different names and an array to hold their weights. Use Scanner to prompt for their name and weight and store in correct array. Compute and print the average of the weights entered using printf command. Use a loop to traverse and print the elements of each array or...

  • Java Task 2: Write a program where you create an ArrayList object that can store letters....

    Java Task 2: Write a program where you create an ArrayList object that can store letters. Show how can you add a letter to the ArrayList, remove a letter from theArrayList, replace a letter in the ArrayList, insert a letter in a specific location in the ArrayList, and display all the letters in the ArrayList. Task 3: Write a program where you create an ArrayList object that can store Circle object (you created circle class in your previous PAs). Create...

  • Write a program to create a file named integerFile.txt if it does not exist. Write 100...

    Write a program to create a file named integerFile.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. The random integers should be in the range 0 to 100 (including 0 and 100). Integers should be separated by spaces in the file. Read the data back from the file and display the data in increasing order This is what I have so far: import java.io.File; import java.util.Scanner; import java.io.PrintWriter; public class integerFile {...

  • Write a c# console program called “ArrayOfThings” where you create a class called Staff with private...

    Write a c# console program called “ArrayOfThings” where you create a class called Staff with private fields for StaffID and StaffName. Create Properties that can write data and retrieve data from these private fields. Create a Staff array that can hold up to ten Staff objects. Create ten objects using a for loop that automatically creates a new (empty) Staff object at each iteration of the loop and adds that object into the Staff array. Write data into the first...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT