Holder Holder - 2 months ago 5
Java Question

Java Array Issues with adding in the Array and Sorting alphabetically

I am having some trouble figuring out how to sort the array as well as adding another name to the array. I am relatively new at coding java and I cant seem to figure it out. (The trouble is in choice 4 and enterTVshow() )
Any suggestions on how to approach this?
Thanks

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

public class JavaVision {
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
static int x;
static String name[] = new String[48];

public static void main(String[] args) throws IOException {

int choice;

do {
System.out.println(" ");
System.out.println("Java TV Program Menu");
System.out.println("----------------------------");
System.out.println("What would you like to do?");
System.out.println("1. Add a TV Show");
System.out.println("2. Modify a TV Show");
System.out.println("3. Delete a TV Show");
System.out.println("4. Sort TV Shows");
System.out.println("5. Show All TV Shows");
System.out.println("6. Exit");

choice = Integer.valueOf(in.readLine()).intValue();

if(choice ==1) {
enterTVshow();
}
if(choice ==2) {
modTVshow();
}
if(choice ==3) {
deleteTVshow();
}
if (choice ==4) {
Arrays.sort(name);

for (int i = 0; i < name.length; i++) {
}
}

if(choice ==5) {
System.out.println(Arrays.toString(name));
}

}while (choice !=6);
}

private static void deleteTVshow() throws IOException {
String deleteTVshow;
int flag = 0;
int i;
boolean found = false;

System.out.print("Which TV Show do you want to delete?");
deleteTVshow = in.readLine();

for(i = 0; i<=(x-1); i++) {
if(deleteTVshow.compareTo(name[i]) == 0) {
flag = x;
found = true;
}
}

if (found == false) {
System.out.println("There is no TV Show by that name.");
}
else {
for(i = flag; i <=(x-2); x++) {
name[i] = name[i+1];
}
x = x -1;
System.out.print("Type 'go back' to go back to the menu");
}while ((in.readLine().compareTo("go back")) !=0);

}

private static void modTVshow() throws IOException {
String modTVshow;
int flag = 0;
int i;
boolean found = false;

System.out.print("What is the TV Show you wish to modify?");
modTVshow = in.readLine();

for(i = 0; i<= (x-1); i++) {
if(modTVshow.compareTo(name[i]) == 0) {
flag = i;
found = true;
}
}
if (found == false) {
System.out.print("There is no TV show by that name.");
}
else {
System.out.print("Enter new Name: ");
name[flag] = in.readLine();
System.out.print("Type 'go back' to go back to the menu");
}while ((in.readLine().compareTo("go back")) !=0);
}

private static void enterTVshow() throws IOException {
x = 0;

do {
System.out.print("Enter TV Show Name:");
name[x] = in.readLine();
x++;
System.out.print("Adding Show.....");
System.out.println(" ");
System.out.print("Type 'go back' to go back to the menu ");

}while((in.readLine().compareTo("go back")) !=0);

}

}

Answer

You are facing problem in sorting because you might have some null value in any of 48 elements of array. to handle this just make your own comparator for sort like below or avoid null value in array.

    public class JavaVision {
    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    static int x;
    static String name[] = new String[48];

    public static void main(String[] args) throws IOException {

        int choice;

        do {
            System.out.println("     ");
            System.out.println("Java TV Program Menu");
            System.out.println("----------------------------");
            System.out.println("What would you like to do?");
            System.out.println("1. Add a TV Show");
            System.out.println("2. Modify a TV Show");
            System.out.println("3. Delete a TV Show");
            System.out.println("4. Sort TV Shows");
            System.out.println("5. Show All TV Shows");
            System.out.println("6. Exit");

            choice = Integer.valueOf(in.readLine()).intValue();

            if(choice ==1) {
                enterTVshow();
            }
            if(choice ==2) {
                modTVshow();
            }
            if(choice ==3) {
                deleteTVshow();
            }
            if (choice ==4) {
                sortArray(name);


            }

            if(choice ==5) {
                System.out.println(Arrays.toString(name));
            }

        }while (choice !=6);
    }

    private static void deleteTVshow() throws IOException {
        String deleteTVshow;
        int flag = 0;
        int i;
        boolean found = false;

        System.out.print("Which TV Show do you want to delete?");
        deleteTVshow = in.readLine();

        for(i = 0; i<=(x-1); i++) {
            if(deleteTVshow.compareTo(name[i]) == 0) {
                flag = x;
                found = true;
            }
        }

        if (found == false) {
            System.out.println("There is no TV Show by that name.");
        }
        else {
            for(i = flag; i <=(x-2); x++) {
                name[i] = name[i+1];
            }
            x = x -1;
            System.out.print("Type 'go back' to go back to the menu");
        }while ((in.readLine().compareTo("go back")) !=0);

    }

    private static void modTVshow() throws IOException {
        String modTVshow;
        int flag = 0;
        int i;
        boolean found = false;

        System.out.print("What is the TV Show you wish to modify?");
        modTVshow = in.readLine();

        for(i = 0; i<= (x-1); i++) {
            if(modTVshow.compareTo(name[i]) == 0) {
                flag = i;
                found = true;
            }
        }
        if (found == false) {
            System.out.print("There is no TV show by that name.");
        }
        else {
            System.out.print("Enter new Name: ");
            name[flag] = in.readLine();
            System.out.print("Type 'go back' to go back to the menu");
        }while ((in.readLine().compareTo("go back")) !=0);
    }

    private static void enterTVshow() throws IOException {
        System.out.println();
        x = 0;

        while(name[x]!=null){

            x++;
        }

        do {
            System.out.print("Enter TV Show Name:");
            name[x] = in.readLine();
            x++;
            System.out.print("Adding Show.....");
            System.out.println("     ");
            System.out.print("Type 'go back' to go back to the menu ");
            sortArray(name);
        }while((in.readLine().compareTo("go back")) !=0);

    }

    public static void sortArray(String [] myArray){

        Arrays.sort(myArray, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if (o1 == null && o2 == null) {
                    return 0;
                }
                if (o1 == null) {
                    return 1;
                }
                if (o2 == null) {
                    return -1;
                }
                return o1.compareTo(o2);
            }});
    }

}
Comments