I need help writing a java with NetBeans program that takes an input as an eight
ID: 3662830 • Letter: I
Question
I need help writing a java with NetBeans program that takes an input as an eight-bit binary number and prints the next 10 binary numbers, defining a binary number as int binNum[8]; I have convert the eight characters in line to eight integers in the binNum array and verify that each bit entered by the user is 0 or 1 and output an error message if it is not. If the user enters 11111101 the output to the console should be 11111101 11111110 11111111 00000000 00000001 00000010 00000011 00000100 00000101 00000110
I already have the interface in place so this is what i need to complete
public void actionPerformed(ActionEvent event) {
int[] binNum = new int[8];
String line = textField.getText();
if (line.length() == 8) {
// problem
} else {
System.out.printf("Enter exactly eight bits. ");
}
}
}
Also my professor said using binNum[i] = line.charAt(i) - '0' would be helpful but i am unsure how to use it
Explanation / Answer
public void actionPerformed(ActionEvent event) {
int[] binNum = new int[8];
String line = textField.getText(); //"11111101";
int num = 0;
if (line.length() == 8) {
// problemi
for(int i=0; i < line.length(); i++) {
if(line.charAt(i) == '0' || line.charAt(i)== '1') {
binNum[i] = line.charAt(i) - '0' ;
} else {
System.out.println("Invalid input");
break;
}
}
System.out.println("Starting Binary number: "+line);
//Now increment the binary number and print 10 next binary numbes
int carry = 0;
int sum = 0;
int rem = 0;
for(int j = 1; j < 10; j++) {
for(int i=binNum.length-1; i >=0 ; i--) {
if(i == binNum.length-1)
sum = binNum[i]+1; //increment unit bit
else
sum = binNum[i]+carry;
carry = sum / 2;
rem = sum % 2;
binNum[i] = rem;
}
for(int i=0; i < binNum.length; i++)
System.out.print(binNum[i]);
System.out.println();
}
} else {
System.out.printf("Enter exactly eight bits. ");
}
}
package assignment;
import java.awt.event.ActionEvent;
public class BinaryNumbers {
public static void main(String args[]) {
BinaryNumbers bn = new BinaryNumbers();
bn.actionPerformed(null);
}
public void actionPerformed(ActionEvent event) {
int[] binNum = new int[8];
String line = "11111101";//textField.getText();
int num = 0;
if (line.length() == 8) {
// problemi
for(int i=0; i < line.length(); i++) {
if(line.charAt(i) == '0' || line.charAt(i)== '1') {
binNum[i] = line.charAt(i) - '0' ;
} else {
System.out.println("Invalid input");
break;
}
}
System.out.println("Starting Binary number: "+line);
//Now increment the binary number and print 10 next binary numbes
int carry = 0;
int sum = 0;
int rem = 0;
for(int j = 1; j < 10; j++) {
for(int i=binNum.length-1; i >=0 ; i--) {
if(i == binNum.length-1)
sum = binNum[i]+1; //increment unit bit
else
sum = binNum[i]+carry;
carry = sum / 2;
rem = sum % 2;
binNum[i] = rem;
}
for(int i=0; i < binNum.length; i++)
System.out.print(binNum[i]);
System.out.println();
}
} else {
System.out.printf("Enter exactly eight bits. ");
}
}
// public int[] incrementBinary(int[] binNum) {
//
// }
}
---output-----
Starting Binary number: 11111101
11111110
11111111
00000000
00000001
00000010
00000011
00000100
00000101
00000110