1

I recenlty started to learn JAVA programming. I am using BlueJ compiler. Can anyone tell me or give me a hint, how to make "System.out.println" not print out empty array values (I mean Null) values if array is not filled compleatly.

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

   }

}

}

Helvijs
  • 160
  • 13
  • `if (variable != null) { do_something;}` – PM 77-1 Mar 08 '16 at 20:46
  • 1
    `!Country.equals("Null")` You might want to look up how to check if something is null first. – tnw Mar 08 '16 at 20:47
  • Please do not ignore java's naming convention. You code is confusing to read. – callOfCode Mar 08 '16 at 20:47
  • Possible duplicate of [How to check if array element is null to avoid NullPointerException in Java](http://stackoverflow.com/questions/425439/how-to-check-if-array-element-is-null-to-avoid-nullpointerexception-in-java) – PM 77-1 Mar 08 '16 at 20:49

2 Answers2

2

Can anyone tell me or give me a hint, how to make "System.out.println" not print out empty array values

You test, whether the value at the index position is null and only print, if not null. You need to consider, that the rest of your code, especially your counters a, b and c are outside of this condition.

    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("");
   }
Jankapunkt
  • 8,128
  • 4
  • 30
  • 59
0

I am not sure where in your code you want to use this but is like this, in side you loop just check if you loop variable is not equal to null. Ex:

if(variable != null) {
    //do something
}
acardoso
  • 68
  • 5