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 possible
1. To find properties of the file.
2. To delete the file
3. To read / write the content from / to the file.
4. To find out if the file exists. To rename the file.
Which of the following are correct?
I:
try (PrintWriter output = new PrintWriter("output.txt")) {
output.println("Welcome to Java");
}
II:
try (PrintWriter output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
III:
PrintWriter output;
try (output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
IV:
try (PrintWriter output = new PrintWriter("output.txt");) {
output.println("Welcome to Java");
}
finally {
output.close();
}
1. IV
2. I
3. II
4. III
Which of the following are correct?
I:
File file = new File("input.txt");
try (Scanner input = new Scanner(file)) {
String line = input.nextLine();
}
II:
try (File file = new File("input.txt");
Scanner input = new Scanner(file);) {
String line = input.nextLine();
}
III:
File file;
try (file = new File("input.txt");
Scanner input = new Scanner(file);) {
String line = input.nextLine();
}
IV:
File file;
Scanner input;
try (file = new File("input.txt");
input = new Scanner(file);) {
String line = input.nextLine();
}
1. III
2. II
3. I
4. IV
Suppose you have written 34.3 57.8 789 to the console and then pressed the Enter / Return button. Analyze the following example:
Scanner input = new Scanner(System.in); double v1 = input.nextDouble(); double v2 = input.nextDouble(); String line = input.nextLine();
1. The content of the line will be: '7', '8', '9'.
2. The content of the line will be: '', '7', '8', '9'.
3. The content of the line will be: '', '7', '8', '9', 'n'.
4 .The content of the line will be: '7', '8', '9', 'n'.
Which of the following can be used to write to temp.txt? - several alternatives possible
1. new PrintWriter("temp.txt")
2. new PrintWriter(temp.txt)
3. new PrintWriter(new File("temp.txt"))
4 .new PrintWriter(File("temp.txt"))
To create an InputStream for reading a file from a web server, you can use any of the following methods in the URL class.
1. openStream()
2. getInputStream()
3. connectStream()
4. obtainInputStream()
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Question 2:
Answer :
1. To find properties of the file.
2. To delete the file
4. To find out if the file exists. To rename the file.
Explanation :File instance is created to find file properties such as getName(),getPath() etc.exists() method is used to check whether that method exists or not. etc.
*****************************
Question 3:
Answer :2. I
Explanation :
try (PrintWriter output = new PrintWriter("output.txt")) {
output.println("Welcome to Java");
}
*****************************
Question 4:
Answer :3. I
Explanation :
I:
File file = new File("input.txt");
try (Scanner input = new Scanner(file)) {
String line = input.nextLine();
}
*******************************
Question 6:
Answer :3. new PrintWriter(new File("temp.txt"))
*****************************
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.
JAVA Which of these statements does not match by using exception action in Java? 1. Exception...
(Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...
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...
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
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...
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 {...
Modify your program in Lab Assignment 4A to throw an exception if the file does not exist. An error message should result. Use the same file name in your program as 4A, however, use the new input file in 4B assignment dropbox. My code: import java.io.*; import java.util.*; public class Lab4B { public static void main(String[] args) throws IOException { final String input_file = "Lab_4A.txt"; final String output_file = "Lab_4A.txt"; Scanner x = null; FileWriter y = null; try {...
In java write a simple 1-room chat server that is compatible
with the given client code.
9 public class Client private static String addr; private static int port; private static String username; 14 private static iter> currenthriter new AtomicReference>(null); public static void main(String[] args) throws Exception [ addr -args[]; port Integer.parseInt (args[1]); username-args[21 Thread keyboardHandler new Thread(Main: handlekeyboardInput); 18 19 while (true) [ try (Socket socket -new Socket (addr, port) println(" CONNECTED!; Printwriter writer new Printwriter(socket.getoutputStreamO); writer.println(username); writer.flush); currenthriter.set(writer); BufferedReader...
Java Your boss has just put you in charge of updating the company's client contact list. This file contains records in the following format: id,first_name,last_name,email 1,Norry,Killby,nkillby0@photobucket.com Your job is to reformat each record as follows: last_name, first_name, email Killby, Norry, <nkillby0@photobucket.com> Starter Code: package contactlistupdater; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Paths; import java.util.Formatter; import java.util.Scanner; import sun.reflect.generics.reflectiveObjects.NotImplementedException; public class ContactListUpdater { private static final String INPUT_FILENAME = "contacts.txt"; private static final String OUTPUT_FILENAME = "updated-contacts.txt"; /** * Transforms a String...
How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...
How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner { public static void main(String[] args) { /* For Homework! Tokens*/ Scanner file = null; PrintWriter fout= null; ...