class to class

retern value from class to class

Page 1 of 1

4 Replies - 866 Views - Last Post: 17 January 2009 - 08:52 PM Rate Topic: -----

#1 jencalma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 13-July 07

class to class

Posted 17 January 2009 - 06:34 PM

the program that i was working on is that the user input a corresponding number for the direction, example if the user input 1 there's a message saying that "you are on step upward" and then the user will input again,, and it goes on, i uses to class, one for inputting ang the other one is for the direction
here's the program that i already made but i don't know how the user will input again..


//this is for inputting:

import java.io.*;

public class mymain
{
	public static void main(String[] args) throws IOException
	{
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		
		direction mymain = new direction;
		
		int input;
		string x;
		
		
		System.out.println("please choose:")
		System.out.printlng("1-up \n2-left \n3-down \n4-right")
		x = stdin.readline();
		input = Integer.parseInt(x);

		if (input==1)
		{
			mymain.up():
		}
		else if (input==2)
		{
			
			mymain.left():
		}
		else if (input==3)
		{
			
			mymain.down():
		}
		else 
		{
			
			mymain.right():
		}

	}
}




and this is for the direction:

public class direction
{
	public void up()
	{
		for(int ctr=0;ctr<5;ctr++)
		{	
			System.out.println("you are on step "+ctr upward);
			try
			{
				Thread.sleep(1000);
			}
			catch(InterruptedException ie)
			{
				System.out.println(ie.getMessage());
			}
		}
	}
}




the problem is that how can i make the user to input another number.. please help.. and thankyou in advance :D

Is This A Good Question/Topic? 0
  • +

Replies To: class to class

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5073
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: class to class

Posted 17 January 2009 - 07:11 PM

You just need to put a loop around the prompt and the if statements...

// Loop until we break
while (true) {
        // Don't forget to use semicolons at the end of print statements
        System.out.println("please choose:");

        // Remove the g that was here
        System.out.println("1-up \n2-left \n3-down \n4-right");

        // Java is case sensitive, it is readLine not readline
        x = stdin.readLine();
        input = Integer.parseInt(x);

        if (input==1)
        {
            // Semicolons, not colons on each command call
            mymain.up();
        }
        else if (input==2)
        {
           
            mymain.left();
        }
        else if (input==3)
        {
           
            mymain.down();
        }
        else if (input == 4)
        {
           
            mymain.right();
        }
        else { 
            // Break if not 1-4
            break; 
        }
}



You have lots of errors. Remember java is case sensitive so use proper capitalization. It is not readline, it is readLine. And it is not string, it is String. Also you were leaving off semicolons on some lines and others instead of a semicolon you had colons. Be sure they are SEMICOLONS.

Also, in your direction class you were not concatenating correctly....

// Wrong
System.out.println("you are on step "+ctr upward);

// Right
System.out.println("you are on step "+ ctr + " upward");




Lastly, when you create your direction object, don't forget the parenthesis.

// Wrong
direction mymain = new direction;

// Right
direction mymain = new direction();



Fix these problems and you will be up and running. Enjoy!

"At DIC we be number stepping code ninjas... we also put our left legs in and our left legs out, do the hokey pokey and turn code inside out... that is what we are all about!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 jencalma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 13-July 07

Re: class to class

Posted 17 January 2009 - 07:23 PM

View PostMartyr2, on 17 Jan, 2009 - 06:11 PM, said:

You just need to put a loop around the prompt and the if statements...

// Loop until we break
while (true) {
        // Don't forget to use semicolons at the end of print statements
        System.out.println("please choose:");

        // Remove the g that was here
        System.out.println("1-up \n2-left \n3-down \n4-right");

        // Java is case sensitive, it is readLine not readline
        x = stdin.readLine();
        input = Integer.parseInt(x);

        if (input==1)
        {
            // Semicolons, not colons on each command call
            mymain.up();
        }
        else if (input==2)
        {
           
            mymain.left();
        }
        else if (input==3)
        {
           
            mymain.down();
        }
        else if (input == 4)
        {
           
            mymain.right();
        }
        else { 
            // Break if not 1-4
            break; 
        }
}



You have lots of errors. Remember java is case sensitive so use proper capitalization. It is not readline, it is readLine. And it is not string, it is String. Also you were leaving off semicolons on some lines and others instead of a semicolon you had colons. Be sure they are SEMICOLONS.

Also, in your direction class you were not concatenating correctly....

// Wrong
System.out.println("you are on step "+ctr upward);

// Right
System.out.println("you are on step "+ ctr + " upward");




Lastly, when you create your direction object, don't forget the parenthesis.

// Wrong
direction mymain = new direction;

// Right
direction mymain = new direction();



Fix these problems and you will be up and running. Enjoy!

"At DIC we be number stepping code ninjas... we also put our left legs in and our left legs out, do the hokey pokey and turn code inside out... that is what we are all about!" :snap:



with the loop the user can input another number again?? tnx :D

View Postjencalma, on 17 Jan, 2009 - 06:20 PM, said:

View PostMartyr2, on 17 Jan, 2009 - 06:11 PM, said:

You just need to put a loop around the prompt and the if statements...

