Results 1 to 6 of 6
  1. #1
    masokis's Avatar
    masokis is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Exclamation 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;
        }
    	
    }
    Acctualy this code are more longer.. but i shorted it here.

    here.. my quest:

    i has main class
    Java Code:
    public static void main( String args[])
    then i pass a value to a method.. for example, it pass char value X
    *Just assume the user has already input the value that declare as LOL.
    Java Code:
    getInput(LOL);
    thus code was insert into main..to pass the value

    and..now inside the method is
    Java Code:
    getInput(char GEEK)
    {
    System.out.print("LOL is : "+GEEK);
    }
    get
    Thus code are running without error..but if i pass the value method -> meth0d. sumthing goes wrong....
    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
    But...i get err0r...

    Sorry for long explaination. I post it as the reference to all. maybe someday this thread will help the 0thers.

    Anybody can answer this?

  2. #2
    masokis's Avatar
    masokis is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    bump..
    any0ne can help me here ?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by masokis View Post
    But...i get err0r...
    What error, where? Please post your actual error message and indicate which lines of code correspond to the error message line numbers

    Quote Originally Posted by masokis View Post
    bump..
    any0ne can help me here ?
    Please try to avoid bumps < 24 hrs after a post as we're all volunteers here.

  4. #4
    masokis's Avatar
    masokis is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    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;
        }
    	
    }
    at that snippet i also put comment, at where the problem part.
    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:\>

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default

    invalid method declaration; return type required
    When you define a method, java requires that you provide a type to be returned.
    Use "void" if nothing is returned.

  6. #6
    erakhman is offline Member
    Join Date
    May 2010
    Posts
    14
    Rep Power
    0

    Default

    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

  1. Unable to call paint() from inside method
    By Y. Progammer in forum New To Java
    Replies: 3
    Last Post: 03-01-2010, 12:47 PM
  2. Replies: 10
    Last Post: 02-28-2010, 01:52 PM
  3. how to call service method inside another service
    By kirtichopra2003 in forum Web Frameworks
    Replies: 1
    Last Post: 10-24-2009, 02:19 AM
  4. Pass String[] into method in different class
    By Swankee in forum New To Java
    Replies: 19
    Last Post: 09-28-2009, 05:30 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •