Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java: tricky array (For me anyway!)

  • 29-11-2007 04:21PM
    #1
    Registered Users, Registered Users 2 Posts: 8,159 ✭✭✭


    Hey all I'm very new to Java and I'm having trouble with arrays. I can do basic arrays where I can display an index against a value. I have this particular program that displays distances between cities. I have got it work using nested if blocks but I want to get it to work using arrays. I'm not sure even where to start and the crappy Deitel&Deitel book I'm sure a lot of you are aware of is no help.

    Heres the original table I had to work with:
    DistanceTable.jpg

    This is how I got it to work using nested if blocks.
    import java.util.Scanner;
    
    public class Destination
      
    {
    
      private int destinationLocation;
      private int origin;
        
      public Destination()
      {}
      
      public void setDestinationLocation(int destLo)  
        { 
          destinationLocation = destLo;
        }         
    
        public int getDestinationLocation()
        {
          return destinationLocation;
        }
    
      public void setOrigin(int ogn)  
        { 
          origin = ogn;
        }         
    
      public int getOrigin()
        {
          return origin;
        }
        
      public void determineDistance()
        
      {
         Scanner input = new Scanner( System.in );
         
         System.out.print("Please select the origin of your trip: \n1. Belfast \n2. Cork \n3. Dublin \n4. Galway \n5. Limerick\n");
           origin=input.nextInt();
         
         System.out.print("Please select the destination of your trip: \n1. Belfast \n2. Cork \n3. Dublin \n4. Galway \n5. Limerick\n");
           destinationLocation=input.nextInt();
     
         if ( origin == 1 )
         {
          if ( destinationLocation == 2)
          System.out.println( "The distance between Belfast and Cork is 300 Kilometers"); 
          if ( destinationLocation == 3)
          System.out.println( "The distance between Belfast and Dublin is 150 Kilometers");
          if ( destinationLocation == 4)
          System.out.println( "The distance between Belfast and Galway is 200 Kilometers");
          if ( destinationLocation == 5)
          System.out.println( "The distance between Belfast and Limerick is 300 Kilometers");
         }  
         
         else if (origin == 2)
         {
          if ( destinationLocation == 1)
          System.out.println( "The distance between Cork and Belfast is 300 Kilometers"); 
          if ( destinationLocation == 3)
          System.out.println( "The distance between Cork and Dublin is 256 Kilometers");
          if ( destinationLocation == 4)
          System.out.println( "The distance between Cork and Galway is 150 Kilometers");
          if ( destinationLocation == 5)
          System.out.println( "The distance between Cork and Limerick is 75 Kilometers");
         } 
         
         else if (origin == 3)
         {
          if ( destinationLocation == 1)
          System.out.println( "The distance between Dublin and Belfast is 150 Kilometers"); 
          if ( destinationLocation == 2)
          System.out.println( "The distance between Dublin and Cork is 256 Kilometers");
          if ( destinationLocation == 4)
          System.out.println( "The distance between Dublin and Galway is 180 Kilometers");
          if ( destinationLocation == 5)
          System.out.println( "The distance between Dublin and Limerick is 220 Kilometers");
         } 
         
         else if (origin == 4)
         {
          if ( destinationLocation == 1)
          System.out.println( "The distance between Galway and Belfast is 200 Kilometers"); 
          if ( destinationLocation == 2)
          System.out.println( "The distance between Galway and Cork is 150 Kilometers");
          if ( destinationLocation == 3)
          System.out.println( "The distance between Galway and Dublin is 180 Kilometers");
          if ( destinationLocation == 5)
          System.out.println( "The distance between Galway and Limerick is 134 Kilometers");
         }   
         
         else if (origin == 5)
         {
          if ( destinationLocation == 1)
          System.out.println( "The distance between Limerick and Belfast is 300 Kilometers"); 
          if ( destinationLocation == 2)
          System.out.println( "The distance between Limerick and Cork is 75 Kilometers");
          if ( destinationLocation == 3)
          System.out.println( "The distance between Limerick and Dublin is 220 Kilometers");
          if ( destinationLocation == 4)
          System.out.println( "The distance between Limerick and Galway is 134 Kilometers");
         }        
              
        else 
          System.out.println( "Valid numbers not entered please try again" );
            
      }
    }
    

    public class DestinationTest
    
    {
    
      public static void main( String args[] )
        
      {
        
        Destination myDestination = new Destination();
        
        myDestination.determineDistance();
        
      }
      
    }
    


    Any pointers would be greatly appreciated


