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 first method will input into the 2-D array and return it. The other two methods are void methods. Here is the primary set of code for main. String [] [] data = input("test1.txt"); process("test2.txt", data); output("output.txt", data); Code: import java.io.File; import java.io.IOException; //import java.io.PrintWriter; import java.util.Scanner; public class Program11A { public static void main(String []args) throws IOException { // Read from the file: String[][] data = input("test1.txt"); process("test2.txt", data); output("output.txt", data); } private static String [ ] [ ] input(String filename)throws IOException { File f1 = new File( filename );//create the file Scanner inf1 = new Scanner( f1 );// set up scanner for the input file int numrows = inf1.nextInt( ); //read # of rows and columns int numcols = inf1.nextInt( ); String [ ] [ ] table = new String [ numrows] [numcols]; for (int x = 0; x < numrows; x++) for (int y = 0; y < numcols; y++) table[x][y] = inf1.next( ); return table; } private static void process( String filename, String [][] table ) { int errorcount = 0; int linecount = 0; File f2 = new File( filename ); //create the file Scanner inf2 = new Scanner( f2 ); // set up scanner for the input file while ( inf2.hasNext() ) { row = inf2.nextInt( ); col = inf2.nextInt( ); str1 = inf2.next( ); str2 = inf2.next( ); lineCount = lineCount + 1; if ( (table [ row ] [ col ]).equals(str1) ) table [ row ] [ col ] = str2; else errorcount++; }// put code here to print to System.out the file name, the total number of lines and the total// number of errors} } private static void output(){ ? } }
//thanks...
//------------------ Program11.java
//package project123;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Program11 {
static String[][] data;
public static void main(String[] args) throws IOException {
data = input("test1.txt"); //Read 2d data from the file:
process("test2.txt", data); //make changes in data as per test2.txt
file
output("output.txt", data); //save modified data to file
}
private static String[][] input(String filename) throws
IOException {
File f1 = new File(filename);
//create the file
Scanner inf1 = new Scanner(f1);
//set up scanner
//for the input file
// read # of rows and columns
int numrows = inf1.nextInt();
int numcols = inf1.nextInt();
String[][] table = new String[numrows][numcols];
for (int x = 0; x < numrows; x++) {
for (int y = 0; y < numcols; y++) {
table[x][y] = inf1.next();
System.out.println(table[x][y]); //print table data
}
}
return table;
}
private static void process(String filename, String[][] table)
throws FileNotFoundException {
int errorcount = 0;
int linecount = 0;
File f2 = new File(filename);
//create the file
Scanner inf2 = new Scanner(f2);
//set up scanner for the input file
int row;
int col;
String str1;
String str2;
int lineCount=0;
// read line by line
while (inf2.hasNext()) {
row = inf2.nextInt(); //read row number
col = inf2.nextInt(); //read col number
str1 = inf2.next(); //read old value string
str2 = inf2.next(); //read new value string
lineCount = lineCount + 1; // increase linecount
if ((table[row][col]).equals(str1)) { // if old value present in
table at row col position
table[row][col] = str2; //then replace new value
} else {
errorcount++; //if old value is different than which is present in
table then increase errorcount
}
}
//put code here to print to System.out the file name, the total
number of lines
//and the total number of errors
System.err.println("File name: " + filename);
System.err.println("Total lines: " + lineCount);
System.err.println("Total errors: "+ errorcount);
}
private static void output(String filename, String[][] table)
throws FileNotFoundException {
File f3 = new File(filename);
PrintWriter writer = new PrintWriter(f3);
int numrows = table.length; //get rows count of table
int numcols = table[0].length; //get cols count of table (length of
first row)
//write table data to file
for (int x = 0; x < numrows; x++) {
for (int y = 0; y < numcols; y++) {
writer.write(table[x][y] + " ");
}
writer.write("\n");//print newline after each row
}
writer.flush(); //save data to file
writer.close(); //close file
}
}
//------------------ end of Program11.java
//-------------- test1.txt
3 2
one two
three four
five six
//-------------- end of test1.txt
//-------------- test2.txt (contains row col oldval
newval)
0 0 one onee
0 1 two twoo
1 0 thr three
//-------------- end of test2.txt
//-------------- output.txt NO NEED TO SAVE. It will be
generated
onee twoo
three four
five six
//-------------- end of output.txt
//-------- program output
one
two
three
four
five
six
File name: test2.txt
Total lines: 3
Total errors: 1
Java : Please help me correct my code: create a single class (Program11.java) with a main...
Computer Science - Java
Explain the code step by step (in detailed order). Also explain
what the program required. Thanks
7 import java.io.File; 8 import java.util.Scanner; 9 import java.util.Arrays; 10 import java.io.FileNotFoundException; 12 public class insertionSort 13 14 public static void insertionSort (double array]) 15 int n -array.length; for (int j-1; j < n; j++) 17 18 19 20 21 double key - array[j]; int i-_1 while ( (i > -1) && ( array [i] > key array [i+1] -...
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; }...
/* * CPS150_Lab10.java */ import java.io.*; import java.util.*; /** * CPS 150, Fall 2018 semester * * Section N1 * * Lab Project 13: Comparing Java Strings * * @author *** Replace with your name *** */ public class CPS150_Lab13 { static final Scanner KBD = new Scanner(System.in); static final PrintStream OUT = System.out; // TO DO: Implement each of the following 4 methods, // using the String compareTo method: /* * lessThan(String, String) -> boolean * * method is...
Java:
Create the skeleton.
Create a new file called ‘BasicJava4.java’
Create a class in the file with the appropriate name.
public class BasicJava4 {
Add four methods to the class that return a default value.
public static boolean isAlphabetic(char aChar)
public static int round(double num)
public static boolean useSameChars(String str1, String
str2)
public static int reverse(int num)
Implement the methods.
public static boolean isAlphabetic(char aChar):
Returns true if the argument is an alphabetic character, return
false otherwise. Do NOT use...
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...
Identify a logical error in the following code and fix it. public class test1 { public static void main(String[] args){ int total; float avg; int a, b, c; a=10; b=5; c=2; total=a+b+c; avg=total/3; System.out.println("Average: "+avg); } } Answer: Write the output of the program below. import java.util.Scanner; public class test2 { public static void main(String[] arg){ int psid; String name; Scanner input=new Scanner(System.in); System.out.println("Enter your PSID"); psid=input.nextInt(); System.out.println("Enter your...
Can someone help me out with this? You are given a program that receives four lines in the below format, and stores them in str1, str2, str3, and num1. This is not a very long sentence. is long 4 Expand this program to: Write an if-elseif-else statement to print this line only if num1 is higher than 0: "Num1 is higher than 0!" print this line only if num1 is 0: "Num1 equals to 0!" And otherwise, print: ""Num1 is...
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...
Help check why the exception exist do some change but be sure to use the printwriter and scanner and make the code more readability Input.txt format like this: Joe sam, thd, 9, 4, 20 import java.io.File; import java.io.PrintWriter; import java.io.IOException; import java.io.FileNotFoundException; import java.io.FileWriter; import java.util.Scanner; public class Main1 { private static final Scanner scan = new Scanner(System.in); private static String[] player = new String[622]; private static String DATA = " "; private static int COUNTS = 0; public static...
Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration” program...