我无法弄清楚如何将字符串需要传递给下面的方法。
stringToFile();和 readingStringFromFile();
我知道我需要在主方法中传递字符串,但我无法弄清楚如何。
提前致谢。
public static void main(String[] args) {
Scanner keyboard = new Scanner (System.in);//allow for use of keyboard input
mask(keyboard);
printingString();
fileName(keyboard);
stringToFile();
readingStringFromFile();
}
public static int mask(Scanner keyboard){
int holder;//creats a temp int
System.out.print("Enter the encryption mask: ");//asks fro encrytipon
holder = keyboard.nextInt();//userinput to holder
keyboard.nextLine();//consumption
return holder;//returns encryption mask
}
public static void fileName(Scanner keyboard){
String fileName ="a";
System.out.print("\nEnter a file name without extensions: ");
fileName = keyboard.next();//userinput to fileName
String completeFileName = fileName + ".txt";
}
public static void printingString(){
System.out.println("Original random character string:");
for (int i = 0; i < 50; i++)//loop to obtain 50 random characters
{
char randomChar = (char) ((Math.random()*255)+32);
System.out.print((randomChar));
}
}
public static void stringToFile(String completeFileName, String printingString)
throws FileNotFoundException {
System.out.println("Saving Original random character string...");
File myFile = new File (completeFileName);
Scanner fileReader = new Scanner (myFile);
PrintWriter fileWriter = new PrintWriter (myFile);
fileWriter.println(printingString);
fileWriter.close();
}
public static void readingStringFromFile(String completeFileName)
throws FileNotFoundException {
System.out.println("Original random character string from the file");
File myFile = new File (completeFileName);
Scanner fileReader = new Scanner (myFile);
String lineFromFile = fileReader.nextLine();
System.out.println(lineFromFile);
}
答案 0 :(得分:1)
在你的主要方法中你应该得到输入 -
System.out.println("Enter file name : ");
String completeFileName = keyboard.next();
System.out.println("Enter string : ");
String printingString = keyboard.next();
stringToFile(completeFileName, printingString);