Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't make the inputted string get read in my Java program

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading