Question

*JAVA File Input/Output Homework* (4 parts)

Part 1 1) Open a TEXT editor program 2) Type in 50 words, one word per line, all words of the SAME catagory, max chars per wo

int r = 0; string temp; for (int x = 0; x < 50; x++) r = random..... number.... // code to get a random number between 1..49

0 0
Add a comment Improve this question Transcribed image text
Answer #1
 import java.util.*; //import util library import java.io.*; //import io library public class solution{ //file name is solution.java public static void main(String[] args) throws Exception //main class { File file =new File("myWordFile.txt"); //read the text file containg 50 flowers name List<String> list = new ArrayList<String>(); //create a array list of string BufferedReader br=new BufferedReader(new FileReader(file));//using buffer reader to read file String st; while((st=br.readLine())!=null)//read all line using while loop { list.add(st); //add all string to list } String[] myWordArray = list.toArray(new String[list.size()]); //convert list to array of string String temp; Random r = new Random(); // Random number generator for (int i=0; i<50; i++) //run loop for 50 words in file { int randomPosition = r.nextInt(50); //generate random number upto 50 temp = myWordArray[i]; //swap the current index with random number index myWordArray[i] = myWordArray[randomPosition]; myWordArray[randomPosition] = temp; //swap with all index in array } for(String s : myWordArray)// run loop till all elements of array using for each loop { System.out.println(s);//print the elements of array } } }
Text file:myWordFile.txt Daisy Rose Iris Narcis Orchid Tulip Sunflower Cyclamen Carnation Poppy Pansy Violet Mimosa Daffodil Lily Hyacinth Anemone Gladiolus Forgetnot Bluebell Bougain Buttercup Cactus Camellia Chrysan Comb Confeder Crocus Dahlia Eglantine Flamboy Foxglove Geranium Gerbera Hibiscus Honey Hop Jasmine Lavender Lilac Lotus Magnolia Marigold Morning Orange Peony Primrose Snapdrag Snowdrop Tuberose 

import java.util.*; //import util library import java.io.*; //import io library public class solution{ //file name is solutiomyWordFile - Notepad File Edit Format View Help Narcis Orchid Tulip Sunflower Cyclamen Carnation Poppy Pansy Violet Mimosa DaC. Command Prompt Honeysuckle Rose Buttercup Daffodil Primrose Bluebell Snapdragon Confederate rose Eglantine Bougainvillea I

//Please upvote :) it will motivate !!

Add a comment
Know the answer?
Add Answer to:
*JAVA File Input/Output Homework* (4 parts) Part 1 1) Open a TEXT editor program 2) Type...
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 writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

  • MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random...

    MUST BE PROCEDURAL CODE, DO NOT USE GLOBAL VARIABLES. Write a program in C++that generates random words from a training set as explained in the lectures on graphs. Do not hard-code the training set! Read it from a file. A suggested format for the input file: 6 a e m r s t 10 ate eat mate meet rate seat stream tame team tear Here are some suggestions for constants, array declarations, and helper functions #include <iostream> #include <fstream> #include...

  • python Create a program to open a text file for reading, find the maximum number in...

    python Create a program to open a text file for reading, find the maximum number in the file, determine if that maximum number is even, and write to an output text file. You should either write Yes if the number is even, otherwise write the maximum number. You should note the following: • Your input file must be named input.txt • The input file has one integer number per line • Your output file must be named output.txt • Your...

  • looking for in Java Write the method loadArray. The method will declare and open a file...

    looking for in Java Write the method loadArray. The method will declare and open a file stream and connect it to the external file passed in. It will then read patient names into the patients array and their temperatures into the temps array. The method should compute/determine the number of patients in the file. Make sure you do all steps of file VO] A sample file, and some declarations and a sample function call are given Sample File Mickey 102.5...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • In java, write a program with a recursive method which asks the user for a text...

    In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...

  • This lab will combine reading data from a file and searching the array to find a...

    This lab will combine reading data from a file and searching the array to find a specific value. Assignment Write a program that reads in a file full of strings into an array, and prompts the user for a string to find in the array. The program should loop until a sentinel value (such as -1) is entered. After looping in main() for the input, write a search function, with the following prototype: int findWord(string [], int, string); with arguments...

  • JAVA Write a program which will read a text file into an ArrayList of Strings. Note...

    JAVA Write a program which will read a text file into an ArrayList of Strings. Note that the given data file (i.e., “sortedStrings.txt”) contains the words already sorted for your convenience. • Read a search key word (i.e., string) from the keyboard and use sequential and binary searches to check to see if the string is present as the instance of ArraryList. • Refer to “SearchInt.java” (given in “SearchString.zip”) and the following UML diagram for the details of required program...

  • (C++) This is the part 2 of the program. In this program you will use part...

    (C++) This is the part 2 of the program. In this program you will use part 1 and build on it to call getAnswer function which would return a random answer. Remember that you had stored these random answers in a string array. You will pass the array and the possible answers (ideally the size of the array) to getAnswer, and the getAnswer will return a randomly selected answer back to main. This is the starting code (below). Please make...

  • Open a new file in your text editor, and start a class that will demonstrate a...

    Open a new file in your text editor, and start a class that will demonstrate a working two-dimensional array: import java.util.Scanner; class TwoDimensionalArrayDemo { public static void main(String[] args) { 2. Declare a three-by-three array of integers. By default, the elements will all be initialized to 0. int[][] count = new int[3][3]; 3. Declare a Scanner object for input, variables to hold a row and column, and a constant that can be used to indicate when the user wants to...

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