java class and static method -
i have question regarding how use variables have used in method method , how set limit on user can input, example, 1 of java homework assignments need write program takes 3 6 names , need create new static method called geneatenewname() , has use 3 6 names display names take each of names' second letter , produce new name, how can this, thank you. also, here code have far:
public class testing{ public static void main(string args[]){ console c = system.console(); string str1; string str2; string str3; string str4; string str5; string str6; str1 = c.readline("please enter family member's name: "); str2 = c.readline("please enter family member's name: "); str3 = c.readline("please enter family member's name: "); str4 = c.readline("please enter family member's name: "); str5 = c.readline("please enter family member's name: "); str6 = c.readline("please enter family member's name: "); } public static void generatenewname(){ system.out.println((str1.charat(1))); system.out.print(str22.charat(1)); system.out.print(str33.charat(1)); system.out.print(str44.charat(1)); system.out.print(str55.charat(1)); system.out.print(str66.charat(1)); } }
when run program on command line, asks 6 names, doesn't display newly generated name coded in generatenewname() static method.
like others have said, never call generatenewname()
doesn't run. there bigger problems here.
you should using scanner
read user input. you're declaring string variables in main()
limits scope generatenewname()
can't see them. there several ways can make strings available generatenewname()
, including passing in strings individually or storing them in array , passing in. purposes can declare strings static class variables.
class testing { public static string str1; public static string str2; public static string str3; public static string str4; public static string str5; public static string str6; public static void main(string args[]) { scanner scanner = new scanner(system.in); system.out.println("please enter family member's name: "); str1 = scanner.next(); system.out.println("please enter family member's name: "); str2 = scanner.next(); system.out.println("please enter family member's name: "); str3 = scanner.next(); system.out.println("please enter family member's name: "); str4 = scanner.next(); system.out.println("please enter family member's name: "); str5 = scanner.next(); system.out.println("please enter family member's name: "); str6 = scanner.next(); generatenewname(); } public static void generatenewname() { system.out.print(str1.charat(1)); system.out.print(str2.charat(1)); system.out.print(str3.charat(1)); system.out.print(str4.charat(1)); system.out.print(str5.charat(1)); system.out.print(str6.charat(1)); } }
since i'm assuming you're still beginner above code work fine. i'd show how cut way on number of lines of code have using techniques you've learned or learn soon. using for
loop , string array don't have write out lines each time want prompt , collect data user. there can pass array of names generatenewname()
, use for
loop print out new name. don't need declare strings individually , can control how many times user prompted provide input changing size of array.
class testing { public static void main(string args[]) { scanner scanner = new scanner(system.in); int arraysize = 0; while(arraysize > 6 || arraysize < 3) { //get user input here. } string[] names = new string[arraysize]; for(int = 0; < names.length; i++) { system.out.println("please enter family member's name: "); names[i] = scanner.next(); } generatenewname(names); } public static void generatenewname(string[] names) { for(int = 0; < names.length; i++) { system.out.print(names[i].charat(1)); } } }
to print out generated name on 1 line make sure use system.out.print()
, not system.out.println()
.
Comments
Post a Comment