14 Replies - 1046 Views - Last Post: 22 December 2013 - 02:04 AM Rate Topic: -----

#1 ecinna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-December 13

Problem with invoking a method in another method

Posted 21 December 2013 - 08:42 PM

Hi, I have a problem in Java with invoking the method "handleInput" in the method "topLoop". The thing is that when you type something the message "Unknown command 'command'" should appear ('command' is the output from index 0 in a string-array) but when the input is empty I DON'T want the message to appear, the message should only appear once you actually write something. And when you type "quit" it will simply exit the program after you've confirmed with "y". Everything works except that when the input is empty the message still shows as "Unknown command ''" when it shouldn't appear at all, so how do I invoke the handleInput-method in the topLoop-method so that this will work?

In other words, I want the output to look something like this:

Welcome to the program.
Write ’quit’ to quit.
>>
>> g
Unknown command ’g’
>> add 1 2
Unknown command ’add’
>> quit
Write ’y’ if you really want to quit: y
Quitting program, goodbye!


Not like this:

Write ’quit’ to quit.
>>
Unknown command ’’

(...)


Here is the Java-code for handleInput:
public static void handleInput() {

        Scanner input = new Scanner(System.in);
        String command = input.nextLine();

        String[] split = command.split(" "); //Split string in array after space

        printLine("Unknown command '"+ split[0] +"'"); //Print out index 0 from array
        
    }



Here is the code for the topLoop-method:
 public static void topLoop() {
        
        while (true) //While true - loop forever
        {     
            print(">> ");

            Scanner input = new Scanner(System.in); //Create new scanner-input
            String answer = input.nextLine(); //Input for string-value

            if ("".equals(answer)) {
                continue;
            }
            else if (("quit".equals(answer)) && (confirm() == true)) { //If input is "quit" and confirmed with "y":
                break; //exit program
            }
handleInput(); //Invoke method
        }
    } 



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with invoking a method in another method

#2 salazar   User is offline

  • D.I.C Addict

Reputation: 105
  • View blog
  • Posts: 648
  • Joined: 26-June 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 09:10 PM

Use the length of the command variable as the condition of an if statement. Only execute the print message if the string is not equal to zero.
Was This Post Helpful? 0
  • +
  • -

#3 yashwanth.c.b   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 09:17 PM

if(input==null)
{
         //if the input is empty insert the error msg or whatever here
}


compare this with your handleinput function and insert this code at the appropriate place
Was This Post Helpful? 0
  • +
  • -

#4 ecinna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-December 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 09:52 PM

View Postsalazar, on 21 December 2013 - 09:10 PM, said:

Use the length of the command variable as the condition of an if statement. Only execute the print message if the string is not equal to zero.


I've modified the code like this in handleInput:
public static void handleInput() {

	Scanner input = new Scanner(System.in);
	String command = input.nextLine();

	String[] split = command.split(" "); //Split string in array after space

	if (split.length != 0) {
		printLine("Unknown command '"+ split[0] +"'"); //Print out index 0 from array
	}

}



And the topLoop is modified like this:
public static void topLoop() {

	while (true) //While true - loop forever
	{ 	
		print(">> ");
			
		Scanner input = new Scanner(System.in); //Create new scanner-input
		String answer = input.nextLine(); //Input for string-value
				
		if (("quit".equals(answer)) && (confirm() == true)) { //If input is "quit" and confirmed with "y":
			break; //exit program
			}
		handleInput();
	}
}



But when I execute the program by hitting return a couple of times I only get this output:


Welcome to the program.
Write ’quit’ to quit.
>>

Unknown command ''
>>

Unknown command ''
>>


Pressing return a couple of times should rather look this:


Welcome to the program.
Write ’quit’ to quit.
>>
>>
>>

Was This Post Helpful? 0
  • +
  • -

#5 yashwanth.c.b   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 10:08 PM

i

This post has been edited by yashwanth.c.b: 21 December 2013 - 10:18 PM

Was This Post Helpful? 0
  • +
  • -

#6 ecinna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-December 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 10:29 PM

View Postyashwanth.c.b, on 21 December 2013 - 10:08 PM, said:

i


i? ;)

No, I saw you first post but when I modify the topLoop code like this:
	public static void topLoop() {

		while (true) //While true - loop forever
		{ 	
			print(">> ");
			
			Scanner input = new Scanner(System.in); //Create new scanner-input
			String answer = input.nextLine(); //Input for string-value
				
			if (("quit".equals(answer)) && (confirm() == true)) { //If input is "quit" and confirmed with "y":
				break; //exit program
			}
			else if (answer == null) {
			topLoop();
			}
			handleInput(); //Invoke handleInput-method
		}
	}



But I still get this output:


Welcome to the program.
Write ’quit’ to quit.
>>

Unknown command ''
>>

Unknown command ''
>>

Was This Post Helpful? 0
  • +
  • -

#7 yashwanth.c.b   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 10:44 PM

:sweatdrop:/>
but when i compiled your first post it asked me to input twice and only then it outputted error message.
like this
>>a
b
unknown command b


why do you get 2 inputs
.i modified the post to i in fear that both yours and mine produced the same output

This post has been edited by yashwanth.c.b: 21 December 2013 - 10:45 PM

Was This Post Helpful? 0
  • +
  • -

