I am a beginner and am stuck at a point where I don't know what to do. The assignments asks this:
1) ask user to input file name or "done" to quit
2) increment the file name: add "_n"
3) ask the user for a upper range limit of the random number (max is 100)
4) ask the user for a number of random numbers (m) that should be written to the file
5) create the file and place (m) random integers (with max value inserted by the user, less than 100)
6) Open the file created, have it read it and sum all the values up and display it
7) Loop back to #1 8) When user enters "done", display the number of files created during the session
this is what I have so far:
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Program {
public static void main (String[] args)
throws IOException {
String fileName;
String sum = null;
String upperRangeLimit;
String userRandomNumber;
boolean done = false;
int x = 1;
//int n;
while (!done)
{
fileName = JOptionPane.showInputDialog("Enter a file name or done to exit: ");
if (!(fileName.equals("done")))
fileName = "fileName" + "_" + x++;
Random randomNumbers = new Random(100);
upperRangeLimit = JOptionPane.showInputDialog("Enter the upper range limit:\n Maximum range is 100");
userRandomNumber = JOptionPane.showInputDialog("Error; Maximum range is 100. Enter another number: ");
File file = new File(fileName);
PrintWriter outputFile = new PrintWriter(fileName);
//for (n = 1; n < upperRangeLimit; n++)
//{
// sum =
//}
outputFile.println(randomNumbers);
outputFile.close();`enter code here`
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext())
{
sum = inputFile + inputFile.nextLine();
}
System.out.println(sum);
inputFile.close();
if (fileName.equals("done"))
done = true;
}
}
}
Help is much needed and appreciated, thank you!
I changed your code a bit, now it seem to be working fine like you wished it to be. read my comments to understand what you did wrong.
import javax.swing.JOptionPane;
import java.io.*;
import java.util.Random;
import java.util.Scanner;
public class Program{
public static void main(String[] args) throws IOException {
String fileName;
int sum = 0; // this should be integer because you want to sum the numbers...
String upperRangeLimit;
String userRandomNumber;
boolean done = false;
int x = 1;
// int n;
while (!done) {
fileName = JOptionPane.showInputDialog("Enter a file name or done to exit: ");
if (!(fileName.equals("done")))
fileName = fileName + "_" + x++; // you wrote "fileName" like that which means you dont actually
// reference the variable
// I didnt understand this part , i mean you ask for the input correctly but the text you provide to the user isnt write
upperRangeLimit = JOptionPane.showInputDialog("Enter the upper range limit:\n Maximum range is 100");
userRandomNumber = JOptionPane.showInputDialog("Error; Maximum range is 100. Enter another number: ");
File file = new File(fileName);
// convert user input to integers
int limit = Integer.parseInt(upperRangeLimit);
int range = Integer.parseInt(userRandomNumber);
// write to the file
PrintWriter outputFile = new PrintWriter(fileName);
for (int n = 1; n < limit; n++) {
// (Math.random() * ((max - min) + 1)) + min => way to generate random number in
// range
int random = (int) ((Math.random() * ((range - 1) + 1)) + 1);
outputFile.println(random);
}
outputFile.close();
// read the file
Scanner inputFile = new Scanner(file);
while (inputFile.hasNext()) {
// sum up all of the lines
// the integers are written in the file as strings so you have to convert them
// back to integers again
int nextNum = Integer.parseInt(inputFile.nextLine());
sum = sum + nextNum;
}
System.out.println(sum);
inputFile.close();
if (fileName.equals("done"))
done = true;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.