C++ to Java Conversion Help!

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2005 Views - Last Post: 11 March 2012 - 12:11 AM Rate Topic: -----

#16 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 10:05 PM

View PostAchri, on 10 March 2012 - 08:26 PM, said:

To change your C++ function over to Java, I would basically copy it all into your Java class, below your main method.

Then I would convert
string numberToBarcode(int num)
{ ... Do something }



into
public static String numberToBarcode(int num)
{...Do something}



I very quickly looked over the C++ function. So I would just see what other errors there are from that and fix them.


I dont think everything will work though because dont you use scanner for user input and not
cout << "Please enter a 5 digit zip code.\n"  
        << " --> ";  

Was This Post Helpful? 0
  • +
  • -

#17 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 10:09 PM

View PostavbFan07, on 10 March 2012 - 10:05 PM, said:

I dont think everything will work though because dont you use scanner for user input and not
cout << "Please enter a 5 digit zip code.\n"  
        << " --> ";  


Yes, Java does not use cout, look up System.out.print. The rest of the conversion process should be easy if you are making an attempt at learning Java.

System.out.println("Hello World"); is practically the first thing taught in Java.
Was This Post Helpful? 0
  • +
  • -

#18 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 10:36 PM

View PostAchri, on 10 March 2012 - 10:09 PM, said:

View PostavbFan07, on 10 March 2012 - 10:05 PM, said:

I dont think everything will work though because dont you use scanner for user input and not
cout << "Please enter a 5 digit zip code.\n"  
        << " --> ";  


Yes, Java does not use cout, look up System.out.print. The rest of the conversion process should be easy if you are making an attempt at learning Java.

System.out.println("Hello World"); is practically the first thing taught in Java.


Ya, thnx I finished the translation, could you please tell me if this is correct? I am having errors with the method names numberToBarcode():

My code:
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 = "|";
		
		System.out.println("Enter zip code: ");
		Scanner zip = new Scanner(System.in);
		z = zip.nextInt(); 
		
		if (z >= 10000 || z < 0)
		{
			System.out.println("Input Error: Input not a valid zip code");
		
		}
		tempNum = z;
		
		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;
		
		checkTotal = num1 + num2 + num3 + num4 + num5;
		checkNum = (10-(checkTotal % 10)) % 10;
		
		barcode += numberToBarcode(num1);
		barcode += numberToBarcode(num2);  
		barcode += numberToBarcode(num3);  
		barcode += numberToBarcode(num4);  
		barcode += numberToBarcode(num5);  
		barcode += numberToBarcode(checkNum);
		
		barcode += "|";
		
		System.out.println("Your zip code's barcode is: " + barcode);
		
		//return 0;
		
	}
	
