Different approach to this code
Instructor informed me today that my code is similar to another students and I must redo it. I am not quite sure what to change to give her what she is asking for. Any suggestions? new to JAVA.
Code java:
<import java.util.*;
import java.io.*;
import java.text.SimpleDateFormat;
public class A2RE5161843 {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
int firstNum = 0, secondNum = 0;
boolean valid = false;
while (!valid) {
System.out.print("Enter first number :");
firstNum = input.nextInt();
if (firstNum < 0) {
System.out.println("Enter positive number ");
} else if (firstNum > 1000) {
System.out.println("Enter number less than 1000");
} else {
valid = true;
}
}
valid = false;
while (!valid) {
System.out.print("Enter second number :");
secondNum = input.nextInt();
if (secondNum < 0) {
System.out.println("Enter positive number ");
} else if (secondNum > 1000) {
System.out.println("Enter number less than 1000");
} else if ((secondNum - firstNum) < 10) {
System.out.println("Second number should be at least 10 greater than first number");
} else {
valid = true;
}
}
FileWriter fw = new FileWriter(new File("A2RE5161843.txt"));
//Use a for loop to perform the following steps:
//Continue writing to the same file as before.
//Write ad label as before.
//Output all odd numbers between firstNum and secondNum inclusive, one number per line.
fw.write("1. All odd numbers between firstNum and secondNum inclusive");
fw.write(System.getProperty("line.separator"));
for (int i = firstNum; i <= secondNum; i++) {
if (i % 2 != 0) {
fw.write(i + "");
fw.write(System.getProperty("line.separator"));
}
}
//Output the sum of all numbers between firstNum and secondNum exclusive.
fw.write("2. The sum of all numbers between firstNum and secondNum exclusive");
fw.write(System.getProperty("line.separator"));
int sum = 0;
for (int i = firstNum + 1; i < secondNum; i++) {
sum += i;
}
fw.write(sum + "");
fw.write(System.getProperty("line.separator"));
//Output all numbers from secondNum to firstNum in a single line with commas separating the numbers.
fw.write("3. All numbers from secondNum to firstNum in a single line with commas separating the numbers");
fw.write(System.getProperty("line.separator"));
for (int i = firstNum; i <= secondNum; i++) {
fw.write(i + ",");
}
fw.write(System.getProperty("line.separator"));
//Write the date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss.
fw.write("4. The date and time as the last line in the file in the format yyy-mm-dd hh:mm:ss");
fw.write(System.getProperty("line.separator"));
fw.write((new SimpleDateFormat("yyyy-mm-dd hh:mm:ss")).format(new Date()));
System.out.println("File Assignment2.txt written");
fw.close();
}
}>
Re: Different approach to this code
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Re: Different approach to this code