I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30
import java.io.*; // BufferedReader
import java.util.*; // Scanner to read from a text file
public class Project1
{
public static void main (String args[]) throws
Exception // we NEED this 'throws' clause
{
// ALWAYS TEST FIRST TO VERIFY USER
PUT REQUIRED CMD ARGS
if (args.length < 3)
{
System.out.println("\nusage: C:\\> java Project1 P1input.txt 1
30\n\n");
// i.e. C:\>
java Project1.txt P1input.txt 1 30
System.exit(0);
}
String infileName = args[0]; //
no need to convert, just copy into infileName
int lo = Integer.parseInt( args[1]
); // conver to int then store into lo
int hi = Integer.parseInt( args[2]
); // conver to int then store into hi
// STEP #1: OPEN THE INPUT FILE
AND COMPUTE THE MIN AND MAX. NO OUTPUT STATMENTS ALLOWED
Scanner infile = new Scanner( new
File(infileName) );
int min,max;
min=max=infile.nextInt(); // WE
ASSUME INPUT FILE HAS AT LEAST ONE VALUE
while ( infile.hasNextInt() )
{
// YOUR CODE
HERE FIND THE MIN AND MAX VALUES OF THE FILE
// USING THE
LEAST POSSIBLE NUMBER OF COMPARISONS
// ASSIGN
CORRECT VALUES INTO min & max INTHIS LOOP.
// MY CODE BELOW
WILL FORMAT THEM TO THE SCREEN
// DO NOT WRITE
ANY OUTPUT TO THE SCREEN
int num =
infile.nextInt();
if(num <
min)
min = num;
if(num >
max)
max = num;
}
System.out.format("min: %d max: %d\n",min,max); // DO NOT REMOVE OR MODIFY IN ANY WAY
// STEP #2: DO NOT MODIFY THE REST
OF MAIN. USE THIS CODE AS IS
// WE ARE TESTING EVERY NUMBER
BETWEEN LO AND HI INCLUSIVE FOR
// BEING PRIME AND/OR BEING
PERFECT
for (int i=lo ; i<=hi ;
++i)
{
System.out.print( i );
if ( isPrime(i)
) System.out.print( " prime ");
if (
isPerfect(i) ) System.out.print( " perfect ");
System.out.println();
}
} // END MAIN
// *************** YOU FILL IN THE METHODS BELOW **********************
// RETURNs true if and only if the number passed in
is perfect
static boolean isPerfect( int n )
{
int sum = 0;
for (int i =1; i <= n/2;
i++)
{
if(n % i ==
0)
sum += i;
}
if (sum == n)
return true;
else
return false; //
(just to make it compile) YOU CHANGE AS NEEDED
}
// RETURNs true if and only if the number passed in is
prime
static boolean isPrime( int n )
{
for (int f = 2; f <= n/2;
f++)
{
if (n % f == 0)
return
false;
}
return true; // (just to make it compile) YOU
CHANGE AS NEEDED
}
}
// Project1.java
import java.io.*; // BufferedReader
import java.util.*; // Scanner to read from a text file
public class Project1
{
public static void main (String args[]) throws
Exception // we NEED this 'throws' clause
{
// ALWAYS TEST FIRST TO VERIFY USER
PUT REQUIRED CMD ARGS
if (args.length < 3)
{
System.out.println(" usage: C:\> java Project1 <input file
name> <lo> <hi> ");
// i.e. C:>
java Project1.txt P1input.txt 1 30
System.exit(0);
}
// grab args[0] and store into a
String var named infileName
final String infileName = args[0];
// grab args[1] and conver to int then store into a var named lo
final int lo = Integer.parseInt(args[1]);
// grab args[2] and conver to int then store into a var named hi
final int hi = Integer.parseInt(args[2]);
// STEP #1: OPEN THE INPUT FILE
AND COMPUTE THE MIN AND MAX. NO OUTPUT STATMENTS ALLOWED
Scanner infile = new Scanner( new
File(infileName) );
int min,max;
min=max=infile.nextInt(); // WE
ASSUME INPUT FILE HAS AT LEAST ONE VALUE
while ( infile.hasNextInt() )
{
int next =
infile.nextInt();
if (next <
min){
min =
next;
}
if (next >
max){
max =
next;
}
}
System.out.format("min: %d max: %d
",min,max); // DO NOT REMOVE OR MODIFY IN ANY WAY
// STEP #2: DO NOT MODIFY THE REST
OF MAIN. USE THIS CODE AS IS
// WE ARE TESTING EVERY NUMBER
BETWEEN LO AND HI INCLUSIVE FOR
// BEING PRIME AND/OR BEING
PERFECT
for ( int i=lo ; i<=hi ;
++i)
{
System.out.print( i );
if ( isPrime(i)
) System.out.print( " prime ");
if (
isPerfect(i) ) System.out.print( " perfect ");
System.out.println();
}
} // END MAIN
// *************** YOU FILL IN THE METHODS BELOW **********************
// RETURNs true if and only if the number passed in
is perfect
static boolean isPerfect( int n )
{
int total = 0;
for (int i = 1; i < n; i++){
if ((n % i) == 0){
total += i;
}
}
if (total == n){
return true;
}
return false;
}
// RETURNs true if and only if the number passed in is
prime
static boolean isPrime( int n )
{
if (n == 1){
return false;
}
for (int i = 1; i < n; i++){
if ((n % i) == 0 && (n / i)
!= 1 && (n / i) != n){
return false;
}
}
return true; // (just to make it
compile) YOU CHANGE AS NEEDED
}
} // END Project1 CLASS
I am given an input file, P1input.txt and I have to write code to find the...
composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main { public static void main(String[] args) { System.out.print("Enter the...
I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...
Below I have my 3 files. I am trying to make a dog array that aggerates with the human array. I want the users to be able to name the dogs and display the dog array but it isn't working. //Main File import java.util.*; import java.util.Scanner; public class Main { public static void main(String[] args) { System.out.print("There are 5 humans.\n"); array(); } public static String[] array() { //Let the user...
Create a method based program to find if a number is prime and then print all the prime numbers from 1 through 500 Method Name: isPrime(int num) and returns a boolean Use for loop to capture all the prime numbers (1 through 500) Create a file to add the list of prime numbers Working Files: public class IsPrimeMethod { public static void main(String[] args) { String input; // To hold keyboard input String message; // Message...
I need help on creating a while loop for my Java assignment. I am tasked to create a guessing game with numbers 1-10. I am stuck on creating a code where in I have to ask the user if they want to play again. This is the code I have: package journal3c; import java.util.Scanner; import java.util.Random; public class Journal3C { public static void main(String[] args) { Scanner in = new Scanner(System.in); Random rnd = new Random(); System.out.println("Guess a number between...
Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...
need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. // Prompt user for value to start // Value must be between 1 and 20 inclusive // At command line, count down to blastoff // With a brief pause between each displayed value import java.util.*; public class DebugSix3 { public static void...
Please explain if the following code is actually correct. If the following code correct, please explain why the code works and is also correct. Don’t use * Java’s Integer .toBinaryString(int) in this program./* According to the textbook and the instructor, I am not supposed to use arrays such as int binary[] = new int[25]; I guess this is the reason why this problem is starting to look kind of hard. Chapter 5 Exercise 37: Java Programming * * (Decimal to...
I have this program that works but not for the correct input file. I need the program to detect the commas Input looks like: first_name,last_name,grade1,grade2,grade3,grade4,grade5 Dylan,Kelly,97,99,95,88,94 Tom,Brady,100,90,54,91,77 Adam,Sandler,90,87,78,66,55 Michael,Jordan,80,95,100,89,79 Elon,Musk,80,58,76,100,95 output needs to look like: Tom Brady -------------------------- Assignment 1: A Assignment 2: A Assignment 3: E Assignment 4: A Assignment 5: C Final Grade: 82.4 = B The current program: import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.Scanner; public class main { public static void main(String[]...