public class File {
public String base; //for example, "log" in
"log.txt"
public String extension; //for example, "txt" in
"log.txt"
public int size; //in bytes
public int permissions; //explanation in toString
public String getExtension() {
return extension;
}
public void setExtension(String e) {
if(e == null || e.length() ==
0)
extension =
"txt";
else
extension =
e;
}
public class FileCollection {
private File[] files;
/**
* DO NOT MODIFY
* Loads collection from input file
* @param input: name of input file
* @throws IOException
*/
public FileCollection(String input) throws IOException
{
FileInputStream inputStream1 = new
FileInputStream(input);
BufferedReader bufferedReader1 =
new BufferedReader(new InputStreamReader(inputStream1));
int count = 0;
while (bufferedReader1.readLine()
!= null) {
count++;
}
bufferedReader1.close();
FileInputStream inputStream2 =
new FileInputStream(input);
BufferedReader bufferedReader2 =
new BufferedReader(new InputStreamReader(inputStream2));
files = new File[count];
String line = null;
for (int i = 0; i < count; i++)
{
line =
bufferedReader2.readLine();
String tokens[]
= line.split(",");
String base =
tokens[0];
String ext =
tokens[1];
int size =
Integer.parseInt(tokens[2]);
int perm =
Integer.parseInt(tokens[3]);
files[i] = new
File(base, ext, size, perm);
}
bufferedReader2.close();
}
/**
* DO NOT MODIFY
* @param files
*/
public FileCollection(File[] files) {
this.files = files;
}
/*
* @param extension
* @return number of files with the given
extension
* (case insensitive)
* NOTE: don't compare Strings using ==
* Google "String case insensitive comparison
java"
*/
public int getCountByExtension(String extension)
{
return 0; //to be completed
}
@Test
void testGetCountByExtension() throws IOException
{
assertEquals(4,
c1.getCountByExtension("py"));
assertEquals(4,
c1.getCountByExtension("PY")); //case insensitive
assertEquals(0,
c1.getCountByExtension("dll"));
assertEquals(1,
c1.getCountByExtension("docx"));
score+=10;
}
public class File {
public String base; //for example, "log" in "log.txt"
public String extension; //for example, "txt" in "log.txt"
public int size; //in bytes
public int permissions; //explanation in toString
public String getExtension() {
return extension;
}
public void setExtension(String e) {
if(e == null || e.length() == 0)
extension = "txt";
else
extension = e;
}
}
public class FileCollection {
private File[] files;
/**
* DO NOT MODIFY
* Loads collection from input file
* @param input: name of input file
* @throws IOException
*/
public FileCollection(String input) throws IOException {
FileInputStream inputStream1 = new FileInputStream(input);
BufferedReader bufferedReader1 = new BufferedReader(new
InputStreamReader(inputStream1));
int count = 0;
while (bufferedReader1.readLine() != null) {
count++;
}
bufferedReader1.close();
FileInputStream inputStream2 = new FileInputStream(input);
BufferedReader bufferedReader2 = new BufferedReader(new
InputStreamReader(inputStream2));
files = new File[count];
String line = null;
for (int i = 0; i < count; i++) {
line = bufferedReader2.readLine();
String tokens[] = line.split(",");
String base = tokens[0];
String ext = tokens[1];
int size = Integer.parseInt(tokens[2]);
int perm = Integer.parseInt(tokens[3]);
files[i] = new File(base, ext, size, perm);
}
bufferedReader2.close();
}
/**
* DO NOT MODIFY
* @param files
*/
public FileCollection(File[] files) {
this.files = files;
}
/*
* @param extension
* @return number of files with the given extension
* (case insensitive)
* NOTE: don't compare Strings using ==
* Google "String case insensitive comparison java"
*/
public int getCountByExtension(String extension) {
int count = 0;
// loop through the files
array
for(int
i=0;i>files.length;i++)
// check if
extension is same, then increment the count
if(files[i].getExtension().equalsIgnoreCase(extension))
count++;
return count;
}
@Test
void testGetCountByExtension() throws IOException {
assertEquals(4, c1.getCountByExtension("py"));
assertEquals(4, c1.getCountByExtension("PY")); //case
insensitive
assertEquals(0, c1.getCountByExtension("dll"));
assertEquals(1, c1.getCountByExtension("docx"));
score+=10;
}
}
Public class File { public String base; //for example, "log" in "log.txt" &nbs...
COMPILER ERROR PLS HELP CAPITALIZE FIRST LETTER OF EACH WORD IN STRING IN JAVA public class Main { /** * Iterate through each line of input. */ public static void main(String[] args) throws IOException { InputStreamReader reader = new InputStreamReader(System.in, StandardCharsets.UTF_8); BufferedReader in = new BufferedReader(reader); String line; while ((line = in.readLine()) != null) { line = Character.toUpperCase(line.charAt(0)) + line.subtring(1) + (" "); System.out.println(line); } } }
Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...
package Lab11; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Lab10 { public static void main (String [] args) { // ============================================================ // Step 2. Declaring Variables You Need // These constants are used to define 2D array and loop conditions final int NUM_ROWS = 4; final int NUM_COLS = 3; String filename = "Input.txt"; // A String variable used to save the lines read from input...
Using java socket programming rewrite the following program to handle multiple clients simultaneously (multi threaded programming) import java.io.*; import java.net.*; public class WelcomeClient { public static void main(String[] args) throws IOException { if (args.length != 2) { System.err.println( "Usage: java EchoClient <host name> <port number>"); System.exit(1); } String hostName = args[0]; int portNumber = Integer.parseInt(args[1]); try ( Socket kkSocket = new Socket(hostName, portNumber); PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true); BufferedReader in = new BufferedReader( new InputStreamReader(kkSocket.getInputStream())); ) { BufferedReader...
How to solve this problem by using reverse String and Binary search? Read the input one line at a time and output the current line if and only if it is not a suffix of some previous line. For example, if the some line is "0xdeadbeef" and some subsequent line is "beef", then the subsequent line should not be output. import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet;...
Java only. Thanks These questions involve choosing the right abstraction (Collection, Set, List, Queue, Deque, SortedSet, Map, or SortedMap) to EFFICIENTLY accomplish the task at hand. The best way to do these is to read the question and then think about what type of Collection is best to use to solve it. There are only a few lines of code you need to write to solve each of them. Unless specified otherwise, sorted order refers to the natural sorted order...
1. Copy the file secret.txt into a path that you can access. Read FilePath.doc if you have questions on file path. Copy SecretMessage.java into your NetBeans or other IDE tools. 2. Finish the main method that will read the file secret.txt, separate it into word tokens.You should process the tokens by taking the first letter of every fifth word, starting with the first word in the file. These letters should converted to capitals, then be appended to StringBuffer object to...
Can someone please help me? I'm having trouble with this assignment and have been stuck trying to figure it out for over an hour. Here's the assignment details below: For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text flie, sort and store the contents of the text file into an ArrayList, then write the sorted contents...
Having trouble with my code, it keeps giving me an error every time I run it. Can someone please help me understand what I'm doing wrong? *********CODE*************** // Java program to read the file contents, sort it and output the sorted content to another file import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; public class Datasort { public static void main(String[] args) throws IOException { File fin = new File("input.txt");...
Java Project
Draw a class diagram for the below class (using UML
notation)
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class myData {
public static void main(String[] args) {
String str;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter text (‘stop’ to quit).");
try (FileWriter fw = new FileWriter("test.txt")) {
do {
System.out.print(": ");
str = br.readLine();
if (str.compareTo("stop") == 0)
break;
str = str + "\r\n"; // add newline
fw.write(str);
} while (str.compareTo("stop") != 0);
} catch (IOException...