hi ...I am tryin to do this methods homework...I have to have a system.println for a void method..I know u cannot have a return for void method..but can u have a system.println in the main method for the helper void method?

I don't know anyone called "u"...

But I suggest you think about where you can use System.println, as your understanding of how to use it seems to be lacking.

hi ...I am tryin to do this methods homework...I have to have a system.println for a void method..I know u cannot have a return for void method..but can u have a system.println in the main method for the helper void method?

public static void main(String[] args)
{
  System.out.println("Hello World");
}

And they said it couldn't be done. Shame on them.

i know that..i was asking for smth like this...

import java.util.Scanner
Public class..{

public static void main(...)
{

variable;
Scanner sc = new Scanner(System.in);

Switch(choice)
{

case1 :

System.out.print("ans" + mulTest());

}


public static void mulTest()
{
}
....something like this...N Thank you so replyin to my question...but u got to know i have no background in programmin and thus trying to get as much help as i could so that i could get my 1st year through...instead of tryin to make fun it would be helpful if u try to clear my doubts..Thank You!

i know that..i was asking for smth like this...

import java.util.Scanner
Public class..{

public static void main(...)
{

variable;
Scanner sc = new Scanner(System.in);

Switch(choice)
{

case1 :

System.out.print("ans" + mulTest());

}


public static void mulTest()
{
}

i don't think that would work. java is going to have a hernia about you trying to use "System.out.print("ans" + mulTest());". mulTest() is a void method, and the wayyou are using it there is as if it returns a string or someting. you can have mulTest() do something with a variable and then use that variable somewhere else, but you have to get the scope of the variable right(scope is where it would be visible in the application):

public class Test
{
	static int i = 2;  //you can use i throught the whole test class,
				//but only from where it was declared downwards.
	
	public static void main( String[] args )
	{
		runMethod();
		
		System.out.println( "i is equal to : " + i );
	}
	
	public static void runMethod()
	{
		i = i * 2;
		int p = 7;	//p can only be used in runMethod(), which
					//means that as soon as you close the method
					//with the squigly bracket, then you lose p
		
		i = i + p;
	}
}

void methods can do everything a normal method can, except return things.

That indeed won't work (not sure if it will give a compiler error or just result in nothing getting printed, never tried something like that). What you can do is use System.print() inside main to print part of the line, in the method to print something else, then use System.println() without arguments in main to generate the newline.
Not pretty, and I'd not recommend it for anything but throwaway experiments (I'd not use printing to console in general except in experimental code), but it works.

This article has been dead for over six months. Start a new discussion instead.