Can someone correct this for me?
The reason why your program does not output the data is because it gets hung up in an infinite loop.
See the feedback embedded below.
Correct the code and continue debugging your program.
You can use some extra print statements to help you when debugging.
After making the necessary corrections and testing your program resubmit the assignment for grading when ready.
Let me know if you have questions.
Renee Warren
Instructor
= = == = = = = = = = = = = = = = =
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
* Name: Dakota Mammedaty
* Date: 07/31/2016
* Assignment: A2
* Student Number: MA5331550
* Description: This program prompts the user to input two integers, verify the user entered acceptable number and provide feedback
*/
public class A2MA5331550 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Prompt the user to input two integers: firstNum and secondNum where
// secondNum is at least 10 greater than firstNum, both numbers are positive
// integers, and secondNum is less than 1000.
boolean flag = true;
while(flag){
System.out.println(“Enter a positive number :”);
int firstNum = Integer.valueOf(scan.nextLine());
System.out.println(“Enter another positive number (at least 10 greater than first Num, and is less than 1000.) :”);
int secondNum = Integer.valueOf(scan.nextLine());
// Verify that the user entered acceptable numbers, and if not, provide error
// feedback and prompt them again.
if(firstNum <= 0 || secondNum <=0 ){
System.out.println(“Invalid number “);
writeToFile(“Invalid number “);
continue;
}
else if(secondNum < firstNum +10){
System.out.println(“Second number is not 10 greater than first”);
writeToFile(“Second number is not 10 greater than first”);
continue;
}
else if(secondNum > 1000){
System.out.println(“Second number is greater than 1000”);
writeToFile(“Second number is greater than 1000”);
continue;
}
else{
/*
Once you have values that meet the requirements, the program should terminate the while loop used to read in the values.
Processing of the data should be below the loop.
*/
/*
Process data
*/
//4. Output all odd numbers between firstNum and secondNum inclusive, one
//number per line.
int firstNumNew = firstNum;
String output = “”;
/*
* This is an infinite loop.
*
* Because firstNumNew is only incremented if it is odd, it never
* gets incremented once it is an even number.
*/
while(firstNumNew <= secondNum){
if(firstNumNew % 2 == 1){
output = output +”n” +firstNumNew;
firstNumNew++; // firstNumNew only gets incremented once –
// if firstNumNew is odd.
}
firstNumNew++; // This is where firstNumNew should be incremented.
}
// the fact that the print statement below never displays its output
// was a hint that the problem was above that line of code.
System.out.println(“output : “+output);
writeToFile(output);
// 5. Output the sum of all numbers between firstNum and secondNum
// exclusive.
int sum = 0;
for(int i=firstNum+1; i<secondNum; i++){
sum = sum + i;
}
output=String.valueOf(sum);
writeToFile(output);
System.out.println(“output : “+output);
break;
} // end of the if-else sequence
} // end of the while-loop
} // end of main method
/**
* 3. Output all results to a file in the same directory as the program, placing an
* appropriate label between each section of output. Note that your program
* must be able to run repeatedly overwriting the file from the previous run.
* @param content
*/
public static void writeToFile(String content)
{
// creating FileWriter Object using file2
FileWriter filewriter;
try {
filewriter = new FileWriter(“file_out.txt”);
// Writing the content
filewriter.write(content);
filewriter.flush();
filewriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Expert Answer
A2MA5331550.java
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
/**
* Name: Dakota Mammedaty Date: 07/31/2016 Assignment: A2 Student Number:
* MA5331550 Description: This program prompts the user to input two integers,
* verify the user entered acceptable number and provide feedback
*/
public class A2MA5331550 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Prompt the user to input two integers: firstNum and secondNum where
// secondNum is at least 10 greater than firstNum, both numbers are
// positive
// integers, and secondNum is less than 1000.
// Declaring varaibles
int firstNum, secondNum, num, sum = 0;
/*
* This while loop continues to execute until the user enters a valid
* number
*/
while (true) {
// Getting the numbers entered by the user
System.out.print(“nEnter First number :”);
firstNum = scan.nextInt();
System.out.print(“Enter Second Number :”);
secondNum = scan.nextInt();
if (firstNum > 0 && secondNum > 0) {
if (secondNum – firstNum >= 10 && secondNum < 1000) {
break;
} else {
System.out.print(“:: Invalid Inputs ::”);
continue;
}
} else {
System.out.print(“:: Invalid Inputs ::”);
continue;
}
}
writeToFile(firstNum, secondNum);
} // end of main method
private static void writeToFile(int firstNum, int secondNum) {
// creating FileWriter Object using file2
int num, sum = 0;
FileWriter filewriter;
try {
filewriter = new FileWriter(“file_out.txt”);
// Writing the content
filewriter.write(“First Number :” + firstNum + “rn”);
filewriter.write(“Second Number :” + secondNum + “rn”);
num = firstNum;
// Writing to the output file
filewriter.write(“Odd Numbers between ” + firstNum + ” and ” + secondNum + “:rn”);
while (num <= secondNum) {
if (num % 2 != 0) {
filewriter.write(num + ” “);
}
num++;
}
filewriter.write(“nSum of all numbers between ” + firstNum + ” and ” + secondNum + “:”);
num = firstNum;
while (num <= secondNum) {
sum += num;
num++;
}
filewriter.write(“:” + sum);
filewriter.flush();
filewriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
_____________________
Output:
Enter First number :-10
Enter Second Number :50
:: Invalid Inputs ::
Enter First number :10
Enter Second Number :5
:: Invalid Inputs ::
Enter First number :10
Enter Second Number :1006
:: Invalid Inputs ::
Enter First number :10
Enter Second Number :60
_____________________
file_out.txt
First Number :10
Second Number :60
Odd Numbers between 10 and 60:
11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59
Sum of all numbers between 10 and 60::1785