Comments

  • Registered Users, Registered Users 2 Posts: 901 ✭✭✭EL_Loco


    that image won't load here at work but in general terms

    you'll need a 2 dimensional array to replicate the table.
    you'll need to populate the array with the data.
    you'll need to ask the user the city, which will be a row in the array.
    you'll need to ask the user the destination city, which will be a column.
    These 2 "co-ordinates" should relate to a value in the 2D array which holds your answer.


  • Registered Users, Registered Users 2 Posts: 12,025 ✭✭✭✭Giblet


    Make a 2D array;


    int [][] distances = new int [origin][destinationLocation];

    origin and destination being the number of rows and columns

    so distances[0][1] would be 300; (origin 0 , destinationLocation 1)

    You can use this to populate and retrieve data.


  • Registered Users, Registered Users 2 Posts: 8,159 ✭✭✭youcancallmeal


    So would it be something like this:
    Destination[][] destinations = new Destination [4][4];
           
    destinations[0] = new Destination(300,150,200,300); //Belfast
    destinations[1] = new Destination(300,256,150,75);  //Cork
    destinations[2] = new Destination(150,256,180,220); //Dublin
    destinations[3] = new Destination(200,150,180,134); //Galway
    destinations[3] = new Destination(300,75,220,134);  //Limerick
    

    I tried compiling it but I'm getting errors saying
    cannot find symbol
    symbol  : constructor Destination(int,int,int,int)
    location: class Destination
    


  • Registered Users, Registered Users 2 Posts: 901 ✭✭✭EL_Loco


    Destination[][] destinations = new Destination [4][4];
    

    I think should be (based on above)
    int[][] destinations = new Destination [4][4];
    

    edit: I'm not sure about the way you're populating your array. but I found this code example which may help if you're having trouble:
    String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                                {"Smith", "Jones"}};
    


  • Registered Users, Registered Users 2, Paid Member Posts: 2,032 ✭✭✭lynchie


    I tried compiling it but I'm getting errors saying
    cannot find symbol
    symbol  : constructor Destination(int,int,int,int)
    location: class Destination
    

    The compiler is telling you whats wrong... It cant find a constructor in class Destination that takes 4 parameters...


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    If it was me I would create a TreeMap and use the names vs as keys. Example.
    Map<String, Integer> table = new TreeMap<String, Integer>(); 
    
    
    table.put("Cork-Dublin",  256);
    table.put("Belfast-Cork", 300);
    
    // To get the value then. 
    
    String from = "Cork";
    String to = "Dublin";
    
    int miles = table.get(from + "-" + to); // will get 256 miles.
    

    This will auto-sort the values, stop duplicates and allow you to quicky retrieve the keys quickly to populate your rows. to make it cleaner just have the from/to transpose themselves if the key isn't found.


  • Registered Users, Registered Users 2 Posts: 8,159 ✭✭✭youcancallmeal


    I left this for a while because it was wrecking my head. Anyway I came back to it and got it to work pretty quickly. Heres what I did:
    import java.util.Scanner;
    
    public class Destination1
      
    {
      
      private int destinationLocation;
      private int origin;
      
      public Destination1()
      {}  
      
      public void setDestinationLocation(int destLo)  
        { 
          destinationLocation = destLo;
        }         
    
        public int getDestinationLocation()
        {
          return destinationLocation;
        }
    
      public void setOrigin(int ogn)  
        { 
          origin = ogn;
        }         
    
      public int getOrigin()
        {
          return origin;
        }
    
        
        
      public void determineDistance()
        
      {
         Scanner input = new Scanner( System.in );
         
         System.out.print("Please select the origin of your trip: \n1. Belfast \n2. Cork \n3. Dublin \n4. Galway \n5. Limerick\n");
           origin=input.nextInt();
         
         System.out.print("Please select the destination of your trip: \n1. Belfast \n2. Cork \n3. Dublin \n4. Galway \n5. Limerick\n");
           destinationLocation=input.nextInt();
        
        int distance[][] = new int[5][5];
        
        distance[0][0]=0;
        distance[0][1]=300;
        distance[0][2]=150;
        distance[0][3]=200;
        distance[0][4]=300;
        distance[1][0]=200;
        distance[1][1]=0;
        distance[1][2]=256;
        distance[1][3]=150;
        distance[1][4]=75;
        distance[2][0]=150;
        distance[2][1]=256;
        distance[2][2]=0;
        distance[2][3]=180;
        distance[2][4]=220;
        distance[3][0]=200;
        distance[3][1]=150;
        distance[3][2]=200;
        distance[3][3]=0;
        distance[3][4]=134;
        distance[4][0]=300;
        distance[4][1]=75;
        distance[4][2]=300;
        distance[4][3]=135;
        distance[4][4]=0;
    
        
        System.out.printf ("\nThe distance between those two locations is: %dkm", distance[origin - 1][destinationLocation - 1]);
          
      }
    }
    

    I don't think I needed the set and get methods for DestinationLocation and Origin but I put them in anyway just out of habit.


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    While I still think my last post is the more elegant way to do it. If you really have to code it that way I suggest you change the initialisation of the data like so.
    int distance[][] = { 
    	{ 0,   300, 150, 200, 300 }, 
    	{ 200,   0, 256, 150,  75 }, 
    	{ 150, 256,   0, 180, 220 },
    	{ 200, 150, 200,   0, 134 },
    	{ 300,  75, 300, 135,   0 }
    	};
    

    It will make your code much easier to read. I would also make the counties as Enums.


Advertisement