Modify your program to take 2 command line arguments. It should print out an err
ID: 3681840 • Letter: M
Question
Modify your program to take 2 command line arguments. It should print out an error message if the program is run without 2 arguments. Hint: check the length of args
• The first command line argument will be the number of integers to read from the array . Your program should sum these numbers and print the sum.
For example: java Lab8 6 1 should print out 392 which is the sum of the first 6 numbers in the array.
Make sure this works before moving on.
You can enter any argument you want for the 2 nd command line argument for right now.
• Next, modify your program to utilize the second CLA. The second argument should control whether your program sums numbers using every consecutive element in the array, every other element, every third element, and so forth.
For example, java Lab8 6 2 sums 6 numbers by summing every other element in the array , starting with index 0 . When run with these CLAs, the program should print out the value 351.
Another example: java Lab8 3 3 reads 3 numbers, every 3 rd value in the array, starting with index 0. When run with these CLAs the program should print out the value 157.
• You can assume that the user of your program will always enter CLAs which can always be converted to integers. You can also assume that the user will not enter a number which is gre ater than the number of lines in the file, and will not request more numbers then are in the file.
• Some other examples you can use to test your program: java Lab8 300 3 13617 java Lab8 100 4 5626 java Lab8 5 20 427
This is my program so far:
import java.util.Scanner;
import java.io.*;
public class lab8 {
public static void main(String[] args) throws IOException
{ int count = 0;
File x = new File("nums.txt"); //declaring and initializing nums.txt
Scanner file = new Scanner(x);//declaring and inititalizing scanner
while(file.hasNextInt())
{
file.nextInt();
count++;
}
System.out.println("Total integers = " + count);
int[] hold = new int[count];
File y = new File("nums.txt");
Scanner file2 = new Scanner(y);
for(int i = 0; i < count; i = i + 1) {
hold[i] = file2.nextInt();
}
for (int i = 0; i < hold.length; i++) {
System.out.println(hold[1]);
System.out.println(hold[2]);
System.out.println(hold[3]);
System.out.println(hold[4]);
System.out.println(hold[5]);
System.out.println(hold[6]);
System.out.println(hold[7]);
System.out.println(hold[8]);
System.out.println(hold[9]);
System.out.println(hold[10]);
break;
}
int sum = 0;
for(int i = 0; i<args.length; i++) {
sum = sum + Integer.parseInt(args[i]);
}
System.out.println("Sum: " + sum);
}
}
Explanation / Answer
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class lab8 {
public static void main(String[] args) throws IOException {
// if CLSs are less than 2 , quit program
if(args==null || args.length<2){
System.out.println("Lesser number of CLS");
return;
}
int count = 0;
File x = new File("nums.txt"); // declaring and initializing nums.txt
Scanner file = new Scanner(x);// declaring and inititalizing scanner
while (file.hasNextInt()) {
file.nextInt();
count++;
}
System.out.println("Total integers = " + count);
int[] hold = new int[count];
File y = new File("nums.txt");
Scanner file2 = new Scanner(y);
for (int i = 0; i < count; i = i + 1) {
hold[i] = file2.nextInt();
}
/*for (int i = 0; i < hold.length; i++) {
System.out.println(hold[1]);
System.out.println(hold[2]);
System.out.println(hold[3]);
System.out.println(hold[4]);
System.out.println(hold[5]);
System.out.println(hold[6]);
System.out.println(hold[7]);
System.out.println(hold[8]);
System.out.println(hold[9]);
System.out.println(hold[10]);
break;
}*/
int numberToSum = Integer.parseInt(args[0]);
int increment = Integer.parseInt(args[1]);
int sum = 0;
int cnt = 1;
/*
// this loop will sum first numberToSum elements from array
// do not consider second CLS
for (int i = 0; cnt<=numberToSum && i < args.length; i++) {
sum = sum + Integer.parseInt(args[i]);
cnt++;
}
*/
// this program will sum numberToSum numbers , with loop increment equal to 'increment'
for (int i = 0; cnt<=numberToSum && i < args.length; i=i+increment) {
sum = sum + Integer.parseInt(args[i]);
cnt++;
}
System.out.println("Sum: " + sum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.