I am making a program that repeats a word for the length of characters that it has,
such if I were to write "Hello" it would repeat it 5 times. But, as I’m writing it I keep getting an error saying
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method nextLine() is undefined for the type WordLoop
inputString cannot be resolved
at WordLoop.main(WordLoop.java:11)
And due to my lackluster knowledge of Java, I have fallen flat on my face. So, any help would be appreciated.
Code:
import java.util.*;
class WordLoop
{
public static void main (String[] args )
{
int times;
//String string = new String();
Scanner scan = new Scanner(System.in);
System.out.println("Enter word: "); // Asks for the word
String string = nextLine();
while (string = inputString.length()) // loop until the end
{
System.out.println(string);
}
System.out.println("Done with the loop");
}
}
>Solution :
This might be what you are looking for:
import java.util.*;
class WordLoop
{
public static void main (String[] args )
{
//String string = new String();
Scanner scan = new Scanner(System.in);
System.out.print("Enter word: "); // Asks for the word
String string = scan.nextLine();
int count = 1;
while (count <= string.length()) // loop until the end
{
System.out.println(count + ": " + string);
count++;
}
System.out.println("Done with the loop");
}
}