Question

**Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by...

**Java**

Assume that indata1 and indata2 are two files containing at least two integers, separated by white space.

Write a program named Add2 that writes the sum of the first integers of these two files to a file named outdata1. It writes the sum of the first integers of these two files to a file named outdata2. Each sum is written on a line by itself.

So, if the contents of indata1 were "37 6 90" and the contents of indata2 were "1 4 9" then outdata1 would get 38 and outdata2 would get 10.

This is what i got

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
public class Add2{
public static void main(String[] args) throws Exception{
File f1 = new File("indata1.txt");
File f2 = new File("indata2.txt");
Scanner data1 = new Scanner(f1);
Scanner data2 = new Scanner(f2);   
int x1 = data1.nextInt();
int y1 = data1.nextInt();
int x2 = data2.nextInt();
int y2 = data2.nextInt();
File out1 = new File("outdata1");
File out2 = new File("outdata2");
PrintWriter pw1 = new PrintWriter(out1);   
PrintWriter pw2 = new PrintWriter(out2);
pw2.println(x1+x2);
pw1.println(y1+y2);
pw1.close();
pw2.close();
}
}
and this is the error i get on codelab:

  • The value of outdata1 is incorrect
  • The value of outdata2 is incorrect
  • outdata1 was not assigned a value
  • outdata2 was not assigned a value

what am i doing wrong here?

Thanks.

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

The program runs perfectly fine. Please try rerunning the code

INPUT
########

//########################### PGM START ###############################

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;

public class Add2{
   public static void main(String[] args) throws Exception{
       File f1 = new File("indata1.txt");
       File f2 = new File("indata2.txt");
      
      
       if(f1.exists() && f2.exists()) {
           Scanner data1 = new Scanner(f1);
           Scanner data2 = new Scanner(f2);
          
           int x1 = data1.nextInt();
           int y1 = data1.nextInt();
           int x2 = data2.nextInt();
           int y2 = data2.nextInt();
          
           File out1 = new File("outdata1");
           File out2 = new File("outdata2");
          
           if(out1 !=null && out2!=null) {
               PrintWriter pw1 = new PrintWriter(out1);
               PrintWriter pw2 = new PrintWriter(out2);
               pw1.println(x1+x2);
               pw2.println(y1+y2);
               pw1.close();
               pw2.close();
           }
          
          
           data1.close();
           data2.close();
       }
      
   }
}

//###############################################################

OUTPUT
#########

Add a comment
Know the answer?
Add Answer to:
**Java** Assume that indata1 and indata2 are two files containing at least two integers, separated by...
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
  • Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class...

    Can someone help me with my Java code error! Domain package challenge5race; import java.util.Random; public class Car {    private int year; private String model; private String make; int speed; public Car(int year, String model, String make, int speed) { this.year = year; this.model = model; this.make = make; this.speed = speed; } public Car() { } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public String getModel() { return model; }...

  • in JAVA, why my code can run, but it does not show any files on my...

    in JAVA, why my code can run, but it does not show any files on my desktop. Scanner k = new Scanner(System.in); System.out.println(" Please enter the first filename: "); String fileName1 = k.nextLine(); System.out.println(" Please enter the second filename: "); String fileName2 = k.nextLine(); File f1 = new File(fileName1); Scanner file1 = new Scanner(f1); PrintWriter f2 = new PrintWriter(fileName2);    while(file1.hasNext()){ String str= fileName1.toUpperCase(); f2.println(str.toUpperCase()); } file1.close(); f2.close(); }

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

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

  • Hi All, Can someone please help me correct the selection sort method in my program. the...

    Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...

  • 2.13.2: Get random numbers. JAVA Type two statements using nextInt() to print two random integers between...

    2.13.2: Get random numbers. JAVA Type two statements using nextInt() to print two random integers between (and including) 0 and 9. End with a newline. Ex: 5 7 Note: For this activity, using one statement may yield different output (due to the interpreter calling randGen.nextInt() in a different order). Use two statements for this: import java.util.Scanner; import java.util.Random; public class DiceRoll { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); Random randGen = new Random();...

  • JAVA: Rewrite the following code to where the inputs are from a file. The file name...

    JAVA: Rewrite the following code to where the inputs are from a file. The file name will be from the input from the user scanner. CODE: import java.util.*; public class Q1 { public static int sumOD (int k) { int sumOD = 0; int in = k; while (in != 0) { int digitON = in % 10; sumOD += digitON; in /= 10; } return sumOD; }    public static void main(String[] args) { int n, i, k; System.out.println("Enter...

  • Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed...

    Homework 1- Merge Files: 1. Design a class named MergeFiles 1 Three file names are passed as command-line arguments to MergeFiles as follows: java MergeFiles filel file2 mergedFile II. MergeFiles reads names stored in files file and file2 III. Merges the two list of names into a single list IV. Sorts that single list V. Ignores repetitions VI. Writes the sorted, free-of-repetitions list to a new file named mergedFile.txt VII. The names in all three files are stored as one...

  • Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions...

    Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...

  • Please write in java program Write an application that generates 100 random integers between 1 and...

    Please write in java program Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following: The sum of the numbers in the file The average of the numbers in the file The numbers in the file in increasing order To sort an array: int[] arr = new int[20]; Arrays.sort(arr); -- this sorts an array To sort an ArrayList: ArrayList<Integer>...

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