Click here to Skip to main content
15,888,322 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    System.out.println("Hey! What's your name?");
    Scanner scanner = new Scanner(System.in);
    String name = scanner.nextLine();
    System.out.println("Hello " + name);
    System.out.println("Do you want to print your name n times?(y/n)");
    String yOrN = scanner.nextLine();
    if(yOrN=="y"){
      System.out.println("How many times do you want to print your name?");
      int times = scanner.nextInt();
      for (int i = 0; i < times; i++){   
        System.out.println(i);
      }
    }else if(yOrN=="n"){
      System.out.println("Alright");
    }
  }<pre lang="java">
}


What I have tried:

I dunno what to try cuz im new to java
Posted
Updated 5-Sep-20 5:34am

1 solution

Quote:
System.out.println(i);

Java
System.out.println(name);


It would work out as:
1. Ask name
2. Ask if wants to print
3. Ask n=no of times

Prints name n times.
 
Share this answer
 
Comments
Anurag Panda1 5-Sep-20 12:00pm    
So, should I write System.out.println(name);? also the program just stops after saying hello to the name pls help..
Anurag Panda1 5-Sep-20 12:01pm    
more specifically it asks if you want to print their names n times (y/n) and then stops for some reason
Sandeep Mewara 5-Sep-20 12:16pm    
Sounds like you are not providing the input as expected in the next line (scanner.nextLine())

Instead of taking input, I hardcoded the expected values and works fine for me. So Debug and see whats stopping.
Read: https://data-flair.training/blogs/read-java-console-input/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900