JavaGuy1 JavaGuy1 - 1 month ago 5
Java Question

Removing First Element From Array

right now I have an ArrayList of Strings. The array is filled with data the user enters in the console. I am trying to remove the first element of the array, and then use the rest of the input. By the rest of the input I mean the input without the first element. I am trying to do this by using

nameofarray.remove(0);
but that does not seem to be working. Any help on removing the first element of the array and then getting the rest of the array is appreciated, thanks!

import java.util.ArrayList;
import java.util.Scanner;

class Driver {

public static void main(String[] args) {
Scanner k = new Scanner(System.in);
String userInput;
char msb;
int convertedBinary;
String removedMSB;
ArrayList<String> binaryInput = new ArrayList<String>();
System.out.println("Press 1 to use the Binary Converter: Press 2 to use the Decimal Converter:");
int userChoice = k.nextInt();

if (userChoice == 1) {
System.out.println("Welcome to the Binary Converter!");
System.out.println("Please enter a 10 bit binary number");
// Adding the input to the ArrayList
userInput = k.next();
msb = userInput.charAt(0);
binaryInput.add(userInput);

if (msb == '0') {
convertedBinary = Integer.parseInt(userInput, 2);
System.out.println("Your number in decimal is " + convertedBinary);
} else if (msb == '1') {
// Attempting to remove first element from ArrayList
binaryInput.remove(0);
convertedBinary = Integer.parseInt(userInput, 2);
System.out.println("Your number in decimal is " + convertedBinary);
}
}
}
}

Answer
binaryInput.add(userInput);

BinaryInput is an ArrayList of strings. You're adding the string userInput into binaryInput, so now binaryInput[0] is the string userInput.

binaryInput.remove(0);

This line will remove the first element of binaryInput- which is userInput. I assume you only want to remove the first character of userInput, not the entire string as you're doing in your code right now.

I would suggest doing something like this instead:

public static void main(String[] args) {
    Scanner k = new Scanner(System.in);
    String userInput;
    char msb;
    int convertedBinary;
    String removedMSB;
    ArrayList<String> binaryInput = new ArrayList<String>();
    System.out.println("Press 1 to use the Binary Converter: Press 2 to use the Decimal Converter:");
    int userChoice = k.nextInt();

    if (userChoice == 1) {
        System.out.println("Welcome to the Binary Converter!");
        System.out.println("Please enter a 10 bit binary number");
        // Adding the input to the ArrayList
        userInput = k.next();
        String withoutFirstChar = userInput .substring(1);
        msb = userInput.charAt(0);

        if (msb == '0') {
            convertedBinary = Integer.parseInt(userInput, 2);
            System.out.println("Your number in decimal is " + convertedBinary);
        } else if (msb == '1') {
            convertedBinary = Integer.parseInt(withoutFirstChar , 2);
            System.out.println("Your number in decimal is " +      convertedBinary);
        }
    }
}

}