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

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

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

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • In JAVA, In two classes: As a zookeeper, it is important to know the activities of...

    In JAVA, In two classes: As a zookeeper, it is important to know the activities of the animals in your care and to monitor their living habitats. Create a monitoring system that does all of the following: -Asks a user if they want to monitor an animal, monitor a habitat, or exit -Displays a list of animal/habitat options (based on the previous selection) as read from either the animals or habitats file and asks the user to enter one of...

  • This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static...

    This is the contents of Lab11.java import java.util.Scanner; import java.io.*; public class Lab11 { public static void main(String args[]) throws IOException { Scanner inFile = new Scanner(new File(args[0])); Scanner keyboard = new Scanner(System.in);    TwoDArray array = new TwoDArray(inFile); inFile.close(); int numRows = array.getNumRows(); int numCols = array.getNumCols(); int choice;    do { System.out.println(); System.out.println("\t1. Find the number of rows in the 2D array"); System.out.println("\t2. Find the number of columns in the 2D array"); System.out.println("\t3. Find the sum of elements...

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