Why i cannot quit this while loop(java)?

I am new here and i got into trouble about my coding, i hope someone could help me break this loop, cause no matter what i type, it always in the loop.My code is as follow: boolean flag = true; while(flag) { System.out.println(“Please enter the movie’s title:”); title = s.nextLine().trim(); if(title.trim().equals(“”)) { System.out.println(“Please do not … Continue reading “Why i cannot quit this while loop(java)?”

Java Loop control (while)

int levelP1; int levelP2; do { p1.startTurn(p1, d1); p2.startTurn(p2, d1); levelP1 = p1.getLevel(); levelP2 = p2.getLevel(); } while (levelP1 <= 10 || levelP2 <= 10); if (levelP1 >= 10) { System.out.println(p1.getName() + ” hit LvL ” + levelP1 + ” and WON the game !”); } else if (levelP2 >= 10) { System.out.println(p2.getName() + ” … Continue reading “Java Loop control (while)”

Program bypasses while loop with multiple conditions (java)

I have this really simple code: public class ArrayIO { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println(“Enter a number from 50 to 150: “); int elements = sc.nextInt(); while(elements<50 && elements>150) { System.out.println(“Less than 50 or more than 150”); elements = sc.nextInt(); } sc.close(); ArrayList<Integer> integers = new ArrayList<>(); for(int … Continue reading “Program bypasses while loop with multiple conditions (java)”

how to rewrite while to Do while in java

i have rewritten this in a do while loop but how can i make the division part retun/accept just three decimal place for example is if i get 1/4 i should be able to enter 0.25 and also if i get 1/7 i should be able to get 0.14. import java.util.*; public class HelloWorld { … Continue reading “how to rewrite while to Do while in java”

How to restart while loop in order to restart game?

I want the game to restart when the player pressed the y key on the keyboard after dying or getting hit by the small ball. BoxX and BoxY are the coordinates for enemy balls Ball holds the rectangle or hitbox for all the enemy balls ReverseX and Y, IndexX and Y, and BallXchange and Y … Continue reading “How to restart while loop in order to restart game?”

Java ‘break’ statement sometimes not work, Why?

I have following method: public int quickFind (int[] nums, int lo, int hi) { if (lo >= hi) return -1; int gard = nums[lo]; int i = lo + 1, j = hi + 1; while (true) { while (nums[++i] < gard) { if (i == hi) { System.out.println(“break ” + i + ” / … Continue reading “Java ‘break’ statement sometimes not work, Why?”

How to stop infinite while loop

When the code below is true, meaning that when the first and last characters match it loops forever till I exit out the code, how do I fix it? while (true) { // Ignores case and checks if the firstLetters and lastLetters are the same if (firstLetters.equalsIgnoreCase(lastLetters)) { // if they are then print out … Continue reading “How to stop infinite while loop”

Creating new objects using a iterator in a while loop

Here is my code first: public static List<AnagramFamily> assignFamilies(List<Word> theOriginalList) { //Stores the list of anagram Families. List<AnagramFamily> anagramList = new LinkedList<AnagramFamily>(); //TempList to store the word objects. List<Word> tempList = new LinkedList<Word>(); ListIterator<Word> itr = theOriginalList.listIterator(); Word firstWord = theOriginalList.get(0); int cnt = 0; while(itr.hasNext()) { Word secondWord = itr.next(); if (firstWord.getMyCanonical().equals(secondWord.getMyCanonical())) { tempList.add(secondWord); … Continue reading “Creating new objects using a iterator in a while loop”

How to check if Scanner input is an Integer and if so, break from the loop

First off, I’m sorry if I am making a duplicate post. I tried looking for the solution and could not find it. I’m making a grade calculator where the user inputs a double “x” amount of times via a scanner. I’ve got the basic fundamentals of it down, and I’m not trying to fix any … Continue reading “How to check if Scanner input is an Integer and if so, break from the loop”