Question

What is wrong with the following code? How would you fix it? (Assume all the appropriate...

What is wrong with the following code? How would you fix it? (Assume all the appropriate import statements)

/* print every other element in a linkedlist */

LinkedList<String> exampleLL = new LinkedList<String>();
exampleLL.add("Today");
exampleLL.add("is");
exampleLL.add("a");
exampleLL.add("sunny");
exampleLL.add("day");
exampleLL.add("outside");
​​​​​​​exampleLL.add("I");
​​​​​​​exampleLL.add("Like");
​​​​​​​exampleLL.add("summer");
​​​​​​​exampleLL.add("and");
​​​​​​​exampleLL.add("the");
​​​​​​​exampleLL.add("sunshine");
​​​​​​​exampleLL.add("it");
​​​​​​​exampleLL.add("brings");
​​​​​​​exampleLL.add("too;");
/* etc etc etc */

ListIterator iter = exampleLL.ListIterator();

System.out.println("Every other word of the speach:")

while (iter.hasNext()) {
System.out.println(iter.next().next());
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Iterator;
import java.util.LinkedList;

public class Itr {
   public static void main(String[] args) {
       /* print every other element in a linkedlist */

       LinkedList<String> exampleLL = new LinkedList<String>();
       exampleLL.add("Today");
       exampleLL.add("is");
       exampleLL.add("a");
       exampleLL.add("sunny");
       exampleLL.add("day");
       exampleLL.add("outside");

       /* etc etc etc */
       // listIterator is not available for LinkedList
       //only iterator is available
       Iterator iter = exampleLL.iterator();

       System.out.println("Every other word of the speach:");
       // we need to call only next on this
       while (iter.hasNext()) {
       System.out.println(iter.next());
       }


   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
What is wrong with the following code? How would you fix it? (Assume all the appropriate...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What is wrong with this Code? Fix the code errors to run correctly without error. There...

    What is wrong with this Code? Fix the code errors to run correctly without error. There are seven parts for one code, all are small code segments for one question. All the 'pets' need to 'speak', every sheet of code is connected. Four do need need any Debugging, three do have problems that need to be fixed. Does not need Debugging: public class Bird extends Pet { public Bird(String name) { super(name); } public void saySomething(){} } public class Cat...

  • Please help me fix my errors. I would like to read and write the text file...

    Please help me fix my errors. I would like to read and write the text file in java. my function part do not have errors. below is my code import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.FileWriter; import java.io.IOException; public class LinkedList {    Node head;    class Node    {        int data;        Node next;       Node(int d)        {            data = d;            next = null;        }    }    void printMiddle()    {        Node slow_ptr...

  • I need help with my code when I run my code running the wrong thing like...

    I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 {    static final int INITIAL_CAPACITY = 10;    public static void main (String[] args) throws Exception    {        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE        if (args.length < 1 )       ...

  • What is wrong with the following program? Explain how you will fix it in the code....

    What is wrong with the following program? Explain how you will fix it in the code. #include int main() { int i; int *ptr = &i; scanf("%d", &ptr); printf("The value of i is: %d\n", *ptr); return 0; }

  • What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping...

    What is wrong with my code? Trying to fix the Boolean situation and it keeps skipping my boolean question and repeating. import java.util.Scanner; public class MyTest{       public static void main (String [] args){               Scanner input = new Scanner(System.in);               System.out.println("JAVA QUIZ");        System.out.println("This quiz includes three questions about the Java Programming Language.");        System.out.println("Each question has 4 possible answers. (numbered 1,2,3,4)");        System.out.println("Enter 0 to exit the test.");...

  • Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding...

    Complete the following case study: Case study: Three implementations of contains - comparing approaches to adding functionality Set A contains Set B if every element in Set B is also in Set A. We will compare three ways to determine whether Set A contains Set B. Approach 1: Write code in the main method in a test class Add code to the main of ArraySetTester to create SetA and SetB, fill them with data and write a contains operation that...

  • [Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what...

    [Java] PLEASE FIX MY CODE I think I'm thinking in wrong way. I'm not sure what is wrong with my code. Here'e the problem: Write a class SemiCircle that represents the northern half of a circle in 2D space. A SemiCircle has center coordinates and a radius. Define a constructor: public SemiCircle(int centerX, int centerY, int theRadius) Implement the following methods: public boolean contains(int otherX, int otherY) returns true if the point given by the coordinates is inside the SemiCircle....

  • C++ program: can you help create a autocorrect code using the cpp code provided and the...

    C++ program: can you help create a autocorrect code using the cpp code provided and the words below using pairs, vectors and unordered map: Objectives To practice using C++ std::pair, std::vector, and std::unordered_map To tie together what we've learned into the context of a real-world application used by millions of people every day Instructions For Full Credit You're given a short list of words in known_words_short.txt that contains a handful of very different words. Assume this short list of words...

  • Java : I keep getting something wrong in my loadfile method, how do I fix my...

    Java : I keep getting something wrong in my loadfile method, how do I fix my code? Thank you. here is what is contained in the text file (WorldSeriesWinners.txt) 112 1903 Boston Americans 1904 No World Series 1905 New York Giants 1906 Chicago White Sox 1907 Chicago Cubs 1908 Chicago Cubs 1909 Pittsburgh Pirates 1910 Philadelphia Athletics 1911 Philadelphia Athletics 1912 Boston Red Sox 1913 Philadelphia Athletics 1914 Boston Braves 1915 Boston Red Sox 1916 Boston Red Sox 1917 Chicago...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT