2016-03-08 90 views
1

我recenlty開始學習JAVA編程。我正在使用BlueJ編譯器。 任何人都可以告訴我或給我一個提示,如果數組沒有完全填充,如何使「System.out.println」不打印出空數組值(我的意思是空值)。僅打印填充陣列位置|在While循環中的Java

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(""); 

    } 

} 

}

+0

'如果(!變量= NULL){do_something;}'' –

+1

Country.equals( 「空」)'你可能想查找如何檢查,如果事情是空第一! – tnw

+0

請不要忽略java的命名約定。你的代碼很難理解。 – callOfCode

回答

2

誰能告訴我或者給我一個提示,如何讓 「的System.out.println」 沒有打印出空數組值

你的測試,索引位置的值是否爲空,並且僅打印(如果不爲空)。你需要考慮的是,你的代碼的其餘部分,特別是你的計數器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(""); 
    } 
+0

非常感謝你的隊友! – Helvijs

0

我不確定在你的代碼中你想使用這個,但是就像這樣,在你循環檢查你的循環變量是否不等於null。 例如:

if(variable != null) { 
    //do something 
}