Question
help!
Run the FileClient with the debugger (with a breakpoint on the line with while). Youll need to create a file called input.tx
0 0
Add a comment Improve this question Transcribed image text
Answer #1

First, let us analyze the code line by line and how can we improve it.

1.import java.io.File;
2.import java.util.Scanner;
3.class FileClient{
4.public static void main(String[]args) throws java.io.FileNotFoundException{
5.File inputFile=new File("src/input.txt");
6.Scanner inputFileScanner=new Scanner(inputFile);
7.while(inputFileScanner.hasNext()){
8.System.out.println(inputFileScanner.next());
}
}
}

Line 1 and 2 are used to import the library files in the program.

Line 3 has a definition of the main class.

Line 4 has the main method also , this program has an external dependency for a file. That is why we are specifying that if the file is not found or can't get accessed we should throw an exception and telling the java compiler to catch the exception.

Line no 5 is initializing the file to be read or written.

Line 6 is creating an instance of the Scanner class which takes the argument of a file object.

Line 7 is the while loop which we will discuss later.

//To write something in the file, there are a lot of ways, here I am mentioning one way..//

String content = "Some random content";

FileOutputStream writter = new FileOutputStream("src/input.txt");
DataOutputStream dataWritter= new DataOutputStream(new BufferedOutputStream(writter));
dataWritter.writeUTF(content);
  
dataOutStream.close();

  • If we add this code in the above program we get...

package assing;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileClient {
public static void main(String[]args) throws java.io.FileNotFoundException{
File inputFile=new File("src/input.txt");
Scanner inputFileScanner=new Scanner(inputFile);
String content = "Some random content";
FileOutputStream writter = new FileOutputStream("src/input.txt");
DataOutputStream dataWritter= new DataOutputStream(new BufferedOutputStream(writter));
try {
   dataWritter.writeUTF(content);
} catch (IOException e) {

   e.printStackTrace();
}
try {
   dataWritter.close();
} catch (IOException e) {

   e.printStackTrace();
}
while(inputFileScanner.hasNext()){
try {
   System.out.println(inputFileScanner.next());
} catch (IOException e) {

   e.printStackTrace();
}
}
}
}

//now let's look at the questions//

  • The scanner's hasNext() method is contained in the Java.util.Scanner class.The return value of the method is boolean.The function has two type of parameters.One we can pass a radix value.,in that case the method acts accordingly.In the other case the method does not takes any parameter as input.The working is as follow:

The method checks each contigious location for value other than null.Until null is found the method iterates until null is found.After each iteration it return value as true ,and when null is found ,return value is false.The method throws IllegalStateException .

  • Basically, what a while loop does is that it loops or executes the set of commands within the loop brackets until the loop condition is false. The while function takes an argument, the loop is fully dependent on the argument.

In the above program, the hasNext() function always returns either true or false , basically a boolean value. The while loop continues to run until the value of the argument becomes false . In this case, until the file becomes empty, the while loop runs and prints the content of the file line after line in a separate line , until the file is fully read.

//Here is another example of the while loop//

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class FileClient {
public static void main(String[]args) throws java.io.FileNotFoundException{
File inputFile=new File("src/input.txt");
Scanner inputFileScanner=new Scanner(inputFile);
String content = "Some random content";
FileOutputStream writter = new FileOutputStream("src/input.txt");
DataOutputStream dataWritter= new DataOutputStream(new BufferedOutputStream(writter));
try {
   dataWritter.writeUTF(content);
} catch (IOException e) {

   e.printStackTrace();
}
try {
   dataWritter.close();
} catch (IOException e) {

   e.printStackTrace();
}
while(inputFileScanner.hasNext()){
try {
   dataWritter.flush();
} catch (IOException e) {

   e.printStackTrace();
}
}
}
}

//Do comment for any queries......//

Add a comment
Know the answer?
Add Answer to:
help! Run the FileClient with the debugger (with a breakpoint on the line with while). You'll...
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
  • 1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into...

    1. Import file ReadingData.zip into NetBeans. Also, please download the input.txt file and store it into an appropriate folder in your drive. a) Go through the codes then run the file and write the output with screenshot (please make sure that you change the location of the file) (20 points) b) Write additional Java code that will show the average of all numbers in input.txt file (10 points) import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; import java.io.*; 1- * @author mdkabir...

  • Problem: Use the code I have provided to start writing a program that accepts a large...

    Problem: Use the code I have provided to start writing a program that accepts a large file as input (given to you) and takes all of these numbers and enters them into an array. Once all of the numbers are in your array your job is to sort them. You must use either the insertion or selection sort to accomplish this. Input: Each line of input will be one item to be added to your array. Output: Your output will...

  • Write a Java method that will take an array of integers of size n and shift...

    Write a Java method that will take an array of integers of size n and shift right by m places, where n > m. You should read your inputs from a file and write your outputs to another file. Create at least 3 test cases and report their output. I've created a program to read and write to separate files but i don't know how to store the numbers from the input file into an array and then store the...

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

  • Help check why the exception exist do some change but be sure to use the printwriter...

    Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...

  • import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {              ...

    import java.util.Scanner; import java.io.File; public class Exception2 {        public static void main(String[] args) {               int total = 0;               int num = 0;               File myFile = null;               Scanner inputFile = null;               myFile = new File("inFile.txt");               inputFile = new Scanner(myFile);               while (inputFile.hasNext()) {                      num = inputFile.nextInt();                      total += num;               }               System.out.println("The total value is " + total);        } } /* In the first program, the Scanner may throw an...

  • See notes in Lab4T1SHYPI.java Use the program Lab4T1SHYPL.java to answer the following questions. Part 1: Create...

    See notes in Lab4T1SHYPI.java Use the program Lab4T1SHYPL.java to answer the following questions. Part 1: Create a new program XYZ1234Lab4T1Part1.java and copy the body from Lab4T1SHYPIjava into the new program. In this new program, make the (29 points total a 1.a) Replace the sets of variables with arrays for each kind of data. Ex. shPubl, shPub2, shPub3, and shPub4 should be replaced with a single array StringIl shPub. Replace the variables for superHeros, shYear, shIssue, and shMovi Arrays should hold...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

  • I keep getting the compilation error: pass the filename from command line arguement... Can you please...

    I keep getting the compilation error: pass the filename from command line arguement... Can you please tell me what im doing wrong? here is the code: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ShipDemo { public static void main(String[] args) { String companyName; int num_ships, total_passengers=0, total_tonnage=0; Ship[] shipInventory; try { if(args.length == 1) { File file = new File(args[0]); Scanner fileScan = new Scanner(file); companyName = fileScan.nextLine(); num_ships = fileScan.nextInt(); shipInventory = new Ship[num_ships]; int n=0; String type,name;...

  • *JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before...

    *JAVA Prompt the user for a movie name. Output the three movies that come alphabetically before the one entered. I need help correcting my code. I'm unsure why I am getting a name, year, and genre in my output. Input: Jericho Output: Similar title finder. Enter a movie name.\n Here are the 3 movies that are listed before the one you entered\n Jeremy Paxman Interviews...\n Jeremy Taylor\n Jeremy Vine Meets...\n Current output: Similar title finder. Enter a movie name.\n Here...

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