Hello. I'm hoping someone will be willing to help me out. I'm pretty much done except for one part I've been scratching my head over. I'm sure this headache I have is not helping. Below are the instructions and the code for my problem area.
Output the sum of all numbers between firstNum and secondNum exclusive.
The portion im working on begins on line 52. Thank you for your time!
import java.io.*;
import java.util.*;
import java.text.*;
class A2{
//user inputs and checks
public static void main(String[] args) throws Exception{
Scanner input=new Scanner(System.in);
System.out.print("Enter Num1: ");
int firstNum =input.nextInt();
System.out.print("Enter Num2: ");
int secondNum =input.nextInt();
while(firstNum<0){
System.out.print("Number should be positive.!Re-enter: ");
firstNum =input.nextInt();
}
while(secondNum<0){
System.out.print("Number should be positive.!Re-enter: ");
secondNum =input.nextInt();
}
while(secondNum>1000){
System.out.print("Second Number should be less than 1000!Re-enter: ");
secondNum =input.nextInt();
}
while(secondNum<firstNum){
System.out.print("Second Number should be at least 10 greater than firstNum.Re-enter: ");
secondNum =input.nextInt();
}
int sum=0;
//write new file always
File f;
f=new File("A2 OUTPUT.txt");
BufferedWriter bw=new BufferedWriter(new FileWriter(f,false));
bw.write("Odd numbers from "+Integer.toString(firstNum)+" to "+Integer.toString(secondNum)+",one number per line:"); //Odd numbers one per line
bw.newLine();
for(int i=firstNum;i<=secondNum;i++){
sum+=i;
if((i%2)!=0){
bw.write(Integer.toString(i));
bw.newLine();
}
}
bw.newLine();
//sum of all numbers excluding input values
bw.write("Sum of all numbers in between "+Integer.toString(firstNum)+ " and " +Integer.toString(secondNum)+":"+sum_between );
for (int i=(firstNum+1);i < secondNum; i++) {
sum+=i;{
bw.write(Integer.toString(i));
}}
bw.newLine();
bw.newLine();
// all numbers in reverse order with comma separating
bw.write("Output all numbers from "+Integer.toString(secondNum)+" to "+Integer.toString(firstNum)+":");
bw.newLine();
for(int i=secondNum;i>=firstNum;i--){
bw.write(Integer.toString(i)+",");
}
bw.newLine();
bw.newLine();
//date and time formatted
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
bw.write(sdf.format(d));
bw.close();
}
}