Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅打印While循环中的填充数组位置| Java_Java_Arrays - Fatal编程技术网

仅打印While循环中的填充数组位置| Java

仅打印While循环中的填充数组位置| Java,java,arrays,Java,Arrays,我最近开始学习JAVA编程。我正在使用BlueJ编译器。 谁能告诉我或给我一个提示,如果数组没有完全填充,如何使“System.out.println”不打印空数组值(我的意思是Null)值 static String [] Country = new String [15]; static String [] City = new String [15]; static String [] Region = new String [15]; static Scanner sc = new

我最近开始学习JAVA编程。我正在使用BlueJ编译器。 谁能告诉我或给我一个提示,如果数组没有完全填充,如何使“System.out.println”不打印空数组值(我的意思是Null)值

static String [] Country = new String [15];

static String [] City = new String [15];

static String [] Region = new String [15];
static Scanner sc = new Scanner(System.in);
static int x = 0, y = 0, z = 0;
static int a = 0, b=0,c=0, t=0;

public static void main (String [] args)
{
    String entry = "-1";
    while( !entry.equals ("4")){ 
    menu();
     entry = sc.nextLine();
    if(!entry.equals("1") && (!entry.equals("2")) && (!entry.equals ("3")) && (!entry.equals ("4") && !entry.equals("-1"))) 
     { JOptionPane.showMessageDialog(null, "Please Enter Numbers as shown in 'MENU LIST'");
        }
    if (entry.equals ("1")) {
        System.out.println("\f");
        AddCity();
    }
    if (entry.equals("2")) {
        System.out.println("\f");
        Search();
    }
    if (entry.equals("3")) {

        PrintAll();
    }
  }
  JOptionPane.showMessageDialog(null, "Have a nice day");
}

public static void menu()
{

    System.out.println("                   ---------------- Menu-----------------");
    System.out.println("                   Please Use Numbers ONLY from 1 - 4 ");
    System.out.println("                   1. Add new City");
    System.out.println("                   2. Search for a City.");
    System.out.println("                   3. Print All Cities.");
    System.out.println("                   4. QUIT.");                 
}

public static void AddCity()
{
    if(t >=13) {
        JOptionPane.showMessageDialog(null, "Database capacity is almost full.");
        PrintAll();
    }
    System.out.println("Please enter The name of Country.");
    Country [x]  = sc.nextLine();
    x++;
    System.out.println("Please enter the name of City.");
    City [y] = sc.nextLine();
    y++;
    System.out.println("Please enter the Region where the city is located.");
    Region [z] = sc.nextLine();
    z++;
    t++;
}

public static void Search()
{

}

public static void PrintAll()
{

    while (a <14)
    {
        if (!Country.equals("Null") ) {
          System.out.print("Country: ");
          System.out.print(Country[a]+ " | ");
          a++;
          while(b <(a)) {
            System.out.print("City: ");
             System.out.print(City[b]+ " | ");

             while(c<a) {
                System.out.print("Region: ");
                System.out.println(Region[c] + " | ");
                c++;
             }
            b++;
          }
          }
          System.out.println("");

   }

}
静态字符串[]国家=新字符串[15];
静态字符串[]城市=新字符串[15];
静态字符串[]区域=新字符串[15];
静态扫描仪sc=新扫描仪(System.in);
静态整数x=0,y=0,z=0;
静态整数a=0,b=0,c=0,t=0;
公共静态void main(字符串[]args)
{
字符串条目=“-1”;
而(!entry.equals(“4”){
菜单();
entry=sc.nextLine();
如果(!entry.equals(“1”)和(!entry.equals(“2”)和(!entry.equals(“3”)和(!entry.equals(“4”)和(!entry.equals(“1”))
{JOptionPane.showMessageDialog(null,“请输入“菜单列表”中显示的数字”);
}
如果(输入等于(“1”)){
System.out.println(“\f”);
AddCity();
}
如果(条目等于(“2”)){
System.out.println(“\f”);
搜索();
}
如果(条目等于(“3”)){
PrintAll();
}
}
showMessageDialog(null,“祝您愉快”);
}
公共静态无效菜单()
{
System.out.println(“--------------菜单----------------”);
System.out.println(“请仅使用1-4之间的数字”);
System.out.println(“1.添加新城市”);
System.out.println(“2.搜索城市”);
System.out.println(“3.打印所有城市”);
System.out.println(“4.QUIT”);
}
公共静态void AddCity()
{
如果(t>=13){
showMessageDialog(null,“数据库容量几乎已满”);
PrintAll();
}
System.out.println(“请输入国家名称”);
国家[x]=sc.nextLine();
x++;
System.out.println(“请输入城市名称”);
城市[y]=sc.nextLine();
y++;
System.out.println(“请输入城市所在地区”);
区域[z]=sc.nextLine();
z++;
t++;
}
公共静态无效搜索()
{
}
公共静态void PrintAll()
{
而
谁能告诉我或给我一个提示,如何制作“System.out.println”
不打印空数组值

您测试,索引位置上的值是否为空,仅打印,如果不是NULL。您需要考虑的是,您的代码的其余部分,特别是计数器A、B和C不在该条件下。

    while (a <14)
    {
        if (!Country.equals("Null") && country[a] != null) {
          System.out.print("Country: ");
          System.out.print(Country[a]+ " | ");
          a++;
          while(b <(a)) {
             if(City[b]!=null) {
                System.out.print("City: ");
                System.out.print(City[b]+ " | ");
             }
             while(c<a) {
                if(Region[c]!=null) {
                   System.out.print("Region: ");
                   System.out.println(Region[c] + " | ");
                }
                c++;
             }
            b++;
          }
      }
          System.out.println("");
   }

while(a我不确定在代码中的什么地方要使用它,但它是这样的,在循环中只需检查循环变量是否不等于null。
例:


if(variable!=null){do_something;}
!Country.equals(“null”)
您可能需要先查看如何检查某个内容是否为null。请不要忽略java的命名约定。您的代码难以阅读。可能存在重复的
if(variable != null) {
    //do something
}