// Loop until we break
while (true) {
        // Don't forget to use semicolons at the end of print statements
        System.out.println("please choose:");

        // Remove the g that was here
        System.out.println("1-up \n2-left \n3-down \n4-right");

        // Java is case sensitive, it is readLine not readline
        x = stdin.readLine();
        input = Integer.parseInt(x);

        if (input==1)
        {
            // Semicolons, not colons on each command call
            mymain.up();
        }
        else if (input==2)
        {
           
            mymain.left();
        }
        else if (input==3)
        {
           
            mymain.down();
        }
        else if (input == 4)
        {
           
            mymain.right();
        }
        else { 
            // Break if not 1-4
            break; 
        }
}



You have lots of errors. Remember java is case sensitive so use proper capitalization. It is not readline, it is readLine. And it is not string, it is String. Also you were leaving off semicolons on some lines and others instead of a semicolon you had colons. Be sure they are SEMICOLONS.

Also, in your direction class you were not concatenating correctly....

// Wrong
System.out.println("you are on step "+ctr upward);

// Right
System.out.println("you are on step "+ ctr + " upward");




Lastly, when you create your direction object, don't forget the parenthesis.

// Wrong
direction mymain = new direction;

// Right
direction mymain = new direction();



Fix these problems and you will be up and running. Enjoy!

"At DIC we be number stepping code ninjas... we also put our left legs in and our left legs out, do the hokey pokey and turn code inside out... that is what we are all about!" :snap:



with the loop the user can input another number again?? tnx :D

Was This Post Helpful? 0
  • +
  • -

#4 jencalma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 13-July 07

Re: class to class

Posted 17 January 2009 - 07:50 PM

View Postjencalma, on 17 Jan, 2009 - 06:23 PM, said:

View PostMartyr2, on 17 Jan, 2009 - 06:11 PM, said:

You just need to put a loop around the prompt and the if statements...

// Loop until we break
while (true) {
        // Don't forget to use semicolons at the end of print statements
        System.out.println("please choose:");

        // Remove the g that was here
        System.out.println("1-up \n2-left \n3-down \n4-right");

        // Java is case sensitive, it is readLine not readline
        x = stdin.readLine();
        input = Integer.parseInt(x);

        if (input==1)
        {
            // Semicolons, not colons on each command call
            mymain.up();
        }
        else if (input==2)
        {
           
            mymain.left();
        }
        else if (input==3)
        {
           
            mymain.down();
        }
        else if (input == 4)
        {
           
            mymain.right();
        }
        else { 
            // Break if not 1-4
            break; 
        }
}



You have lots of errors. Remember java is case sensitive so use proper capitalization. It is not readline, it is readLine. And it is not string, it is String. Also you were leaving off semicolons on some lines and others instead of a semicolon you had colons. Be sure they are SEMICOLONS.

Also, in your direction class you were not concatenating correctly....

// Wrong
System.out.println("you are on step "+ctr upward);

// Right
System.out.println("you are on step "+ ctr + " upward");




Lastly, when you create your direction object, don't forget the parenthesis.

// Wrong
direction mymain = new direction;

// Right
direction mymain = new direction();



Fix these problems and you will be up and running. Enjoy!

"At DIC we be number stepping code ninjas... we also put our left legs in and our left legs out, do the hokey pokey and turn code inside out... that is what we are all about!" :snap:



with the loop the user can input another number again?? tnx :D

View Postjencalma, on 17 Jan, 2009 - 06:20 PM, said:

View PostMartyr2, on 17 Jan, 2009 - 06:11 PM, said:

You just need to put a loop around the prompt and the if statements...

// Loop until we break
while (true) {
        // Don't forget to use semicolons at the end of print statements
        System.out.println("please choose:");

        // Remove the g that was here
        System.out.println("1-up \n2-left \n3-down \n4-right");

        // Java is case sensitive, it is readLine not readline
        x = stdin.readLine();
        input = Integer.parseInt(x);

        if (input==1)
        {
            // Semicolons, not colons on each command call
            mymain.up();
        }
        else if (input==2)
        {
           
            mymain.left();
        }
        else if (input==3)
        {
           
            mymain.down();
        }
        else if (input == 4)
        {
           
            mymain.right();
        }
        else { 
            // Break if not 1-4
            break; 
        }
}



You have lots of errors. Remember java is case sensitive so use proper capitalization. It is not readline, it is readLine. And it is not string, it is String. Also you were leaving off semicolons on some lines and others instead of a semicolon you had colons. Be sure they are SEMICOLONS.

Also, in your direction class you were not concatenating correctly....

// Wrong
System.out.println("you are on step "+ctr upward);

// Right
System.out.println("you are on step "+ ctr + " upward");




Lastly, when you create your direction object, don't forget the parenthesis.

// Wrong
direction mymain = new direction;

// Right
direction mymain = new direction();



Fix these problems and you will be up and running. Enjoy!

"At DIC we be number stepping code ninjas... we also put our left legs in and our left legs out, do the hokey pokey and turn code inside out... that is what we are all about!" :snap:



with the loop the user can input another number again?? tnx :D and what will be the condition for the while statement?? :D and when will the user stop inputting? do ineed to put a statement after the else statement??

This post has been edited by jencalma: 17 January 2009 - 07:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 jencalma  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 89
  • Joined: 13-July 07

Re: class to class

Posted 17 January 2009 - 08:52 PM

thanks for the help,,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1