#8 yashwanth.c.b   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 11:24 PM

victory ecinna!!
this code works fine.
this was the change
//this should be the 15th line of your toploop method in your first post.
if (!answer.isempty())
{
       handleinput();
}


this can also be applied for the command string in handleloop method.

This post has been edited by yashwanth.c.b: 21 December 2013 - 11:30 PM

Was This Post Helpful? 0
  • +
  • -

#9 ecinna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-December 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 11:33 PM

View Postyashwanth.c.b, on 21 December 2013 - 11:24 PM, said:

victory ecinna!!
this code works fine.
this was the change
//this should be the 15th line of your toploop method in your first post.
if (!answer.isempty())
{
       handleInput();
}



When I modified the code according to what you said I get an output like this:


Welcome to the program.
Write ’quit’ to quit.
>>
>>
>> Hello
Hello
Unknown command 'Hello'
>> Hello you

Unknown command ''
>>


So it still doesn't really work, but at least it seems to be closer to a solution. Also, I still have a problem with double inputs as you mentioned before. I don't know what's up with that though. :-S
Was This Post Helpful? 0
  • +
  • -

#10 yashwanth.c.b   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Problem with invoking a method in another method

Posted 21 December 2013 - 11:49 PM

Quote

Also, I still have
a problem with double inputs as you mentioned
before.

it is because that you are getting another input in handle input method although there is enough input in toploop method. it is absurd to do like this.
instead remove 3,4,5 lines in handleinput method.and modify like
handleinput(String command)
{
//what ever here
}


This post has been edited by yashwanth.c.b: 21 December 2013 - 11:50 PM

Was This Post Helpful? 0
  • +
  • -

#11 ecinna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-December 13

Re: Problem with invoking a method in another method

Posted 22 December 2013 - 12:05 AM

View Postyashwanth.c.b, on 21 December 2013 - 11:49 PM, said:

Quote

Also, I still have
a problem with double inputs as you mentioned
before.

it is because that you are getting another input in handle input method although there is enough input in toploop method. it is absurd to do like this.
instead remove 3,4,5 lines in handleinput method.and modify like
handleinput(String command)
{
//what ever here
}



I guess you mean that I should take a String as an argument for the handleInput-method, but when I do so I get an error message in the topLoop-method, where I'm trying to invoke the handleInput-method, saying "The method handleInput(String) in the type Interaction [name of the class] is not applicable for the arguments ()"

If it's easier to troobleshoot, here's is all the code I'm working with in the Intersection-class:

import java.util.Scanner;

public class Interaction {

	public static void main(String[] args) {

		printLine("Welcome to the program.\nWrite ’quit’ to quit."); //Use printLine-method for text-output

		topLoop(); //Invoke while-loop

		print("Quitting program, goodbye!"); //Text when program is closed

	}


	public static void print(String a) { //Create method with string-argument

		System.out.print(String.valueOf(a)); //Print out string without line break
	}

	public static void printLine(String B)/>/> {

		print(String.valueOf(B)/>/>+ "\n"); //Use print-method to add a line break after string-output

	}


	public static void topLoop() {

		while (true) //While true - loop forever
		{ 	
			print(">> ");
			
			Scanner input = new Scanner(System.in); //Create new scanner-input
			String answer = input.nextLine(); //Input for string-value
					
			if (("quit".equals(answer)) && (confirm() == true)) { //If input is "quit" and confirmed with "y":
				break; //exit program
			}
			else if (!answer.isEmpty()) { 

				handleInput(); //<---- Get the error message here

			}		
		}
	}


	public static boolean confirm() {

		print("Write 'y' if you really want to quit: "); //Confirm-text before exit

		Scanner input = new Scanner(System.in);
		String question = input.nextLine();

		if ("y".equals(question)) { //If input is "y":
			return true;
		}
		//Else:
		return false;

	}


	public static void handleInput(String command) {

		String[] split = command.split(" "); //Split string in array after space
		printLine("Unknown command '"+ split[0] +"'"); //Print out index 0 from array
		
	}

}//End class


Was This Post Helpful? 0
  • +
  • -

#12 ecinna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-December 13

Re: Problem with invoking a method in another method

Posted 22 December 2013 - 12:32 AM

I solved the problem! It finally works!! :-D At line 42 I simply wrote
handleInput(answer);



instead of:
handleInput();



But thanks for all the help, i.e. I didn't know that you could use isEmpty() as a condition in a if statement before.

Again, thanks for all the help! :-)
Was This Post Helpful? 0
  • +
  • -

#13 yashwanth.c.b   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Problem with invoking a method in another method

Posted 22 December 2013 - 01:00 AM

your welcome.so please upvote me. thats the only sugar i will get from you
Was This Post Helpful? 0
  • +
  • -

#14 ecinna   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 21-December 13

Re: Problem with invoking a method in another method

Posted 22 December 2013 - 01:30 AM

View Postyashwanth.c.b, on 22 December 2013 - 01:00 AM, said:

your welcome.so please upvote me. thats the only sugar i will get from you


I just gave you five stars, well earned! :)
Was This Post Helpful? 0
  • +
  • -

#15 yashwanth.c.b   User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 234
  • Joined: 07-July 13

Re: Problem with invoking a method in another method

Posted 22 December 2013 - 02:04 AM

thank you very much
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1