Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Java (Eclipse) Numbers in Text File: 12345 33199 20500 10001 99950 00501 Can someone please help me with why this code is not working?

Using Java (Eclipse)

image text in transcribedimage text in transcribedimage text in transcribed

Numbers in Text File:

12345

33199

20500

10001

99950

00501

Can someone please help me with why this code is not working?

Please ask specific questions if you do not understand what I'm trying to do and not just (need more information).

**I am trying to verify ZIP Codes inputted into my tax 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 tax 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. **

-----------------------------------------------

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

//System.out.println(bar);

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.

*/

private static String calcBarCode(String zipCode1) {

return calcBarCode(0);

}

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

Scanner sc1 = new Scanner(new File("vaildZipCodes.txt"));

int i = 0;

while(sc1.hasNext()) {

vaildZipCodes[i++] = sc1.nextInt();

}

// Creating loop for what the user enters

boolean newValidator = true;

try(Scanner scanner = new Scanner(System.in)){

int zipCode; String barCode;

// User inputs

while(newValidator) {

System.out.print("Enter the zip code: ");

String zipCode1 = scanner.nextLine();

if(validator(zipCode1)) {

System.out.print("The barcode: " + barcodeMain.calcBarCode(zipCode1));

}

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

}

}

}

290 9 package Barcode; 100 import java.util.Scanner; 11 import java.io.IOException; 12 import java.io.File; 13 14 15 16 public class barcodeMain { 17 // A method called "aildater." that accepts an zin-code as its argument & returns a Boolean 18 static int vaildZipCodes (); 19e public static boolean validator(String acNm) { 20 for (int w = 0; W 0) { 52 switch (zip % 10) { 53 case 0: 54 barCode = "||:::" + barCode; 55 break; 56 case 1: 57 barCode = ":::||" + barCode; 58 break; 59 case 2: 60 barCode = "::1:1" + barCode; 61 break; 62 case 3: 63 barCode = "::||:" + barCode; 64 break; 65 case 4: 66 barCode = ":::" + barCode; 67 break; 68 case 5: 69 barCode = ":1:1:" + barCode; 70 break; 71 case 6: 72 barCode = ":||::" + barCode; 73 break; 74 case 7: 75 barCode = "|:::" + barCode; 76 break; 77 case 8: 78 barCode = "|::1:" + barCode; 79 break; 80 case 9: 81 barCode = "1:1::" + barCode; 82 break; 83 } } zip = zip / 10; } barCode = "1" + barCode + "T"; return barCode; } /* 83 84 85 86 87 88 896 90 91 92 93 94 95 96 97 98 996 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 * 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 bar.code. */ private static String calcBarCode(string zipCodel) { return calcBarCode(0); } public static void main(String arg[]) throws IOException Scanner sci = 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; try(Scanner scanner = new Scanner(System.in)){ int zipCode; String barcode: // User inputs while(newValidator) { System.out.print("Enter the zip code: "); String zipCode1 = scanner.nextLine(); if(validator(zipCode1)) { System.out.print("The barcode: + barcodeMain.calcBarCode(zipCode1)); } 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 **"); 36 01 Console X Scanner.class barcodeMain [Java Application] /Library/Java/JavaVirtual Machines/jdk Exception in thread "main" java.lang.NullPointerException at Barcode. barcodeMain.main(barcodeMain.java:104)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions