Nikolas Nikolas - 7 months ago 8
Java Question

Printing A String at the end of array

Sorry; I wasn't sure how to title this, so the question is a little shoddy.

I'm finishing up a small program for an extra credit assignment, and there are two very small details that are ruining it.

The purpose of the program is to read some data, divide it into a customer number and a flight number, then determine if the flight is suitable. I have an image screenshot of the file here, because it's a bit of a hassle to explain in short detail. Sorry for having to send a link.

Here is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class prog475a {
static Scanner inFile = null;

public static void main(String[] args) {
// make the flight plan array
boolean[][] flightPlan = new boolean[7][7];

// fill the true
flightPlan[1][2]=true;
flightPlan[1][3]=true;
flightPlan[1][6]=true;
flightPlan[2][1]=true;
flightPlan[2][3]=true;
flightPlan[2][6]=true;
flightPlan[3][4]=true;
flightPlan[4][2]=true;
flightPlan[4][5]=true;
flightPlan[4][6]=true;
flightPlan[5][2]=true;
flightPlan[5][4]=true;
flightPlan[6][2]=true;
flightPlan[6][5]=true;

// read for file
try {
// create scanner to read file
inFile = new Scanner(new File ("prog475a.dat"));
} catch (FileNotFoundException e) {
System.out.println("File not found!");
System.exit(0);
}

/*
* read customer number
* method to translate flight plan into coordinates
*
*/

int customer = 0;
int flight = 0;

while (inFile.hasNext()) {
customer = inFile.nextInt();
flight = inFile.nextInt();
translator(customer, flight, flightPlan);
}
}

public static void translator(int c, int f, boolean[][] fl) {
System.out.println("Customer Number " + c + "\tFlight Plan " + f);
// change int f into individual numbers?
int[] coo = new int[6];

int a = 10000;
// boolean ok = true;

for (int x = 0; x < coo.length - 1; x ++) {
coo[x] = (f / a) % 10;
a /= 10;
}

// test if your array has all the right numbers

// for (int x = 0; x < coo.length - 1; x++) {
//
// System.out.println(coo[x]);
//
// }

// instantiate variables to act as parameters to navigate boolean

int n = 0; // the actual coordinate
int p = 0; // placeholders
int q = 0;

while (q < coo.length) { // q has to end when it equals length of coo
p = coo[n];
q = coo[n + 1];

if (fl[p][q]) {
System.out.println(p + "\t" + q + "\t" + "Available");
n++;
} else {
System.out.println(p + "\t" + q + "\t" + "Unavailable\t Flight Plan invalid");
break; // if unavailable, break
}
}

System.out.println("");
}
}


Here is the data:

10123 13426
11305 62000
13427 42320
18211 34212
19006 65426
20831 52500
21475 32000
22138 13424
24105 65231
24216 34250
25009 43621


The issue I am having is very simple-- my program works perfectly fine, except I've tried various different ways to print "Flight Plan is Valid" at the end of the program to no avail. I've honestly done everything; I've added an extra if/else statement to account for when q = coo[coo.length - 1], tried to append it on the outside of the while-loop. I just can't get it to work.

Another issue I'm having is, for flight 34212, an extra line is arbitrarily printed out, indicating a flight from 2 to 0, which doesn't exist in the number at all. It's the only flight that's having this problem.

I'm really not sure what I'm doing wrong, and I hope that someone can give some advice on how to fix my code to make sure these errors are dealt with somewhat. Again, thank you for your time. I'm a bit of an arse, so I've spent a really long time trying to figure out what I was doing wrong, and I feel a bit frustrated.

Answer

Okay there are a lot of logical errors in the translator function, so I'll just list them and then provide some fixed code. However since this sounds like this is for a course, DO NOT JUST COPY AND PASTE. I think actually walking through some of the errors in your program would be highly beneficial.

  1. The length of coo is 6. This may be intentional, but is unnecessary.
  2. When you fill coo. You only fill 5 out of the 6 spots.
  3. You never change the ok variable.
  4. You never use the n variable.
  5. I think in the first part of the while loop, comparing with q is a typo/logical mistake, I think you meant to compare with n.
  6. Using both a check in the while loop for ok, and a break statement is redundant.
  7. You didn't add in an if to do run code depending on the state of ok.

With all these errors, I think that you should definitely ask your teacher or whatever with help, because this is a lot to change in one post. Either way, I hope you don't misuse this code ;). Here's the new translate function:

public static void translator(int c, int f, boolean[][] fl) {
    System.out.println("Customer Number " + c + "    Flight Plan " + f);

    int[] coo = new int[5];
    int a = 10000;

    for (int x = 0; x < coo.length; x++) {
        coo[x] = (f / a) % 10;
        a /= 10;    
    }

    for (int x = 0; x < coo.length - 1; x++) {
        int p = coo[x]; 
        int q = coo[x + 1];

        if (fl[p][q]) {
            System.out.print(p + "\t" + q + "\tAvailable");

            if (x == coo.length - 2) {
                System.out.print("\tFlight Plan is Valid");
            }

            System.out.println();
        }
        else {
            System.out.println(p + "\t" + q + "\tUnavailable\tFlight Plan is Invalid");
            break;
        }
    }
}

Explanation: I used basically the same code until the last for loop except that I changed the length of coo to 5 and made the first for loop run 5 times. You will also notice that I removed ok because it was redundant. This is because once I have a bad flight, I just print the invalid flight message and then break. There's no need of a variable in there. For the last loop you can see, I use a for loop instead of a while loop. Why did I do this? Pretty much just cause of taste. I typically use a for loop when I know how many times I am going to loop, and a while when the number isn't clear cut. Also, in this case, since you were using a counting variable n anyways, that's just the kind of thing a for loop is good for (haha). The rest of the code is pretty self-explanatory. If the flight is valid, you do the print for that. And if it's the last loop, hence if (x == coo.length - 2) (this checks that its the last loop), then you print that the flight plan is valid. Otherwise you print it's not valid and you break. I have done a little testing with this code, and it works so far. I hope this helps you!