Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    import java.util.Scanner;

    class Robot {

    public void speakreturn(String mainmenu) {
    System.out.println("What can I do for you?");
    }
    public void grab(String text) {
    System.out.println("Grabbing " + text + " now!");
    }

    public void jump(int height) {
    System.out.println("Jumping " + height + "m" + " high!");
    }

    public void move(String direction) {
    System.out.println("Moving " + direction);
    }


    }


    public class Robo {

    public static void main(String[] args) {

    Robot kush = new Robot();

    Scanner interactive = new Scanner(System.in);

    System.out.println("Hello, I'm Kush, What's your name?");

    String name = interactive.nextLine();

    System.out.println("Nice to meet you " + name +".");

    while (1>0) {


    String mainmenu = "What can I do for you " + name;

    kush.speakreturn(mainmenu);

    String action = interactive.nextLine();

    if (action == "move") {
    kush.move();
    }

    else if(action == "grab") {
    kush.grab();
    }

    else (action == "menu")
    kush.speakreturn(mainmenu);


    kush.action(grab);

    kush.action(speakreturn);

    int measurements = interactive.nextInt();

    kush.jump(measurements);


    }
    }
    }

    }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    Posting formatted code would help.
    Posting compiler error messages would help.
    Asking a specific question would help.
    Improving the world one idiot at a time!

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

  4. #4
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    Sorry about that! I had no clue. My question is why won't my program work when I want to be able to have a user input for an object to respond and after each response I wanted it to loop back to the "What can I do for you?" line. I can't tell where I went wrong. I get these errors (Lines 47, 51, 54): The method move(String) in the type Robot is not applicable for the arguments (), The method grab(String) in the type Robot is not applicable for the arguments (), The method menu(String) in the type Robot is not applicable for the arguments (). Also there is line 18 problem: The method println(String) in the type PrintStream is not applicable for the arguments (String, String).

    import java.util.Scanner;
     
    class Robot {
     
    	public void speakreturn(String mainmenu) {
    		System.out.println("What can I do for you?");
    	}
    	public void grab(String text) {
    		System.out.println("Grabbing " + text + " now!");
    	}
     
    	public void jump(int height) {
    		System.out.println("Jumping " + height + "m" + " high!");
    	}
     
    	public void move(String direction) {
    		System.out.println("Moving " + direction, " about " + distance + "m.");
    	}
     
     
    }
     
     
    public class Robo {
     
    	public static void main(String[] args) {
     
    		Robot kush = new Robot();
     
    		Scanner interactive = new Scanner(System.in);
     
    			System.out.println("Hello, I'm Kush, What's your name?");
     
    			String name = interactive.nextLine();
     
    			System.out.println("Nice to meet you " + name +".");
     
    			while (1>0) {
     
     
    				String mainmenu = "What can I do for you " + name;
     
    					kush.speakreturn(mainmenu);
     
    					String action = interactive.nextLine();
     
    					if (action == "move") {
    						kush.move();
    					}
     
    					else if(action == "grab") {
    						kush.grab();
    					}
     
    					else (action == "menu")
    						kush.speakreturn(mainmenu);
     
    			}
    			}
    		}
     
    	}

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    You're calling the methods wrong. The methods require a String argument, as in:

    kush.move( "up" );

    I have no idea what the argument should be, so refer to the API for the move() and other methods to determine that.

    Further. do not compare Strings using the '==' operator. Use the equals() method instead:

    if (action.equals( "move" ) )

    For your last error, note that the comma separates the arguments into two which the println() method does not expect. (See how the error messages make sense now?) If you want to include a comma character in the output, include it inside the quotes:

    System.out.println("Moving " + direction " , about " + distance + "m.");

  6. #6
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    Thank you very much! I have a lot to research!

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    I missed a '+' in my change to your statement:

    System.out.println("Moving " + direction + " , about " + distance + "m.");

  8. #8
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    I finally fixed most of the problems but I cannot fix the distance one, I moved the comma around but it still doesn't work. I am trying to have a (String, int). It's on line 20: The method println(String)in the type PrintStrea, is not applicable for the arguments (String, String). Also on line 41 I have the robot restart the loop from that point and I have a single statement of kush.speakreturn(mainmenu); but it comes out twice when it restarts the loop. Thanks in advance! I'm loving this!

     
    import java.util.Scanner;
     
    class Robot {
     
    	String text;
     
    	public void speakreturn(String mainmenu) {
    		System.out.println("What can I do for you?");
    	}
     
    	public void grab(String text) {
    		System.out.println("Grabbing " + text + " now!");
    	}
     
    	public void jump(int height) {
    		System.out.println("Jumping " + height + "m" + " high!");
    	}
     
    	public void move(String direction, int distance) {
    		System.out.println("Moving " + direction, "about" + distance + "m.");
    	}
     
    }
     
    public class Robo {
     
    	public static void main(String[] args) {
     
    		Robot kush = new Robot();
     
    		Scanner interactive = new Scanner(System.in);
     
    			System.out.println("Hello, I'm Kush, What's your name?");
     
    			String name = interactive.nextLine();
     
    			System.out.println("Nice to meet you " + name +".");
     
    			while (1>0) {		
     
    				String mainmenu = "What can I do for you " + name;
     
    					kush.speakreturn(mainmenu);
     
    					String action = interactive.nextLine();
     
    					if (action.equals("move")) {
     
    						System.out.println("Move which direction?");
     
    						String direction = interactive.nextLine();
     
    						System.out.println("How far should I move?");
     
    						int distance = interactive.nextInt();
     
    						kush.move(direction, distance);
    					}
     
    					else if(action.equals("grab")) {
     
    						System.out.println("What should I grab?");
     
    						String text = interactive.nextLine();
     
    						kush.grab(text);
    					}
     
    					else if(action.equals( "menu")) {
     
    						System.out.println("Returning to main menu.");
     
    					}		
     
     
    					else if(action.equals("jump")) {
     
    						System.out.println("How high?");
     
    						int height = interactive.nextInt();
     
    						kush.jump(height);
     
    					}
     
     
    			}
    		}
    }

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    See my correction above. (Post #7).

  10. #10
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Learning to code and this is the first thing I put together from what I learned, I don't know how to fix the problem!

    Oh man, lots of love and respect! Thanks for the quick responses too. So amazing! Thanks again Greg!

Similar Threads

  1. Don't know what statement to put
    By gotdatdough in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 25th, 2013, 09:43 AM
  2. Help with my code for homework assignment, I don't know how to fix it!
    By maxman190 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 19th, 2013, 03:01 AM
  3. I keep getting an error message but I don't know how to fix this!
    By mpagudelo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 12th, 2013, 01:56 AM
  4. [SOLVED] New to JAVA, problem with first code, need to know how to fix.
    By hrenfrow in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2013, 11:17 AM
  5. Replies: 1
    Last Post: June 9th, 2012, 11:29 AM