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....

  • 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 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...

  • 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...

  • 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...

  • 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...

  • We use bluej for our JAVA class. If you can help me id greatly appreciate it....

    We use bluej for our 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 use...

  • JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods...

    JAVA HELP (ARRAYS) Assignment Create a class named Module4 containing the following data members and methods (note that all data members and methods are for objects unless specified as being for the entire class) 1) Data members: none 2) Methods a. A method named displayContents that accepts an array of type int and prints their values to the b. A method named cemoveNegatives that accepts an array of type int, copies all its non-negative the new array should be the...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

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