public static void String numberToBarcode(int num){
	
	String barcode = " ";

	int dig;
	int tempNum;
	int bcTotal = 0;
	
	tempNum = num;
	
	if (tempNum >= 10){
		dig = tempNum % 10;
		tempNum /= 10;
		
		barcode = numberToBarcode(tempNum);
	}
	else{
		dig = tempNum;
	}
	tempNum = dig;
	
	if (dig == 0) {
		barcode += "||:::";
	}
	else{
		if (tempNum / 7 == 1 && bcTotal < 2){
			barcode += "|";
			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;
}

}




C++ Code:
string numberToBarcode(int);  
  
int main() {  
  
    int zip;  
    int num1, num2, num3, num4, num5, checkNum;  
    int tempNum;  
    int checkTotal;  
    string barcode = "|";   // initialized with a front framing bar  
  
    cout << "Please enter a 5 digit zip code.\n"  
        << " --> ";  
    cin >> zip; // prompt for zip code  
  
    // check if valid zip code  
    if (zip >= 100000 || zip < 0) {  
        cout << "Error: Not a zip code.\n";  
        return 0;  
    }  
  
    tempNum = zip;  
    //parsing each digit to determine check value  
    // the parsing is done in reverse order, so the digits are stored in reverse order  
    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;  
  
    // generate check num  
    checkTotal = num1 + num2 + num3 + num4 + num5;  
    checkNum = (10-(checkTotal % 10))%10;   // the last %10 is to deal with cases where checkTotal%10 == 0  
  
    // turn the zip into a 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 += "|";  
  
    cout << "\nYour zip code's barcode is:\n"  
        << " " << barcode << "\n";  
  
    return 0;  
}  
  
string numberToBarcode(int num) {  
  
    string barcode = "";  
    int dig;    // the single digit being converted  
    int tempNum;    // temporary variable  
    int /*bc7=0, bc4=0, bc2=0, bc1=0, bc0=0,*/ bcTotal=0;  
  
    tempNum = num;  
  
    if(tempNum >= 10) {  
        // if the digit is actually multiple digits, this function will  
        // recursively determine the multiple barcode sections they  
        // represent and return them together  
  
        // this strips the last digit...  
        dig = tempNum%10;  
        tempNum /= 10;  
        // then recursively strips each digit and adds their barcodes together  
        barcode = numberToBarcode(tempNum);  
    } else {  
        dig = tempNum;  
    }  
  
    // reusing the temp variable  
    tempNum = dig;  
  
    if (dig == 0) { // 0 is a special case  
        barcode += "||:::";  
    } else {    // otherwise...  
        if (tempNum / 7 == 1 && bcTotal < 2) {   // check for 1st bar - 7s digit  
            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;  
}


Was This Post Helpful? 0
  • +
  • -

#19 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 10:47 PM

You didn't say exactly what problem you were having. Show us the errors for more input.

My best guess at what you mean is you are overloading the method twice without receiving different types.


public static int numberToBarcode(int arg0)


public static void String numberToBarcode(int num)


Both receive an int, and the compiler doesn't know which to use.
Was This Post Helpful? 0
  • +
  • -

#20 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 10:52 PM

View PostAchri, on 10 March 2012 - 10:47 PM, said:

You didn't say exactly what problem you were having. Show us the errors for more input.

My best guess at what you mean is you are overloading the method twice without receiving different types.


public static int numberToBarcode(int arg0)


public static void String numberToBarcode(int num)


Both receive an int, and the compiler doesn't know which to use.


looking @ my code, and the C++ one, you can see where i try and create the methods, Those were my best guess as to how to translate the regular function creation, into a java method. The specific errors in Eclipse says for the first
public static int numberToBarcode(int arg0)
	{
		return arg0;
	}

"Duplicate method numberToBarcode(int) in type zipBar"

Then, here
public static void String numberToBarcode(int num){ }

It says "Duplicate method numberToBarcode(int) in type zipBar"

Finally,
barcode = numberToBarcode(tempNum);

Is saying "Type mismatch: cannot convert from int to String"

Now looking at those errors, and the way it should be according to the C++ version, what is the best way to fix this?
Was This Post Helpful? 0
  • +
  • -

#21 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 11:10 PM

Without looking at the code I can tell you the only way to fix it.
You cannot have multiple methods that are named the same that receive the same type specifier.

For example, this does not work:
public static String numberToBarcode(int num)
{...
//return some string
}

public static int numberToBarcode(int num)
{...
//return some int
}



Both of those methods above receive an 'int' in the parenthesis. Both are named the exact same method. To fix this, either rename one of the methods or add another parameter for 'overloading' the methods.

Lines 4-7 and lines and 61- are doing such a thing.

2ndly, something I didn't catch earlier was this

public static void String numberToBarcode(int num)



I don't know if I typed it out that way or you did, but you can't return a String AND be void. It either returns nothing,void, or it returns a String, but can't be specified both.
Was This Post Helpful? 0
  • +
  • -

#22 avbFan07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-March 12

Re: C++ to Java Conversion Help!

Posted 10 March 2012 - 11:20 PM

Thanks man I fixed the code with your suggestions and it got rid of the errors, but it is not quite doing what I want it too. I'd really appreciate it if you could keep helping me with this. The code converts zip codes to bar codes and vise-versa. For example, if I enter in: 95014, it should print
| | : | : : : | : | : | | : : : : : : | | : | : : | : : : | | |

My code is printing: Input Error: Input not a valid zip code
Your zip code's barcode is: |950141|

Could you help me debug this and see what the problem is?

View PostavbFan07, on 10 March 2012 - 11:18 PM, said:

Thanks man I fixed the code with your suggestions and it got rid of the errors, but it is not quite doing what I want it too. I'd really appreciate it if you could keep helping me with this. The code converts zip codes to bar codes and vise-versa. For example, if I enter in: 95014, it should print
| | : | : : : | : | : | | : : : : : : | | : | : : | : : : | | |

My code is printing: Input Error: Input not a valid zip code
Your zip code's barcode is: |950141|

Could you help me debug this and see what the problem is?

Sorry, here is my corrected Code:
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 = "|";
		
		System.out.println("Enter zip code: ");
		Scanner zip = new Scanner(System.in);
		z = zip.nextInt(); 
		
		if (z >= 10000 || z < 0)
		{
			System.out.println("Input Error: Input not a valid zip code");
		
		}
		tempNum = z;
		
		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;
		
		checkTotal = num1 + num2 + num3 + num4 + num5;
		checkNum = (10-(checkTotal % 10)) % 10;
		
		barcode += numberToBarcode(num1);
		barcode += numberToBarcode(num2);  
		barcode += numberToBarcode(num3);  
		barcode += numberToBarcode(num4);  
		barcode += numberToBarcode(num5);  
		barcode += numberToBarcode(checkNum);
		
		barcode += "|";
		
		System.out.println("Your zip code's barcode is: " + barcode);
		
		//return 0;
		
	}
	
public static String numberToBarcode1(int num){
	
	String barcode = " ";

	int dig;
	int tempNum;
	int bcTotal = 0;
	
	tempNum = num;
	
	if (tempNum >= 10){
		dig = tempNum % 10;
		tempNum /= 10;
		
		barcode = numberToBarcode1(tempNum);
	}
	else{
		dig = tempNum;
	}
	tempNum = dig;
	
	if (dig == 0) {
		barcode += "||:::";
	}
	else{
		if (tempNum / 7 == 1 && bcTotal < 2){
			barcode += "|";
			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;
}

}

Was This Post Helpful? 0
  • +
  • -

#23 Achri  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 52
  • Joined: 18-June 11

Re: C++ to Java Conversion Help!

Posted 11 March 2012 - 12:11 AM

Haha it's getting a bit late for me to start eyeing someone's code in depth.

Just doing a quick look at it. You are getting your input error because you didn't set the limits high enough.

z >= 10000



If I were to use 90210, that will be > 10000.

The best thing I can tell you for debugging the rest of your code is similar to the Chinese proverb:

Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.


Debugging your programs - If you right click to the left of the line numbers in Eclipse, you can toggle a breakpoint. If you set breakpoints throughout your code, it will pause the program at that point in time.

So set some of your breakpoints in between some of your control structures, such as before some of your if statements.
Run your program as Debug, by proessing the little bug icon. The difference is, being in the Debug perspective will let you see what your variable values are at each breakpoint. So you could see for example what the variable 'dig' is doing throughout the entire numberToBarcode method.

Using a little bit of systems diagnostics, you can start to narrow down where a variable is say going from 90210 to 902108.

Edit: You also might want to comment your code a little better. Many forum users will ignore your code, just because it's hard to follow along w/o comments. I probably over-comment my code, but for ending braces }, I always put something like:

...
}//end of class Foo constructor


This post has been edited by Achri: 11 March 2012 - 12:13 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2