Question

Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

Question 1[JAVA]

Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents.

Note, the contents of input.txt will be different (including the number of lines) for each test case.

Example:

getLines( "input.txt" ) returns (an array of Strings with 3 elements): {"Two roads diverged in a wood and I", "I took the one less traveled by", "And that has made all the difference"}

public class Main{

public static void main( String[] args ){




}
}

Question 2 [JAVA]

Write a complete Java program that writes "Hello, world" to a file named "hello.txt".

Note, this means you need to use main().

class Main {
public static void main(String[] args) {
  
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

2)

import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.io.*;
public class Main
{
   public static void main(String[] args) {
       System.out.println("Hello World");
       try {
PrintStream out = new PrintStream(new FileOutputStream(
"hello.txt"));
out.println("Hello, world");

out.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
}
   }
}

M Inbox - gurkaranpreet.singh.me x C Question 1[JAVA] Add A Method X Online Java Compiler - online ed x C My Q&A | Chegg.com

Note: Brother According to HomeworkLib's policy we are only allowed to answer 1 Q if there are many. So, I request you to post other part as separate posts

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...
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
  • Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source...

    Filename(s): ReformatCode. java Public class: ReformatCode Package-visible class(es): none Write a program that reformats Java source code from the next-line brace style to the end-of-line brace style. The program is invoked from the command line with the input Java source code file as args [0] and the name of the file to save the formatted code in as args [1]. The original file is left untouched. The program makes no other changes the source code, including whitespace. For example, the...

  • QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes...

    QUESTION The ReadFile class opens and reads a text file. The WriteFile class opens and writes to a file. Compile and run these programs. There are several ways to read/write text files. The examples shown here are just one way of achieving this. Look at the API for the BufferedReader class. The readline() method is a simple way to read the text file line by line. It returns null when the end of the file has been reached. https://docs.oracle.com/javase/8/docs/api/java/io/BufferedReader.html Look...

  • COMPILER ERROR PLS HELP CAPITALIZE FIRST LETTER OF EACH WORD IN STRING IN JAVA public class...

    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); } } }

  • java question: Write a program that reads in data from a text file named in.txt. Compute...

    java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For...

  • Java : Please help me correct my code: create a single class (Program11.java) with a main...

    Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...

  • This is a java assignment Please help me Write the body of the fileAverage() method. Have...

    This is a java assignment Please help me Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place). For the testing system to work, don't change the class name nor the method name. Additionally, the file will have different contents during testing. public class Main { public double fileAverage( String filename ){    }...

  • Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values...

    Java 1. Create an application named NumbersDemo whose main() method holds two integer variables. Assign values to the variables. In turn, pass each value to methods named displayTwiceTheNumber(), displayNumberPlusFive(), and displayNumberSquared(). Create each method to perform the task its name implies. 2. Modify the NumbersDemo class to accept the values of the two integers from a user at the keyboard. This is the base code given: public class NumbersDemo { public static void main (String args[]) { // Write your...

  • Java Project Draw a class diagram for the below class (using UML notation) import java.io.BufferedReader; import...

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

  • Is there any way that I can call method in other class without constructor in java?...

    Is there any way that I can call method in other class without constructor in java? public class Chest { private String contents; public String getContents()    {        return contents;    } public void setContents(String contents)    {               this.contents = contents;           } I need to set content and print out in the main method, but it keep gives me a error message when I try Chest.setContents("Gold"); on main class. I suppose...

  • create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines....

    create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the method, check if the...

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