Hey guys, I need help finding out what is wrong with my code, I have no errors, but it is not doing what I want it to. I need my code to print a bar code when a user inputs a zip code. My program does not get to the method where it actually figures out what the bar code is. For example: when you enter 95014, you should get:
| | : | : : : | : | : | | : : : : : : | | : | : : | : : : | | |

but my program prints
|950141|

I think it is something I am not seeing with method placement, but I'm stumped.

import java.util.Scanner;

public class zipBar {
	public static int numberToBarcode(int arg0)
	{
		return arg0;
	}

	public static void main(String[] args) {
		
		int z;
		int num1, num2, num3, num4, num5, checkNum;
		int tempNum;
		int checkTotal;
		String barcode = "|"; //Puts front framing bar
		
		System.out.println("Enter zip code: ");
		Scanner zip = new Scanner(System.in);
		z = zip.nextInt(); 
		
		//Checks if inputted zip code is valid
		if (z >= 100000 || z < 0)
		{
			System.out.println("Input Error: Input not a valid zip code");
		
		}
		tempNum = z;
		//Parses each digit to check the value
		num5 = tempNum % 10;
		tempNum = tempNum / 10;
		
		num4 = tempNum % 10;
		tempNum = tempNum / 10;
		
		num3 = tempNum % 10;
		tempNum = tempNum / 10;
		
		num2 = tempNum % 10;
		tempNum = tempNum / 10;
		
		num1 = tempNum % 10;
		tempNum = tempNum / 10;
		
		//creates checkNum
		checkTotal = num1 + num2 + num3 + num4 + num5;
		// the last %10 is to deal with cases where checkTotal%10 == 0
		checkNum = (10-(checkTotal % 10)) % 10;
		//Turns zip into barcode digit by digit
		barcode += numberToBarcode(num1);
		barcode += numberToBarcode(num2);  
		barcode += numberToBarcode(num3);  
		barcode += numberToBarcode(num4);  
		barcode += numberToBarcode(num5);  
		barcode += numberToBarcode(checkNum);
		//Final framing bar.
		barcode += "|";
		
		System.out.println("Your zip code's barcode is: " + barcode);
		
		//return 0;
		
	}
	
public static String numberToBarcode1(int num){
	
	String barcode = " ";

	int dig; // the single digit being converted 
	int tempNum;//Temp Variable
	int bcTotal = 0;
	
	tempNum = num;
	
	 // if the digit is actually multiple digits, this function will  
    // recursively determine the multiple barcode sections they  
    // represent and return them together 
	if (tempNum >= 10){
		// this strips the last digit
		dig = tempNum % 10;
		tempNum /= 10;
		// then recursively strips each digit and adds their barcodes together
		barcode = numberToBarcode1(tempNum);
	}
	else{
		// reusing the temp variable 
		dig = tempNum;
	}
	tempNum = dig;
	//0 is a special Case
	if (dig == 0) {
		barcode += "||:::";
	}
	else{
		// check for 1st bar - 7s digit 
		if (tempNum / 7 == 1 && bcTotal < 2){
			barcode += "|";// if true, full bar
			tempNum -= 7;
			bcTotal++;
		}
		else  
            barcode += ":"; // if false, half bar  
        if (tempNum / 4 == 1 && bcTotal < 2) {   // check for 2nd bar - 4s digit  
            barcode += "|";  
            tempNum -= 4;  
            bcTotal++;  
        } else  
            barcode += ":";  
        if (tempNum / 2 == 1 && bcTotal < 2) {   // check for 3rd bar - 2s digit  
            barcode += "|";  
            tempNum -= 2;  
            bcTotal++;  
        } else  
            barcode += ":";  
        if (tempNum / 1 == 1 && bcTotal < 2) {   // check for 4th bar - 1s digit  
            barcode += "|";  
            tempNum -= 1;  
            bcTotal++;  
        } else  
            barcode += ":";  
        if (bcTotal < 2) {   // check for last bar - 0s digit - This one just takes up slack  
            barcode += "|";  
            bcTotal++;  
        } else  
            barcode += ":";
	}
	return barcode;
	
}

}

it is not doing what I want it to.

Where does the code create the barcode String? Add some printlns to that code to print out the values of the variables as they are set and changed.
The print out will show you what the code is doing. If you know what you want the code to do, then you will see where the code is going wrong.

Edited 4 Years Ago by NormR1: n/a

Where does the code create the barcode String? Add some printlns to that code to print out the values of the variables as they are set and changed.
The print out will show you what the code is doing. If you know what you want the code to do, then you will see where the code is going wrong.

I think it is not reaching line 64 where the second method is created. When i add a print line down at the bottom after that method it says code unreachable.

All the println calls must be inside of a method and before a return statement.

hmm alright, nothing prints, kind of like when your in an infinite while loop lol. Could you look at my methods, im not quite sure if I created those correctly.

nothing prints,

Did you put a println in ALL of the methods? You must have skipped some.

This article has been dead for over six months. Start a new discussion instead.