Question: Hey guys, I need some help on my code for java. I was thinking about using while or for loop but im stuck. Anyways here

Hey guys,

I need some help on my code for java. I was thinking about using while or for loop but im stuck. Anyways here are the instructions:

Hey guys, I need some help on my code for java. I

here is the text from the text file he wants us to use (data.txt) :

computer software hardware secondary primary storage I/O Array analog bandwidths bit security ethernet

And here is the code i have so far:

import java.io.FileNotFoundException; import java.io.FileReader; import java.io.PrintWriter; import java.util.Scanner; public class LongestWordTester { public static void main(String[] args) throws FileNotFoundException { String longestWord=""; String current; try{ Scanner console= new Scanner(System.in); System.out.print("input file: "); String inputFile= console.next(); FileReader text = new FileReader(inputFile); Scanner in =new Scanner(text); while(in.hasNext()) { current=in.next(); if(current.length()>longestWord.length()) { longestWord=current; } } catch(FileNotFoundException e); } System.out.println("longest word is " + longestWord); Scanner out = new Scanner(System.in); System.out.println("Enter file name:"); String fileName= out.next(); PrintWriter writer = new PrintWriter(fileName); writer.print(longestWord); writer.close(); System.out.println("The longest word is now written in your file"); } }

Learning Objectives After completion of this lab, you should be able to: 1. Read from & write to text files. Write a Java program to find the longest word in a text file. Create a main class LongestWordTester: 1. Ask user for file name to read from and to write to. Find the longest word in the input file. Write the longest word in the output file. 2. If they enter the wrong input filename: Let them know Give them 2 more chances before letting the program end. 3. Write the longest word in an output file. i. Prompt user for output file name

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!