Java code that takes user input (scanner) , converts the input into a linked lis
ID: 3597471 • Letter: J
Question
Java code that takes user input (scanner) , converts the input into a linked list, and check each element in linked list to see if the input was double, integer, string, and character with the generics method. Then, give a result of how many of the types were entered individually. Str= Int= Double= Char= Java code that takes user input (scanner) , converts the input into a linked list, and check each element in linked list to see if the input was double, integer, string, and character with the generics method. Then, give a result of how many of the types were entered individually. Str= Int= Double= Char= Str= Int= Double= Char=Explanation / Answer
Step 1: Created Custom linkedlist class and inner class for node creation
Step 2: Allowing user to enter the input through scanner
Step 3: Passing user input to linkedlist by calling custome add()
Step 4: Iterate all elements of linkedlist
Step 5: Passing linkedlist data to custom method calculateDataTypeCount()
Step 6 : Evaluate the datatype based on linkedlist data
Step 7: Printing the result based on datatype of data as expected
Source Code :
import java.util.Iterator;
import java.util.NoSuchElementException;
//custom linkedlist class and implements iterable
public class LinkedList<T> implements Iterable<T> {
// take head node
private Node<T> headNode;
public LinkedList() {
super();
this.headNode = null;
}
/**
* Custom add method to insert element in linkedlist
* @param data
*/
public void add(T data) {
// if headnode is null list is empty and consider element is first element
if (headNode == null) {
// create headnode and initialize data to it.
headNode = new Node<T>(data, null);
return;
}
// take one more pointer as tempnode to move forward while inserting next node.
Node<T> tempNode = headNode;
while (tempNode.next != null) {
tempNode = tempNode.next;
}
tempNode.next = new Node<T>(data, null);
}
/**
* @param headNode
* @return
*/
public T getNode() {
return headNode.data;
}
@Override
public Iterator<T> iterator() {
return new ListIterator<T>();
}
// custom list iterator used for iterate all elements which are present in list
public class ListIterator<T> implements Iterator<T> {
private Node<T> currentNode;
private Node<T> previous;
/**
* @param currentNode
*/
public ListIterator() {
super();
this.currentNode = (Node<T>) headNode;
this.previous = null;
}
// before iterate need to verify whether node is present or not
@Override
public boolean hasNext() {
if (currentNode != null && currentNode.next != null)
return true;
else
return false;
}
//it will return the node data when ever using for each to iterate values for list
@Override
public T next() {
if (!hasNext())
throw new NoSuchElementException();
if (previous == null) {
previous = currentNode;
return previous.data;
}
currentNode = currentNode.next;
return currentNode.data;
}
}
//Inner class for Node generic type
private static class Node<T> {
private T data;
private Node<T> next;
/**
* @param data
* @param next
*/
public Node(T data, Node<T> next) {
super();
this.data = data;
this.next = next;
}
}
}
import java.util.Scanner;
public class LinkedListDataTypeValidator {
// initialize variable for int data type
public static int intCount = 0;
// initialize variable for String data type
public static int stringCount = 0;
// initialize variable for double data type
public static int doubleCount = 0;
// initialize variable for char data type
public static int charCount = 0;
// start main method
public static void main(String[] args) {
// Read input data using scanner class
Scanner userInput = new Scanner(System.in);
// create custom linkedlist class and calling add method
LinkedList customLinkedList = new LinkedList();
// split command line input string by single space
String[] luserInput = userInput.nextLine().split(" ");
// iterate over data and passing to linked list
for (String data : luserInput) {
customLinkedList.add(data);
}
// Iterate linkedlist data and called calculatedatatypecount method to evaluate datatype count
for (Object data : customLinkedList) {
System.out.println(data + " Printed");
calculateDataTypeCount(data.toString());
}
// printing result based on datatype's
System.out.println("Int Count is :" + intCount);
System.out.println("Double Count is :" + doubleCount);
System.out.println("String Count is: " + stringCount);
System.out.println("Char Count is: " + charCount);
}
/**
* It take the linkedlist data and findout the datatype of data
* @param linkedListData
*/
private static void calculateDataTypeCount(String linkedListData) {
Scanner lDataType = new Scanner(linkedListData);
if (lDataType.hasNextInt()) {
//increase intcount if linkedlist data is int type
++intCount;
} else if (lDataType.hasNextDouble()) {
//increase doublecount if linkedlist data is double type
++doubleCount;
} else if (lDataType.hasNext() && linkedListData.toString().length() > 1) {
//increase string count if linkedlist data is string type and length more than 1
++stringCount;
} else if (lDataType.hasNext() && linkedListData.toString().length() == 1) {
//increase char count if linkedlist data is string type and length less than 1
++charCount;
}
}
}
Hopefully! I have solved your problem.. Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.