Hi there i am trying to write a java code that will essentially be like a gps but a lot simplier and i need to make a mini map about 4 by 3. where Dunada and Government is the first intersection and i have already done so by using multiple nested if statements, now i know that there is a much better and easier solution, i was wondering if you could help please!!

This is what i have done so far,

String[] streetNamesX={"Dundas","Younge","University"};
String [] streetNamesY = {"Government","Elm","Alder","Rockland"};

    public  void takeuser()
    {    
    Moving move;;
    move = new Moving();


    String user = "";
    String user2 = "";

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please enter where you would like to go (X enter) (Y enter): "); 
    try {
    user = reader.readLine();  
    user2 = reader.readLine();}     
    catch (IOException e) {
    e.printStackTrace(); }
    System.out.print("You entered : " + user + " and " +user2);
     String OrignalchoiceX = user;
    for (String j: streetNamesY)    {
    for (String i: streetNamesX){
    if (user.equalsIgnoreCase(i) && user2.equalsIgnoreCase(j)){
    System.out.println(" and your destination is set to "+i + " and "+j);
    }
    } }



    if (user2.equals(streetNamesY[0]) ){   
        if (user.equals(streetNamesX[0])){
    }
        else if (OrignalchoiceX.equals(streetNamesX[1])){   
            move.moveDown();
            System.out.println("To return from this location to your origin ");
            move.moveUp();
    }
        else if (OrignalchoiceX.equals(streetNamesX[2])){   
            move.moveDown();
        move.moveDown();
        System.out.println("To return from this location to your origin ");
            move.moveUp(); move.moveUp();
    }
    }    

    else if (user2.equals(streetNamesY[1]) ){   
            if (OrignalchoiceX.equals(streetNamesX[0])){
                move.moveforward();
                System.out.println("To return from this location to your origin");
                move.moveback();
    }
            else if (OrignalchoiceX.equals(streetNamesX[1])){   
                move.moveforward();
            move.moveDown();
            System.out.println("To return from this location to your origin");
                move.moveback();
                move.moveUp();
    }
            else if (OrignalchoiceX.equals(streetNamesX[2])){   
                move.moveforward();
            move.moveDown();
            move.moveDown();
            System.out.println("To return from this location to your origin");
                move.moveback();
                move.moveUp(); move.moveUp();
    }
    }       
    else if (user2.equals(streetNamesY[2])){
            if (OrignalchoiceX.equals(streetNamesX[0])){
                move.moveforward();
            move.moveforward();
            System.out.println("To return from this location to your origin");
                move.moveback(); move.moveback();
            }
            else if (OrignalchoiceX.equals(streetNamesX[1])){   // Johnson
                move.moveDown();
                move.moveforward();
            move.moveforward();
            System.out.println("To return from this location to your origin");
                move.moveback();move.moveback();
                move.moveUp();
    }
            else if (OrignalchoiceX.equals(streetNamesX[2])){   
                move.moveDown();
            move.moveDown();  
            move.moveforward();
            move.moveforward(); 
            System.out.println("To return from this location to your origin");
                move.moveback();
                move.moveback();
                move.moveUp();move.moveUp();} }

    else if (user2.equals(streetNamesY[3])){
    if (OrignalchoiceX.equals(streetNamesX[0])){
            move.moveforward();
        move.moveforward();
        move.moveforward();
        System.out.println("To return from this location to your origin");
            move.moveback();
            move.moveback();move.moveback();
            }
        else if (OrignalchoiceX.equals(streetNamesX[1])){   
            move.moveDown();
            move.moveforward();
        move.moveforward();
        move.moveforward();
        System.out.println("To return from this location to your origin");
            move.moveback();
            move.moveback();move.moveback();
            move.moveUp();
    }
        else if (OrignalchoiceX.equals(streetNamesX[2])){   
            move.moveDown();
        move.moveDown();  
        move.moveforward();
        move.moveforward();
        move.moveforward();
        System.out.println("To return from this location to your origin");
            move.moveback();
            move.moveback();
            move.moveback();
            move.moveUp();move.moveUp();} }
    }

    }

where the move.moveback() or move.forward() is just a print statement saying move in that direction. i would like to use hashsets, or anykind of suggestion on how to improve my code would help,

thanks

Line 25, do you expect user to be on streetNamesX and user2 to be on streetNamesY only? If that's the case, then this if statement would be OK.

Though, you have gone through the loop but not keep the index. In other words, you are using a wrong loop format because you need to save the index values, not just go through for checking... Also, user and user2 are independent to each other, so nested loop will be overkilled. What you should do is as follows...

int xCoor=-1, yCoor=-1;
for (int i=0; i<streetNamesX.length; i++) {
  if (user.equalsIgnoreCase(streetNamesX[i])) {
    xCoor = i;
    break;
  }
}  // end check for X
for (int i=0; i<streetNamesY.length; i++) {
  if (user2.equalsIgnoreCase(streetNamesY[i])) {
    yCoor = i;
    break;
  }
}  // end check for Y

// after the loop, xCoor & yCoor should be greater than -1 if
// it found, so the inputs are valid
if (xCoor>=0 && yCoor>=0) {
  // deal with valid coordinate
}
else {
  // invalid input, ignore or handle it.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.