0

Heres the entire code, its basically reading input and asking for an integer until it is given. But I cannot seem to understand why after my loop, the scanner is not registering the call to rotate = scan.nextLine();?

import java.util.Scanner;


public class PJ{
public static void main(String []args){


Scanner scan = new Scanner(System.in);
String phrase;
String phraseMut;
int index =0 ;
boolean bool = false;
String rotate;


System.out.println("Enter your word or phrase: ");
phrase = scan.nextLine();
phraseMut = phrase;
System.out.println();


do {
    System.out.println("Enter an integer: ");
    //scan.nextLine();
    
    if (scan.hasNextInt()) {
        index = scan.nextInt();
        bool = true;

    }/*if (index > phraseMut.length()) {
    System.out.println("Index is out of bounds.");
    System.out.println("Please enter an integer value between 0 and 
    "+phraseMut.length());
    
    
    }*/else {
    bool = false;
    System.out.println("Error: Index is not Integer.");
    scan.nextLine();
    
}
}while (!bool);


System.out.println();
System.out.println("Rotating phrase to bring index "+index+" to the front. . .");


int count =0;

    for(int i = 0; i < index; i++){
        phraseMut = phraseMut.substring(1,phrase.length())+""+phraseMut.substring(0,1);
        count++;
        System.out.println(phraseMut);
    }   

System.out.println();

System.out.println("Do you want to rotate again? (yes/no)");
    rotate = scan.nextLine();
    rotate = rotate.toLowerCase();  
    
double placeholder = 0.0;   //will be average shifts per rotation


    
if( rotate.equals ("no")){
    System.out.println("\""+phrase+"\" rotated "+count+
        " times, final result \""+phraseMut+"\"");
    System.out.println("Average shifts per rotation: "+placeholder);
    
} 
if( rotate.equals("yes")){
    System.out.println("Enter the index of a character in your phrase: ");
    index = scan.nextInt();
    scan.nextLine();
    
    System.out.println();
    System.out.println("Rotating phrase to bring index "+index+" to the front. . .");
    
        for(int i = 0; i < index; i++){
        phraseMut = phraseMut.substring(1,phrase.length())+""+phraseMut.substring(0,1);
        count++;
        System.out.println(phraseMut);
    }   

    }   
    System.out.println();
    
    
    
    
}

}

specifically the block of code:

System.out.println("Do you want to rotate again? (yes/no)");
    rotate = scan.nextLine();
    rotate = rotate.toLowerCase();  

is not scanning and I do not understand why? There may be some very nuance thing that I am missing here but I would like to add that I am also very new to code and this specifically has been giving me trouble for a view days. Any input is appreciated.

L. Jacob
  • 37
  • 2
  • 1
    Does this answer your question? [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – maloomeister Nov 10 '21 at 07:11
  • In your `do-while` block, you do `nextInt()`, then set the `bool = true`, which means that the loop will be exited (the newline is not consumed up to this point), then the mentioned block of code is executed and the issue in the linked duplicate occurs. – maloomeister Nov 10 '21 at 07:12

0 Answers0