Question
I am trying to verify ZIP Codes inputted into my text file and then asked the user to input a ZIP Code. Then tell them
I am trying to verify ZIP Codes inputted into my text file and then asked the user to input a ZIP Code. Then tell them if it's a valid ZIP Code or not based on what's in my text file. I have created a loop to ask them to enter another one if they want to and have created a way to exit the code. If the zip code is valid, it needs to display the barcode and then ask if the user wants to enter another. I don't understand why this code is not working. Everything is working Except for the barcode output. for example, "12345 barcode => ::||::|:|::||::|::|:|:|:" and I'm only getting || for the output. Can someone please help with this last part of the code?
---------------------------------------------
package Barcode;
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
public class barcodeMain {
// A method called "vaildator" that accepts an zip-code as its argument & returns a Boolean
static int vaildZipCodes[];
public static boolean validator(String acNm) {
for (int w = 0; w vaildZipCodes.length; w++) {
if (Integer.parseInt(acNm) == vaildZipCodes[w]) {
return true;
}
}
// Must return false in order to check if number is valid
return false;
}
private static int calcZip(String barCode) {
int zip = 0;
for(int i = 1; i
String bar = barCode.substring(i,i+5);
if(bar.equals("||:::"))zip = 0 + zip*10;
if(bar.equals(":::||"))zip = 1 + zip*10;
if(bar.equals("::|:|"))zip = 2 + zip*10;
if(bar.equals("::||:"))zip = 3 + zip*10;
if(bar.equals(":|::|"))zip = 4 + zip*10;
if(bar.equals(":|:|:"))zip = 5 + zip*10;
if(bar.equals(":||::"))zip = 6 + zip*10;
if(bar.equals("|:::|"))zip = 7 + zip*10;
if(bar.equals("|::|:"))zip = 8 + zip*10;
if(bar.equals("|:|::"))zip = 9 + zip*10;
}
return zip;
}
private static String calcBarCode(int zip) {
String barCode = "";
while (zip > 0) {
switch (zip % 10) {
case 0:
barCode = "||:::" + barCode;
break;
case 1:
barCode = ":::||" + barCode;
break;
case 2:
barCode = "::|:|" + barCode;
break;
case 3:
barCode = "::||:" + barCode;
break;
case 4:
barCode = ":|::|" + barCode;
break;
case 5:
barCode = ":|:|:" + barCode;
break;
case 6:
barCode = ":||::" + barCode;
break;
case 7:
barCode = "|:::|" + barCode;
break;
case 8:
barCode = "|::|:" + barCode;
break;
case 9:
barCode = "|:|::" + barCode;
break;
}
zip = zip / 10;
}
barCode = "|" + barCode + "|";
return barCode;
}
/*
* Asking the user to enter an zip code and display if it is valid.
* If account number is incorrect, the program will ask to continue or not (y).
* If valid, program will give barcode.
*/
public static void main(String arg[])throws IOException{
vaildZipCodes = new int[200];
Scanner sc1 = new Scanner(new File("vaildZipCodes.txt"));
int w = 0;
while(sc1.hasNext()) {
vaildZipCodes[w++] = sc1.nextInt();
}
// Creating loop for what the user enters
boolean newValidator = true;
Scanner scanner = new Scanner(System.in);
String zipCode; int barCode = 0;{
// User inputs
while(newValidator) {
System.out.print("Enter the zip code: ");
zipCode = scanner.nextLine();
if(validator(zipCode)) {
System.out.println("The barcode: " + barcodeMain.calcBarCode(barCode));
}
else {
System.out.println("Not valid zip code number");
newValidator = true;
}
System.out.println(" Would you like to enter another account number? (y)");
String ans = scanner.nextLine();
if(ans.equals("n") || ans.equals("N")) {
newValidator = false;
}
}
System.out.println("** Program Exit **");
}
}
}
Console x Scanner.classStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started