Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hello I am getting an error \"Can only iterate over an array or an instance of j

ID: 3710073 • Letter: H

Question

Hello I am getting an error "Can only iterate over an array or an instance of java.lang.Iterable" on line:

"for (String s : name )"

Im trying to add a name using linkedqueues through scanner in java.

- - - - - ----- - - - - - - - -- -

public class LinkedQueue

{

private class Node

{

String value;

Node next;

Node(String val, Node n)

{

value = val;

next = n;

}

}

private Node front = null;

private Node rear = null;

/** Enqueue Employee **/

public void enqueue(String s)

{

if(rear != null)

{

rear.next = new Node(s,null);

rear = rear.next;

}

else

{

rear = new Node(s,null);

front = rear;

}

}

public boolean empty()

{

return front == null;

}

/** Return the Employee in the front **/

public String peek()

{

if(empty())

throw new EmptyQueueException();

else

return front.value;

}

/** Dequeue Employee **/

public String dequeue()

{

if(empty())

throw new EmptyQueueException();

else

{

String value = front.value;

front = front.next;

if(front==null) rear = null;

return value;

}

}

public String toString()

{

StringBuilder sBuilder = new StringBuilder();

Node p = front;

while (p != null)

{

sBuilder.append(p.value + " ");

p = p.next;

}

return sBuilder.toString();

}

public void add(String employee) {

// TODO Auto-generated method stub

}

}

- - - - - -


public class EmptyQueueException extends RuntimeException {

}

- - - - - - - - -

import java.util.Scanner;

public class Main

{

public static void main(String [] args)

{

String name;

Scanner scan = new Scanner(System.in); //creates scanner

LinkedQueue queue = new LinkedQueue(); //declares linkedlist

/**Add names**/

System.out.println("Add a name:" );

name = scan.next();

queue.add(scan.nextLine());

queue.add(scan.nextLine());

queue.add(scan.nextLine());

System.out.println("Adding names:" );

for (String s : name )

{

System.out.print(s + " ");

queue.enqueue(s);

}

System.out.println(" State of queue is: ");

System.out.println(queue);

/**Remove names**/

System.out.println("Removing 2 names.");

queue.dequeue(); queue.dequeue();

System.out.println("State of queue is: ");

System.out.println(queue);

}

}

Explanation / Answer

Here is your solution:

Main.java

import java.util.LinkedList;

import java.util.Scanner;

public class Main

{

private static LinkedList<String> nameList = new LinkedList<String>();

public static void main(String[] args)

{

String name;

Scanner scan = new Scanner(System.in); // creates scanner

LinkedQueue queue = new LinkedQueue(); // declares linkedlist

/** Add names **/

System.out.println("Add a name:");

name = scan.next();

queue.add(name);

nameList.add(name);

name = scan.next();

queue.add(name);

nameList.add(name);

name = scan.next();

queue.add(name);

nameList.add(name);

System.out.println("Adding names:");

for (String s : nameList)

{

System.out.print(s + " ");

queue.enqueue(s);

}

System.out.println(" State of queue is: ");

System.out.println(queue);

/** Remove names **/

System.out.println("Removing 2 names.");

queue.dequeue();

queue.dequeue();

System.out.println("State of queue is: ");

System.out.println(queue);

}

}

Explanation:

Actually you attempting to iterate a String instead of an array, So, you cannot iterate a String variable .
You can itertate only collection of objects.So that i've added a collection class object to add all names then only we can iterate .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote