Question

In java language, use IntelliJ IDEA : 1. Write code that does the following: Opens a...

In java language, use IntelliJ IDEA :

1. Write code that does the following: Opens a file named numberList.txt, uses a for loop to write all the even numbers between 1 and 50 to the file.

2. Write a code that does the following: Opens a file named numberList.txt, uses a for loop to append all the odd numbers between 1 and 50 to the file without deleting the existing data.

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

Answer 1)

//java io package
import java.io.*;
import java.util.*;
//myClass is class for java program
public class myClass {
    public static void main(String args[]) throws IOException {
        File numF = new File("e:/output1.txt");
        //create a new file
        numF.createNewFile();
        //Writer is used to write integer values in the file
        Writer wr = new FileWriter("e:/output1.txt");
        for(int i=1;i<=50;i++)
            //check whether the number is even
            if (i % 2 == 0) {
                wr.write(i  + " ");
            }
        wr.close();


    }

}

Answer 2)

//java io package
import java.io.*;
import java.util.*;
public class myClass2 {
    public static void main(String args[]) throws IOException {
//FileWriter is used for append
        FileWriter fW = new FileWriter("e:/output1.txt", true);
        //PrintWriter is used to append values in the file
        PrintWriter wr1 = new PrintWriter(fW);
        for(int i=1;i<=50;i++)
            //check whether the number is odd
            if (i % 2 == 1) {
                wr1.print(i  + " ") ;

            }
        wr1.close();


    }
}

Add a comment
Know the answer?
Add Answer to:
In java language, use IntelliJ IDEA : 1. Write code that does the following: Opens a...
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
  • C++ need #7 and #8 6. Write code that does the following: Opens an output file...

    C++ need #7 and #8 6. Write code that does the following: Opens an output file with the filename Numbers.txt, uses a loop to write the numbers 1 through 100 to the file, and then closes the file. ofstream outputFile("Numbers.txt"); for(int number = 1; number <= 100; number++) outputFile<< number << endl; outputFile.close(); 7. Write code that does the following: Opens the Numbers.txt file that was created by the code you wrote in question 6, reads all of the numbers...

  • Write Java code that does the following Ask the user to enter a file name. Ask...

    Write Java code that does the following Ask the user to enter a file name. Ask the user for two integers as the lower bound and the upper bound. Use a loop to write all the odd numbers between the lower bound and the upper bound (inclusive) to the file, and then close the file. Use either while loop, do while loop, or for loop to complete the program. PrintWriter class is used. The file is closed before the program...

  • USING PYTHON PROGRAMMING LANGUAGE 15. Write code to open a file named data.txt, which contains 3...

    USING PYTHON PROGRAMMING LANGUAGE 15. Write code to open a file named data.txt, which contains 3 numbers, and print the sum of those numbers to the console 16. Write code that opens a file named words.txt containing an essay, and the prints out the SECOND word in the file to the console

  • Draw the flowchart of the code: Write code that opens an Excel file named area.xlsx, writes...

    Draw the flowchart of the code: Write code that opens an Excel file named area.xlsx, writes 'Radius' in cell A1, writes 'Area' in cell B1, and then using the while command populates the next 10 rows of column A (A2-A11) with the numbers 1, 2, ..., 10 and the next 10 rows of column B with the circle areas that correspond to the radii on the left. A while loop should be used.

  • Write a complete Java program that uses a for loop to compute the sum of the...

    Write a complete Java program that uses a for loop to compute the sum of the even numbers and the sum of the odd numbers between 1 and 25.

  • (java) Write a While loop that prompts the user for only even numbers. Keep prompting the...

    (java) Write a While loop that prompts the user for only even numbers. Keep prompting the user to enter even numbers until the user enters an odd number. After the loop ends, print the sum of the numbers. Do not include that last odd number. You should not store the numbers in an array, just keep track of the sum. Make sure to use a break statement in your code. You may assume that a java.util.Scanner object named input has...

  • in Java and also follow rubric please 4. Write a complete program to do the following:...

    in Java and also follow rubric please 4. Write a complete program to do the following: Using an input and output files, write a program that will read 20 numbers from an input file called InFile. Sum all even numbers and multiply all odd numbers. Print the numbers read from the input file to an output file called OutFile. Also print the sum of the even numbers and the product of the odd numbers to the output file. Rubric: •...

  • Use a java program that does the following: . (10 points) Write a program as follows...

    Use a java program that does the following: . (10 points) Write a program as follows a. Prompt the user to input two positive integers: nl and n2 (nl should be less than n2) b. If the user enters the negative number(s), convert it/them to positive number(s) c. If nl is greater than n2, swap them. d. Use a while loop to output all the even numbers between nl and n2 e. Use a while loop to output the sum...

  • java programming!!! Write the code fragment that will do the following: • Ask the user how...

    java programming!!! Write the code fragment that will do the following: • Ask the user how many numbers they want to enter • Declare and instantiate an array of doubles with as many elements as the number entered by the user • Write one 'for' loop to fill the array with data from the user • Write a second 'for' loop to calculate the sum of all of the numbers in the array • Print the calculated sum Note: Write...

  • Write a java program that creates a file called numbers.txt that uses the PrintWriter class. Write...

    Write a java program that creates a file called numbers.txt that uses the PrintWriter class. Write the odd numbers 1 to 99 into the file. Close the file. Using Scanner, open the numbers.txt file and read in the numbers. Add them all up and print the total. Close the file.

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