Question: Java execution problems: - I'm having a problem printing the numbers in low to high (after convertion to binary) - Can't figure out to accept
Java execution problems:
- I'm having a problem printing the numbers in low to high (after convertion to binary)
- Can't figure out to accept the search numbers
- Any suggestions about problems with my code would be appreciated!
----------------------------
Prompt:
Read a file (BS.txt
) of integers into an array list so that it is sorted from lowest to highest. Using console input allow a user to enter a number to search for within the array list using a binary search using recursion (a must). Your code (.java file) and console screenshot showing the execution need to be provided for full credit. Your program will determine if the number is found or not found; if found, the index of the element it is in. The following numbers should be used in testing your program (all of these need to be turned in):
634
4
7532
534
2888
45654
555
----------------------------
My code:
package p4;
/*******************************
* Author: Danielle Coulter
* Title: p4_array_low_to_high.java
* Description: Recursion
* Material: Chapter 12
* Date: 11/16/2017
******************************
*/
/*
* This program will list the numbers of an array into a list
* from lowest to highest. This will be executed by:
* 1: convert the array into binary
* 2: sort the array from smallest to largest
* 3: show the index of a chosen number
*
* Conditions: The program must accept input from a Scanner
* as well as user input.
*/
import java.util.*;
import java.io.*;
public class p4_array_low_to_high {
public static void main(String[] args)
throws FileNotFoundException {
//Create scanner for file input
Scanner in = new Scanner(new File("BS.txt"));
//Create scanner for user/keyboard input
Scanner console = new Scanner(System.in);
//Scanner for keyboard input
//Needed?
Scanner keyboard = new Scanner(System.in);
//Greeting
System.out.println("Let's get started!");
int [] num = {634, 4, 7532, 534, 2888, 45654, 555};
while(in.hasNextInt()) {
int a = in.nextInt();
while(a 100000){
System.out.println("Invalid number, try again: ");
a = in.nextInt();
}//end inner while
printBinary(a);
System.out.println(" Enter next number, q to exit");
}//end outer while
//find minimum length
int small = num[0];
int large = num[0];
for (int i = 1; i
if (num[i]
small = num[i];
} else{
large = num[i];
}//end if/else
}//end for
System.out.println("The smallest of the numbers is: " + small);
System.out.println("The largest of the numbers is: " + large);
//broken
//find index
int find = keyboard.nextInt();
int indexOf = 0;
for (int i = 0; i
if(find == num[i]){
indexOf = i;
break;
}//end if
}//end for
System.out.println(indexOf);
}//end main ()
/*
* This method will convert the numbers from decimal
* to binary.
*/
private static void printBinary(int b){
//base case
if(b > 0){
printBinary(b/2);
//format results to show remainder
System.out.printf("%d", b%2);
}//end if
}//end printBinary()
}//end class
------------------------
BS.txt:
634
34
4
84657
7534
45
534
543
2887
245
45654
964
555
255
3934
732
4879
33
73
67
556
656
89746
342
345
453
345
3453
3453
673
6456
212
564
633
9767
334
546
903
34534
3453
722
4545
5689
234
267
94
235
224
3457
345
833
343
5632
66523
345
657
326
943
73
835
677
344
334
3590
342
8467
567
45
897
2345
844
872
4356
34723
553
73
563
62
68
837
735
3688
6356
5245
7252
2636
7823
63423
456
4567
456
773
346
734
456
4556
5343
456
336
46464
47
944
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
