Results 1 to 6 of 6
- 08-31-2010, 10:09 AM #1
pass value inside method A to method B
I'm develop an CLI apps using java.. because i want t0 learn java.
here my snippet:
Java Code:import java.io.*; import java.lang.*; class Projek { public static void main (String args[] )throws IOException { int menus; String input; BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); { Projek Mas = new Projek(); // for (int Ulang = 1; Ulang == 1;) { Mas.Arahan(); //<--- calling the function System.out.print(" \t\t-1 timetable \n \t\t-2 Buy ticket \n \t\t-3 pay ticket\n"); System.out.print("\n # You choose \t\t: "); input = stdin.readLine(); while(isIntNumber(input)==false) // <--- Error handling (alert user when insert CHAR) { System.out.println("\n Opss, wrong input"); System.out.print("\n # You choose \t\t: "); input = stdin.readLine(); } menus = Integer.parseInt(input); Mas.Menu(menus); System.out.print("\n # continuegi? ( 1= yes / 2= no ) : "); Ulang = Integer.parseInt(stdin.readLine()); if ( Ulang == 2) System.out.println(" * Bye *"); } } } static void Arahan() { System.out.println(" ======================"); System.out.println(" => menu : "); System.out.println(" ======================"); } static void Menu(int menu) // <-- pass the value into functiom { if(menu == 1) Jadual(); else if(menu == 2) System.out.println(" 2"); else if(menu == 3) System.out.println(" 3"); else System.out.print(" err0r weeeh!"); } static void Jadual() { System.out.println("\t\t\t========================================================================================="); System.out.print("\t\t\t========= \t================\t====================\t========================="); System.out.println("\n\t\t\t KOD\t\t MASA\t\t Destinasi\t\t\tHARGA"); System.out.println("\t\t\t========= \t================\t====================\t========================="); for(int p= 0;p<5;p++) { String dest[]={"TERENGGANU ","K.LUMPUR","PASIR MAS","PAHANG ","SEREMBAN"}; String time[]={"06:00PM-08.00AM","06:00PM-06.00AM","08:00AM-08:00PM","06:00PM-07:00PM","06:00PM-05:00AM"}; int price[] = {28,23,40,60,30,15,10,25,30,18}; int u=0; System.out.println(" "); System.out.println("\t\t\t=> ("+(p+1)+") - \t"+time[p]+"\t\tJOHOR >>> "+dest[p]+"\tDEWASA:RM"+price[p]+" @ KANAK2:RM"+price[p+5]); } System.out.print("\t\t\t========= \t================\t====================\t========================="); System.out.print("\n\t\t\t========================================================================================="); } static boolean isIntNumber(String num) // <-- Error handling { try { Integer.parseInt(num); } catch(NumberFormatException nfe) { return false; } return true; } }
here.. my quest:
i has main class
Java Code:public static void main( String args[])
*Just assume the user has already input the value that declare as LOL.
Java Code:getInput(LOL);
and..now inside the method is
Java Code:getInput(char GEEK) { System.out.print("LOL is : "+GEEK); } get
as snippet:
Java Code:getInput(char GEEK) { System.out.print("LOL is : "+GEEK); outInput(char GEEKv2); } outInput(char LoL2) { System.out.print("GEEK is :" +LoL2); get
Sorry for long explaination. I post it as the reference to all. maybe someday this thread will help the 0thers.
Anybody can answer this?
- 08-31-2010, 03:02 PM #2
bump..
any0ne can help me here ?
-
- 08-31-2010, 08:12 PM #4
hmmm.. i aready post what error. but it's ok.. my be its hard to explain.
here.. a snippet. try run this (in windows).
Java Code:import java.io.*; import java.lang.*; class LOL { public static void main (String args[] )throws IOException { int menus; String input; BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); { LOL Mas = new LOL(); // for (int Ulang = 1; Ulang == 1;) { Mas.Arahan(); //<--- calling the function System.out.print(" \t\t-1 timetable \n \t\t-2 Buy ticket \n \t\t-3 pay ticket\n"); System.out.print("\n # You choose \t\t: "); input = stdin.readLine(); while(isIntNumber(input)==false) // <--- Error handling (alert user when insert CHAR) { System.out.println("\n Opss, wrong input"); System.out.print("\n # You choose \t\t: "); input = stdin.readLine(); } menus = Integer.parseInt(input); Mas.Menu(menus); System.out.print("\n # continuegi? ( 1= yes / 2= no ) : "); Ulang = Integer.parseInt(stdin.readLine()); if ( Ulang == 2) System.out.println(" * Bye *"); } } } static void Arahan() { System.out.println(" ======================"); System.out.println(" => menu : "); System.out.println(" ======================"); } static void Menu(int menu)throws IOException // <-- pass the value into functiom { BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); { if(menu == 1) { Jadual(); } else if(menu == 2) { System.out.print(" Insert number here" ); // int numLOL = Integer.parseInt(stdin.readLine()); // <=== this part is OK System.out.print("Ok, y0u has enter : "+numLOL); // loadNow(numLOL); } else if(menu == 3) { System.out.println(" 3"); } else { System.out.print(" err0r weeeh!"); } } } loadNow(int X) { System.out.println(" X is numLOL ="+X); // <===== this is the problem part. it can't read the value pass from another method } static void Jadual() { System.out.println("\t\t\t========================================================================================="); System.out.print("\t\t\t========= \t================\t====================\t========================="); System.out.println("\n\t\t\t KOD\t\t MASA\t\t Destinasi\t\t\tHARGA"); System.out.println("\t\t\t========= \t================\t====================\t========================="); for(int p= 0;p<5;p++) { String dest[]={"TERENGGANU ","K.LUMPUR","PASIR MAS","PAHANG ","SEREMBAN"}; String time[]={"06:00PM-08.00AM","06:00PM-06.00AM","08:00AM-08:00PM","06:00PM-07:00PM","06:00PM-05:00AM"}; int price[] = {28,23,40,60,30,15,10,25,30,18}; int u=0; System.out.println(" "); System.out.println("\t\t\t=> ("+(p+1)+") - \t"+time[p]+"\t\tJOHOR >>> "+dest[p]+"\tDEWASA:RM"+price[p]+" @ KANAK2:RM"+price[p+5]); } System.out.print("\t\t\t========= \t================\t====================\t========================="); System.out.print("\n\t\t\t========================================================================================="); } static boolean isIntNumber(String num) // <-- Error handling { try { Integer.parseInt(num); } catch(NumberFormatException nfe) { return false; } return true; } }
then we run it.. we get err0r...
Java Code:Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved. D:\>javac LOL.java LOL.java:77: invalid method declaration; return type required loadNow(int X) ^ 1 error D:\>
- 08-31-2010, 09:06 PM #5invalid method declaration; return type required
Use "void" if nothing is returned.
- 09-01-2010, 05:06 AM #6
Member
- Join Date
- May 2010
- Posts
- 14
- Rep Power
- 0
because you call method loadNow(int x) from static method, so you must declare method loadNow(int x) as static method too and you must add return type of the method, like this :
Java Code:static void loadNow(int X) { System.out.println(" X is numLOL ="+X); // <===== this is the problem part. it can't read the value pass from another method }
Similar Threads
-
Unable to call paint() from inside method
By Y. Progammer in forum New To JavaReplies: 3Last Post: 03-01-2010, 01:47 PM -
JOptionPane when called from inside the paintComponent() method
By Y. Progammer in forum New To JavaReplies: 10Last Post: 02-28-2010, 02:52 PM -
how to call service method inside another service
By kirtichopra2003 in forum Web FrameworksReplies: 1Last Post: 10-24-2009, 03:19 AM -
Pass String[] into method in different class
By Swankee in forum New To JavaReplies: 19Last Post: 09-28-2009, 06:30 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 09:37 AM
Bookmarks