fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception {
  11. System.out.println("*INTENDED* Function of this-To create a custom method to take in a variable 'nameRaw' and change it within the function 'nameChanger()' then out put the changed name to variable: 'nameChanged'.\nProblem: the name changes within the function 'nameChanger()' only, therefore I cannot call the variable 'nameChanged' elsewhere, such as in main().........\n\n\n");
  12. // Initalize the Java Scanner
  13. Scanner in = new Scanner(System.in);
  14.  
  15. // Initalizes a completely empty string with name "nameChanged"
  16. String nameChanged = null;
  17.  
  18. System.out.println("Please enter a username for yourself below:");
  19. System.out.println("Test"); // Fake input to make me feel good I guess, it just looks better
  20.  
  21. // Sets "nameRaw" to "Test"
  22. String nameRaw = in.nextLine();
  23.  
  24. // Spits out the untouched value that the user has entered
  25. System.out.println("\nRaw name before passing to custom method: " + nameRaw);
  26.  
  27. // Puts "nameRaw" into the custom method "nameChanger()" to change the, "s" in, "Test" to a "z" by default
  28. nameChanger(nameRaw, nameChanged);
  29.  
  30. nameChanged = nameChanger(nameRaw, nameChanged);
  31.  
  32. // Spits out the touched and changed nameChanged variable from "nameChanger()"
  33. if(nameChanged == null) {
  34. System.out.println("\nHere is the failed changed name: " + nameChanged);
  35. System.out.println("\nARE YOU KIDDING ME! WHY DOES THIS NOT WORK?!?! PLEASE HELP!!!!");
  36. } else {
  37. System.out.println("Here is the successfuly changed name: " + nameChanged);
  38. System.out.println("\nWhoever solved this problem is a god..");
  39. }
  40.  
  41. } // Closes method main()
  42.  
  43. // Custom method named "nameChanger" that will need a variable named "nameRaw" *which is set* within its () to function
  44. private static String nameChanger(String nameRaw, String nameChanged) {
  45.  
  46. //// Initalizes a completely empty string with name "nameChanged"
  47. //String nameChanged = null;
  48.  
  49. // States the set name *unchanged* from the main method on line 17
  50. System.out.println("\t#\tName unchanged read and displayed by the nameChanger custom method: " + nameRaw);
  51.  
  52. // The name by default "Test" does contain an "s" so the if statement is satisfied
  53. if(nameRaw.contains("s")) {
  54.  
  55. // The variable "nameRaw should be running through here and having it's "s" replaced with a "z"
  56. nameChanged = nameRaw.replace("s", "z");
  57.  
  58. // Output the changed name *if all goes right*
  59. System.out.println("\t#\tName changed *still in custom function* is: " + nameChanged);
  60.  
  61. // The else statement is just for testing purposes, such as changing "Test" as the input to "Demo" to dis-satisfy the if statemtent
  62. } else {
  63. System.out.println("\t#\tFor reference, here is the unchanged name raw: " + nameRaw);
  64. System.out.println("\t#\tAlso for reference, here is the null value 'nameChanged': " + nameChanged);
  65. }
  66.  
  67. // One more time to show my hate toward Java, output the changed variable "nameChanged". Take note that the "s" is a "z".....
  68. System.out.println("\t#\tPlease don't be null or have an 's' in it: " + nameChanged);
  69.  
  70. // To output to main() that nameChanged is now not null *if all goes right*, but "Tezt" should be the output
  71. return nameChanged;
  72. } // Close custom method, nameChanger()
  73. }
Success #stdin #stdout 0.06s 321344KB
stdin
Test
stdout
*INTENDED* Function of this-To create a custom method to take in a variable 'nameRaw' and change it within the function 'nameChanger()' then out put the changed name to variable: 'nameChanged'.
Problem: the name changes within the function 'nameChanger()' only, therefore I cannot call the variable 'nameChanged' elsewhere, such as in main().........



Please enter a username for yourself below:
Test

Raw name before passing to custom method: Test
	#	Name unchanged read and displayed by the nameChanger custom method: Test
	#	Name changed *still in custom function* is: Tezt
	#	Please don't be null or have an 's' in it: Tezt
	#	Name unchanged read and displayed by the nameChanger custom method: Test
	#	Name changed *still in custom function* is: Tezt
	#	Please don't be null or have an 's' in it: Tezt
Here is the successfuly changed name: Tezt

Whoever solved this problem is a god..