Home > Back-end >  Java: linked list - (boolean [head=true, tail false])
Java: linked list - (boolean [head=true, tail false])

Time:07-06

Condition 1: if you input more than or equal to 10 straight heads and are able to show in the input then the return is "Streak is found"

Condition 2: if you input less than 10 straight heads then the return is "Streak is broken"

However, I have a problem with condition 1 where it didn't execute to the output.

The code:

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

public class LinkedListProgram2
{    
public static void main (String [] args)
{
    Scanner input = new Scanner (System.in);
    LinkedList<String> cointoss = new LinkedList<String>();
    boolean head = true;
    boolean tail = false;
    boolean streak = true;
    int streakcount = 0;

    System.out.println ("Welcome to the Program #2 ");
    //ask for the boolean value. It can be head and tail or true and false.
    System.out.print ("\nEnter the boolean value (head=true, tail=false): ");
    
    for (int i = 0; i<18; i  )
    {
        cointoss.add(input.next());        
    }
    
    Iterator<String> it = cointoss.iterator();
    while (it.hasNext())
    {
        if(streakcount >= 10)
        {
            streak = true;
            System.out.println ("Streak is found! ");
            break;
        }
        else if(streakcount < 10)
        {
            streak = false;
            System.out.println ("Streak is broken! ");
            break;
        }
    }
}
}

CodePudding user response:

You miss something in your code, you need to check the input value, and increase streakcount, untested example code:

while (it.hasNext()) {
    String val = it.next();
    if (val.equals("true")) {
        streakcount  ;
        if (streakcount >= 10) {
            streak = true;
            System.out.println ("Streak is found! ");
            break;
        }
    }
    else if (val.equals("false")) {
        streak = false;
        System.out.println ("Streak is broken! ");
        break;
    }
}

There are more action to do, check different input value, or do you need to find streak if it is not from starting array...

CodePudding user response:

2 logic needs to be added atleast.

  1. Increment the streakcount when true is found

  2. Resetting the counter when false is found

You can have the outer if condition inside the while loop to print broken for every instance or let it be outside while to be printed at the end of loop

    while (it.hasNext()) {
        String val = it.next();
        if (val.equals("true"))
            streakcount  ;
        else
            streakcount = 0;
        
        if (streakcount >= 10) {
            streak = true;
            System.out.println("Streak is found! ");
            break;
        }
    }
    if (!streak) {
        System.out.println("Streak is broken! ");
    }

CodePudding user response:

According to your description, this is actually a small program that counts specific strings, and doesn't even need LinkedList I modified your code and now it should satisfy the two conditions you proposed

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        LinkedList<String> cointoss = new LinkedList<String>();
        boolean head = true;
        boolean tail = false;
        boolean streak = true;
        int streakcount = 0;

        System.out.println("Welcome to the Program #2 ");
        // ask for the boolean value. It can be head and tail or true and false.
        System.out.println("Enter the boolean value (head=true, tail=false): ");

        for (int i = 0; i < 18; i  ) {
            String next = input.next();
            if (next.equals("true") || next.equals("head")) {
                streakcount  ;
            }
            cointoss.add(next);
        }

        if (streakcount >= 10) {
            System.out.println("Streak is found! ");
        } else {
            System.out.println("Streak is broken! ");
        }
    }
  • Related