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 23 of 23

Thread: help me plz with my code

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help me plz with my code

    hey guys

    I am a new member will you please help me with this question?
    Design a program that creates a virtual world you can “walk” around in, based on text. The user should be able to type in N,E,S,W to move North, East, South, and West.
    The world can be represented with a two dimensional array. Based on where the user wants to move, you can increment or decrement the first or second dimension of the array.
    Print the user’s position after each move, in X/Y coordinates

    here is the code which i made


    package chapt10;
    import java.util.Scanner;
    public class walk {

    /**
    * @param is
    */
    public static void main(int[][] is) {
    // TODO Auto-generated method stub

    main(new int [10][10]);
    Scanner sc = new Scanner (System.in);
    String choice = "Y";
    while (!choice.equalsIgnoreCase(choice))


    System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
    int currentX = 5;
    int currentY = 5;

    boolean quitGame = false;

    while (!quitGame)
    {
    System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
    System.out.println("Enter 'Q' to quit the game.");


    String userInput = sc.nextLine();

    if (userInput == "N")
    {
    currentY = currentY + 1; // move the player one unit north.

    }
    else if (userInput == "S")
    {
    currentX--; // move the player one unit south.
    }
    else if (userInput == "E")
    {
    currentY--; // move the player one unit east.
    }
    else if (userInput == "W")
    {
    currentX--; // move the player one unit west

    }
    else if (userInput == "Q")
    {

    quitGame = true;
    continue;
    }
    else
    {
    System.out.println("I didn't recongnize that comman,Please try again.");

    }
    System.out.println("Tou are at the position" + currentY + "," + currentY + ",");
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    What is the question?
    Please explain what your problem is. Show the error messages or show what the program does and what is wrong with its output.
    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    if (userInput == "N")
    A problem I see is that you are using == to compare Strings. You need to use the equals method to compare the contents of objects. == is for comparing primitives.

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me plz with my code

    thanks

    here is the copy

    package chapt10;
    import java.util.Scanner;
    public class walk {

    private static int currentx;

    /**
    * @param is
    */
    public static void maps(int[][] is) {
    // TODO Auto-generated method stub

    maps(new int [10][10]);
    Scanner sc = new Scanner (System.in);
    String choice = "Y";
    while (!choice.equalsIgnoreCase(choice))


    System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
    setCurrentx(5);
    int currentY = 5;

    boolean quitGame = false;

    while (!quitGame)
    {
    System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
    System.out.println("Enter 'Q' to quit the game.");


    String userInput = sc.nextLine();

    if (userInput == "N")
    {
    currentY = currentY + 1; // move the player one unit north.

    }
    else if (userInput == "S")
    {
    setCurrentx(getCurrentx() - 1); // move the player one unit south.
    }
    else if (userInput == "E")
    {
    currentY--; // move the player one unit east.
    }
    else if (userInput == "W")
    {
    setCurrentx(getCurrentx() - 1); // move the player one unit west

    }
    else if (userInput == "Q")
    {

    quitGame = true;
    continue;
    }
    else
    {
    System.out.println("I didn't recongnize that comman,Please try again.");

    }
    System.out.println("Tou are at the position" + currentY + "," + currentY + ",");
    }
    }

    public static int getCurrentx() {
    return currentx;
    }

    public static void setCurrentx(int currentx) {
    walk.currentx = currentx;
    }
    }




    and the console show me this message
    java.lang.NoSuchMethodError: main
    Exception in thread "main"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    There must be a main() method where the java program starts your program that is defined this way:
    public static void main(String[] args) {
    ...
    }
    That is the only place the java program knows to start your program.
    Last edited by Norm; August 14th, 2011 at 08:25 PM.

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me plz with my code

    is not this one

    public static void maps(int[][] is) {

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    Computers are very fussy about spelling and many other things. You must give it exactly what it wants.
    The String[] args parameters are where the java command will pass in commandline options that follow the class name when you start your program. The name MUST be main

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me plz with my code

    thank you so much

    here is the code
    package chapt10;
    import java.util.Scanner;


    public class walk {


    public static void main(int[][] is){

    main(new int [10][10]);
    Scanner sc = new Scanner (System.in);
    String choice = "Y,x";
    while (!choice.equalsIgnoreCase(choice))


    System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
    int currentx = 5;
    int currentY = 5;

    boolean quitGame = false;

    while (!quitGame)
    {
    System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
    System.out.println("Enter 'Q' to quit the game.");


    String userInput = sc.nextLine();

    if (userInput == "N")
    {
    currentY = currentY + 1; // move the player one unit north.

    }
    else if (userInput == "S")
    {
    }
    else if (userInput == "E")
    {
    currentY--; // move the player one unit east.
    }
    else if (userInput == "W")
    {

    }
    else if (userInput == "Q")
    {

    quitGame = true;
    continue;
    }
    else
    {
    System.out.println("I didn't recongnize that comman,Please try again.");

    }
    System.out.println("Tou are at the position" + currentY + "," + currentY + ",");
    }
    }

    }

    -------
    but why the " int currentx = 5; "
    is not correct

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    Please post the FULL text of any error messages here.
    Or explain what your problem is.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    You need to use the equals method to compare the contents of objects like Strings. The == operator is for comparing primitives.

  10. #10
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me plz with my code

    when I run it the eclpis says :

    java.lang.NoSuchMethodError: main
    Exception in thread "main

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    java.lang.NoSuchMethodError: main
    See post#4 and #6 about your missing main method.

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

    Default Re: help me plz with my code

    Did you read reply #4 by Norm?
    Improving the world one idiot at a time!

  13. #13
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me plz with my code

    yeah i got it and i change it to

    public static void main(String is){

    but when i run it still did not the result.........anyways thank you

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    It MUST BE EXACTLY as I posted in post#4 EXACTLY!!!

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

    Default Re: help me plz with my code

    :headdesk:
    Improving the world one idiot at a time!

  16. #16
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me plz with my code

    sorry guys I am not really good with java

    I made this code but i'm still having trouble with if else statement any idea?

    package chapt10;
    import java.util.Scanner;

    public class walk {

    public static void main(String[][] args ){

    Scanner sc = new Scanner (System.in);
    String choice = "Y";
    while (!choice.equalsIgnoreCase(choice))
    {

    System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
    int[][] currentY,currentx = new int[5][5];
    int currentY1 = 5;
    int currentx1 = 5;


    boolean quitGame = false;

    while (!quitGame)
    {
    System.out.println("Enter 'N' to move north, 'S' to move south, 'E' to move east, or 'W' to move west");
    System.out.println("Enter 'Q' to quit the game.");


    String userInput = sc.nextLine();

    if (userInput == "N") {
    currentY = currentY + 1;
    } else if (userInput == "S") {
    currentx = current + 1;
    } else if (userInput == "E") {
    currentY--;
    } else if (userInput == "W") {
    currentx--;
    } else if (userInput == "Q")
    {

    quitGame = true;
    continue;
    } else {
    System.out.println("I didn't recongnize that comman,Please try again.");
    }
    System.out.println("Tou are at the position" + currentY1 + "," + currentY1 + ",");
    }
    }
    }

  17. #17
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me plz with my code

    thank you guys here is the final


    package Assignment5;
    import java.util.InputMismatchException;
    import java.lang.Math;
    import java.util.Scanner;
    public class Assignment5 {
    static int x;
    static int y;
    static String[][] a2 = new String[10][10];
    public static void main(String[] args) {

    int x=5,y=5;
    for (int r=0; r<a2.length; r++)
    {
    for (int c=0; c<a2[r].length; c++)
    {
    a2[r][c]="0";
    }
    }
    for (int r=0; r<a2.length; r++)
    {
    int num;
    num= (int) Math.random()*10;
    for (int c=0; c<a2[r].length; c++)
    {

    if(num>=0&&num<=3)
    {
    a2[r][c]="1";
    }
    if(num>=4&&num<=7)
    {
    a2[r][c]="2";
    }
    if(num>=8&&num<=10)
    {
    a2[r][c]="0";
    }
    }
    }
    while(true)
    {
    Display.setText("\nEnter Direction: Type n for north, s for south, e for east, w for west : ");
    Scanner userInput = new Scanner(System.in);
    char direction;
    try
    {
    String Enter = userInput.next();
    direction= Enter.charAt(0);
    System.out.print("Enter the lowercase");
    direction = Enter.charAt(0);
    }
    catch(InputMismatchException e)
    {
    Display.setText("Error! Invalid Enter. Please Enter lowercase.");
    continue;
    }
    // print array in rectangular form

    switch( direction )
    {
    case 'n':
    y=y-1;
    if (y<0)
    {
    y=y+1;
    Display.setText("you cannot go beyond the limits!!!");
    }
    CallingMethod(x,y);
    a2[x][y]="a";

    break;
    case 's':
    y=y+1;
    if (y>9)
    {
    y=y-1;
    Display.setText("you cannot go beyond the limits!!!");
    }
    CallingMethod(x,y);
    a2[x][y]="a";

    break;
    case 'e':
    x=x+1;
    if (x>9)
    {
    x=x-1;
    Display.setText("you cannot go beyond the limits!!!");
    }
    CallingMethod(x,y);
    a2[x][y]="a";
    break;

    case 'w':
    x=x-1;
    if (x<0)
    {
    x=x+1;
    System.out.println("you cannot go beyond the limits!!!");
    }
    CallingMethod(x,y);
    a2[x][y]="a";
    break;

    }
    }
    }
    public static void CallingMethod(int x,int y)
    {
    Display.setText(" The direction is");
    Display.setText(" x position is: "+ x +" y position is: "+ y);
    if(a2[x][y].equalsIgnoreCase("1"))
    {
    Display.setText("You Found The Treasure!!!");
    }
    if(a2[x][y].equalsIgnoreCase("2"))
    {
    Display.setText("You Found The Jewel!!!");
    }
    if(a2[x][y].equalsIgnoreCase("0"))
    {
    Display.setText("OOPS nothing is there!!!");
    }
    for (int r=0; r< a2.length; r++)
    {
    for (int c=0; c<a2[r].length; c++)
    {
    Display.setText(" " + a2[c][r]);
    }
    Display.setText(" ");
    }
    }

    }

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    Since you didn't post any question, I guess you've solved it.

  19. #19
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help me plz with my code

    here is the question for the last code :

    Add a function to your program you made during class.
    This function should loop through each of the dimensions two arrays.
    Use Math.Rand to randomly select Yes or No.
    If yes, place something in that X,Y coordinate. It can be a gem, treasure, or something else of note. Use your imagineation.
    The end result should be a randomly populated X/Y coordinate ‘world

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    To get help with your code, you will have to post your code and your questions.

    Please wrap posted code in code tags. See: BB Code List - Java Programming Forums or Go Advanced and use the #icon

  21. #21
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: help me plz with my code

    hAI guise i be following this thread 4 a while now nd i gt same problem as matt.
    i dno wat i wrong i follow exacli wat u said 2 matt ye.

    public class HelloWod {
    public void naim static[int](){
    "hello wordl;"
    }
    }

    dno y it isnt worked. thanx.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,386
    Thanks
    49
    Thanked 2,208 Times in 2,181 Posts

    Default Re: help me plz with my code

    Sorry, I don't speak Welsh.

  23. #23
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: help me plz with my code

    Haha! <3
    Yeah it takes the greatest's of minds to be able to conquer excellent Welsh literature.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code