Why does this code print out random letters? So if you can't tell what I am trying to do, I created four functions. delete_File() Deletes the file if created. new_File() creates a new file called data.txt, read_File() reads the data.txt file, and write_File() writes to data.txt.
NEW FILE FUNCTION
So first thing after I create the functions I call the new_File function which creates a new file this is the code written to create a new file
// This is the code writen to create a new file. File myObj = new File("data.txt"); The next thing the new_File function does is check if the file was created. Which is made possible with this part of the code.
if (myObj.delete()) { System.out.println("Deleted the file: " + myObj.getName()); } Then, I added an else statment which basicly says if the file is not there or is created creates a String variable names it Status and puts this on the variable "file on machine" here is the code that does that.
else { String status = "file on machine"; } Then the final part of this function is the catch function which basicly looks out for a IOException e here is the code for that.
catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } READ FILE FUNCTION
What the read file function does is pretty straight forward just uses a Scanner and a .nextLine function saves the data found on the file to a data variable then prints out and prints out the data. Here is the code that does.
public static String write_File(double Write_This) { try { FileWriter myWriter = new FileWriter("data.txt"); myWriter.write((int) Write_This); myWriter.close(); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } return "..."; } WRITE FILE FUNCTION
So what this function does is create a paremater, the paremeter is a Double named Write_This. And uses the java.io FileWriter method to write to the file and the normal catch for the IOException e error. Here is the code where I do that.
public static String write_File(double Write_This) { try { FileWriter myWriter = new FileWriter("data.txt"); myWriter.write((int) Write_This); myWriter.close(); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } return "..."; } ALL THE CODE PUT TOGETTER
So what I do here is call the new_File function which creates a new file. Reads the file so if there is any data on that file it would print it out. Then there is the gpa calculator, after that there is the write_File function we talked about earlier. The write_File function writes the input entered as grade_Average.
package com.company; import java.io.*; import java.util.Scanner; public class beta_Gpa_Calculator { public static void new_File() { try { File myObj = new File("data.txt"); if (myObj.createNewFile()) { String result = "Done"; } else { String status = "file on machine"; } } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public static void read_File() { try { File myObj = new File("data.txt"); Scanner myReader = new Scanner(myObj); while (myReader.hasNextLine()) { String data = myReader.nextLine(); System.out.println(data); } myReader.close(); } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } } public static String write_File(double Write_This) { try { FileWriter myWriter = new FileWriter("data.txt"); myWriter.write((int) Write_This); myWriter.close(); } catch (IOException e) { System.out.println("An error occurred."); e.printStackTrace(); } return "..."; } public static void delete_File() { File myObj = new File("data.txt"); if (myObj.delete()) { System.out.println("Deleted the file: " + myObj.getName()); } else { System.out.println("Failed to delete the file."); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean quit = false; while (!quit) { new_File(); read_File(); System.out.println("Enter your grade average in percent form. Type 1010 to exit."); int grade_Average = (int) sc.nextDouble(); if (grade_Average <= 59) { System.out.println("0.0 GPA"); } else if (grade_Average <= 62) { System.out.println("0.7 GPA"); } else if (grade_Average <= 63) { System.out.println("1.0 GPA"); } else if (grade_Average <= 69) { System.out.println("1.3 GPA"); } else if (grade_Average <= 72) { System.out.println("1.7 GPA"); } else if (grade_Average <= 76) { System.out.println("2.0 GPA"); } else if (grade_Average <= 79) { System.out.println("7.3 GPA"); } else if (grade_Average <= 82) { System.out.println("2.7"); } else if (grade_Average <= 86) { System.out.println("3.0 GPA"); } else if (grade_Average <= 89) System.out.println("3.3"); else if (grade_Average <= 92) { System.out.println("3.7 GPA"); } else if (grade_Average <= 100) { System.out.println("4.0 GPA"); } else if (grade_Average == 1010) { System.out.println("Quiting..."); quit = true; } else { System.out.println(" Invalid Input Please try again"); } write_File(grade_Average); } } } PROBLEM
So here is the problem when you run this code it prints random letters I do not know where the random letter are comming from. Run the code for yourself when you enter a grade average it prints the gpa plus some random letter can you explain to me what is going on here? There is a screenshot bellow.
没有评论:
发表评论