开发者

Printing Two Dimensional Array

I am working in a small task that allow the user to enter the regions of any country and store them in one array. Also, each time he enters a region, the system will ask him to enter the neighbours of that entered region and store these neighbours.

I did the 开发者_StackOverflow中文版whole task but I have small two problems:

  1. when I run the code, the program does not ask me to enter the name of the first region (This is happened only to the first region)
  2. I could not be able to print each region and its neighbours like the following format: Region A: neighbour1 neighbour2 Region B: neighbour1 neighbour2 and so on.

My code is the following:

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class Test6{

public static void main(String[] args) {

    Scanner kb = new Scanner(System.in);

    System.out.print("Please enter the number of regions: ");
    int REGION_COUNT = kb.nextInt();

    String[][] regions = new String[REGION_COUNT][2];
    for (int r = 0; r < regions.length; r++) {
        System.out.print("Please enter the name of region #" + (r+1) + ": ");
        String region = kb.nextLine();
        System.out.print("How many neighbors for region #" + (r+1) + ": ");
        if (kb.hasNextInt()) {
            int size = kb.nextInt();
            regions[r] = new String[size];
            kb.nextLine();
            for (int n = 0; n < size; n++) {
                System.out.print("Please enter the neighbour #"
                    + (n) + ": ");
                regions[r][n] = kb.nextLine();
            }
        } else System.exit(0);
    }

    for(int i=0; i<REGION_COUNT; i++){
        for(int k=0; k<2; k++){
            System.out.println(regions[i][k]);
            }
        }        
    }
}

Thanks everybody for your immediate helps. But let me explain to you what I want in more details.

First of all, I need to use the two dimensional array.

Secondly, my problem now is just with the printing of the result. I want to print the results like the following format:

> RegionA :  its neighbours

For example, let us take USA

> Washington D.C:  Texas, Florida, Oregon
--------------------------------------------------------------------------

Dear ykartal,

I used your program and it gave me the following:

Printing Two Dimensional Array


  1. when I run the code, the program does not ask me to enter the name of the first region (This is happened only to the first region)

Because you are using nextLine, program take your first edit for nextLine use next insteadof nextline

  1. could not be able to print each region and its neighbours like the following format: Region A: neighbour1 neighbour2 Region B: neighbour1 neighbour2 and so on.

A and B is the name or Alphabetic order of regions? If alphabetics 65 is the aSCII equalence of 'A' and 66 is 'B' ... So using (char)65 write A. Otherwise put region names instead of (char)65+i

Try this;

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;

public class Test6{

public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);

        System.out.print("Please enter the number of regions: ");
        int REGION_COUNT = kb.nextInt();
            String[] regionNames = new String[REGION_COUNT]; 
        String[][] regions = new String[REGION_COUNT][2];
        for (int r = 0; r < regions.length; r++) {
            System.out.print("Please enter the name of region #" + (r + 1)
                    + ": ");
            regionNames[r]  = kb.next();
            System.out
            .print("How many neighbors for region #" + (r + 1) + ": ");
            if (kb.hasNextInt()) {
                int size = kb.nextInt();
                regions[r] = new String[size];
                for (int n = 0; n < size; n++) {
                    System.out.print("Please enter the neighbour #" + (n)
                            + ": ");
                    regions[r][n] = kb.next();
                }
            } else
                System.exit(0);
        }

        for (int i = 0; i < REGION_COUNT; i++) {
            System.out.print(regionNames[i] +": ");
        for (int k = 0; k < 2; k++) {
            System.out.print(regions[i][k]+", ");
        }
        System.out.println();
        }
    }
}

Output;

Please enter the number of regions: 2
Please enter the name of region #1: aaa
How many neighbors for region #1: 1
Please enter the neighbour #0: a1
Please enter the name of region #2: bbb
How many neighbors for region #2: 2
Please enter the neighbour #0: b1
Please enter the neighbour #1: b2
aaa: a1
bbb: b1 b2

Note: This is not a good code, you must handle if user enter alfanumeric characters instead of numbers where you expected user will enter numeric values.


UPDATE:

This is what you want:

for (int i = 0; i < region.length; i++){
   StringBuilder sb = new StringBuilder();
   sb.append(region[i] + ": ");
   for (int i2 = 0; i2 < neighbor.length; i2++){
      if (i2 != 0 && i2 != neighbor.length-1){
         sb.append(", " + neighbor[i2]);
      }else{
         sb.append(neighbor);
      }
   }
   System.out.println(sb.toString());   
}

I think it's going to be easier for you if you manipulate your information in a Map.

Try this

Map<String, List<String>> mapRegionNeighbor = new HashMap<String, List<String>>();
System.out.println("What region would you like to see?");
String regionName = scanner.nextLine();

List<String> neighborList = mapRegionNeighbor.get(regionName);
for(String neighbor : neighborList){
  System.out.println(regionName + ": " + neighbor);
}

So, basically, if you want to add information to regions:

if it's a new region do this

mapRegionNeighbor.put(regionName, new ArrayList<String>);

if region has got a list of neighbor already, you'll do this to add a new neighbor:

List<String> neighborList = mapRegionNeighbor.get(regionName);
neighborList.add(neighborName);
mapRegionNeighbor.put(regionName, neighborList);

This 3 lines above will just update your region.

To run thru all values printing there neighbors, do something like this:

Iterator it = countedWords.entrySet().iterator();
while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry)it.next();
    for (String neighbor : pairs.getValue()){
        System.out.println(pairs.getKey() + ": " + neighbor);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