Advertisement
Guest User

Untitled

a guest
Dec 1st, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Writing {
  5. public static void main(String args []) {
  6. System.out.print("Enter desired username:");
  7. // println prints the string and moves the cursor to the next line while print doesn't.
  8. Scanner scan = new Scanner(System.in);
  9. String accountName = scan.nextLine();
  10. FileWriter fWriter = null;
  11. BufferedWriter writer = null;
  12. try {
  13. //try is used as a block to indicate up to where the "catch" function should check.
  14. fWriter = new FileWriter("C:\\Java trials\\Accounts.txt");
  15. writer = new BufferedWriter(fWriter);
  16. writer.write("Username:" + accountName);
  17. writer.newLine();
  18. writer.close();
  19. System.out.println("Your username \"" + accountName + "\" was saved.");
  20. // .err makes the output RED. When .out would make it the same as everything else.
  21. //to add quotes to a string or a text line use backslash (\) before the desired quote.
  22. } catch (Exception e) {
  23. // catch checks if there's an error up to the try block and if it comes true
  24. // it prints out "Error!" in the console.
  25. // (Exception e) calls for the string e which doesn't exist, thus impractical.
  26. System.out.println("Error!");
  27. }
  28.  
  29. System.out.print("Enter desired password:");
  30. Scanner scan2 = new Scanner(System.in);
  31. String accountPass = scan2.nextLine();
  32. FileWriter fWriter2 = null;
  33. BufferedWriter writer2 = null;
  34. try {
  35. fWriter2 = new FileWriter("C:\\Java trials\\Accounts.txt");
  36. writer2 = new BufferedWriter(fWriter2);
  37. writer2.newLine();
  38. writer2.write("Password:" + accountPass);
  39. writer2.newLine();
  40. writer2.close();
  41. System.out.println("Your password \"" + accountPass + "\" was saved.");
  42. //Each function has a slightly different string as the values that are being inputed
  43. // are different and require separate strings.
  44. } catch (Exception e) {
  45. System.out.println("Error!");
  46.  
  47.  
  48. }
  49.  
  50.  
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement