JavaGuy1 JavaGuy1 - 1 year ago 108
Java Question

ArrayList Get Method

I have an ArrayList and I am trying to get the first element. I am using

when printed it prints the entire array, not the first element. I need to get the first element but I am confused as to why it is getting the entire array, not just the first element. Any help is appreciated, thanks!

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

public class Driver {

public static void main(String[] args) {
Scanner k = new Scanner(;
String msb;
String convertedBinary;
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
msb = binaryInput.get(0);



Answer Source

I don't think necessarily returns one character at a time so when you add it to the list it is adding a chunk of input. So you may have to manually split the text returned before inserting into the list.

"The method finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern." from here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download