## Forum Stats

• 3,727,629 Users
• 2,245,420 Discussions

Discussions

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Method call getting completely ignored.

Member Posts: 9
edited August 2017

Hello. I was making a program that converts decimal numbers to binary numbers.

And I made one method for each step. that is,

1. take the binary number and separate each digit, and then take each digit and add to an arraylist in the reverse order.(method name: Binary2ArrayList)

2.check each digit and if it is "1" add the a power of 2 equivalent to the column(Method name: Arraylist2Decimal)

Basically,

0.(input) 110

1.  1 1 0 -> 0 1 1

2.   [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768]

0  1  1

3. 2+4 = 6

result = 6

I know this is not the most optimal way to do it, but I'm doing it this way so i can practice more coding with a single project.

but what happened, is that my program completely ignores the "Arraylist2Decimal" method even after called.

I will be really grateful if someone could help me.

(Using netbeans IDE)

Code:

package javaapplication1;

import java.util.Scanner;

import java.util.ArrayList;

public class Binary2Decimal2Binary {

private static long binary1;

private static long binary2;

private final static ArrayList POWERSOF2 = new ArrayList<Integer>();

private final static ArrayList<Integer> SEPARATED2 = new ArrayList<Integer>();

private static int result1 = 0;

private static int result2 = 0;

private final static ArrayList<Integer> SEPARATED1 = new ArrayList<Integer>();

static public int getNthDigit(int number, int n) {

return (int) ((number / Math.pow(10, n - 1)) % 10);

}

static void binary2ArrayList(long binary1, long binary2){

String separating1 = String.valueOf(binary1);

for(int i = 1; i < separating1.length() + 1; i++) {

int j =(int) getNthDigit((int)binary1, i);

String separating2 = String.valueOf(binary2);

for(int x = 1; x < separating2.length() + 1; x++) {

int y =(int) getNthDigit((int)binary2,x);

}

System.out.println("Debugger 1 :" + SEPARATED1);

System.out.println("Debugger2 :" + SEPARATED2);

}

static void Arraylist2Decimal(){

for(int i = 1; i == SEPARATED1.size() + 1; i++) {

int intValue = (int)SEPARATED1.get(i);

int addition = (int) POWERSOF2.get(i);

if(intValue == 1){

}

System.out.println("Debugger 3:" + result1);

}

for(int k = 1; k == SEPARATED2.size() + 1; k++) {

int intValue = (int) SEPARATED2.get(k);

int addition = (int) POWERSOF2.get(k);

if(intValue == 1){

}

System.out.println("Debugger 4 :" + result2);

}};

public static void main(String[] args) {

System.out.println("Powers of 2:" + POWERSOF2);

Scanner scan = new Scanner(System.in);

System.out.println("Input first binary number.");

binary1 = scan.nextLong();

System.out.println("Input second binary number.");

binary2 = scan.nextLong();

scan.close();

binary2ArrayList(binary1,binary2);

Arraylist2Decimal();

System.out.println("First Number Converted:" + result1);

System.out.println("Second Number Converted:" + result2);

}}

input : 110

101

output :

Debugger 1 :[0, 1, 1]

Debugger2 :[1, 0, 1]

First Number Converted:0

Second Number Converted:0

(Note that "Debugger 3"println and "Debugger 4"println is not working.)

• Member Posts: 4,688 Gold Crown
edited August 2017

Why do you have a semicolon after the function body for Arraylist2Decimal()?

` }};`
• Member Posts: 1,380 Gold Trophy
edited August 2017

static void Arraylist2Decimal() {

for (int i = 1; i == SEPARATED1.size() + 1; i++) {

Here, i is initialized to 1.

the testing condition for the FOR loop to iterate is set to i == the size of SEPARATED1 plus 1. So only if the size of SEPERATED1 is 0 it will meet the condition to go into the loop.

Change it to

for (int i = 1; i <= SEPARATED1.size() + 1; i++) {

• Member Posts: 1,380 Gold Trophy
edited August 2017

for (int i = 1; i == SEPARATED1.size() + 1; i++) {

int intValue = (int) SEPARATED1.get(i);

Point - 1:

ArrayList indexing starts at 0.

If you loop starting at 1, you will be ignoring the first value in the arraylist.

Point -2 :

The loop is set to run until i == SEPARATED1.size() + 1, which is one index more than the size. This will throw an exception.

It should be

for (int i = 0; i <= SEPARATED1.size() + 1; i++) {

• edited August 2017
`but what happened, is that my program completely ignores the "Arraylist2Decimal" method even after called.I will be really grateful if someone could help me.(Using netbeans IDE)`

All you have to do is step through the code one line at a time and watch/examine what is happening.

If you need help using the debugger in NetBeans read the documentation and then post your question on a NetBeans forum.

• Member Posts: 9
edited August 2017

Thanks for all the help. Every single answer above was right(But now I'm not sure on what answer I should tag as the correct answer.). after implementing the changes, now my code works fine.

And specially to rp0428 for telling me about the debugger. If you didnt tell me about this, I would never know that the arraylist starts at 0 while its size starts counting at 1.

This discussion has been closed.