Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
You are given a word "Medha". You can speak this word repeatedly only upto N letters. (N is user input).
Print the final string that you need to speak.Eg:
N=8
Output-
MedhaMed
I am getting an exception in my code.
Input the maximum no: of letters upto which u want to speak the word repeatedly:

Exception in thread "main" java.util.NoSuchElementException
	at java.base/java.util.Scanner.throwFor(Scanner.java:937)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at medhaword.main(medhaword.java:10)


What I have tried:

Java
import java.util.*;
import java.io.*;
import java.lang.*;
public class medhaword
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Input the maximum no: of letters upto which u want to speak the word repeatedly:");
    int N=sc.nextInt();
         String s="Medha";
        int x=s.length();
        int noofrepetitions=N/x;
        int remletters=N%x;
        String t="";
        for(int i=0; i<noofrepetitions; i++)
="" {
="" t+="s;
" }
="" if(remletters="">0)
        {
            t+=s.substring(0, remletters);
        }
        System.out.println(t);
    }
}
Posted
Updated 25-Jun-23 0:25am
v2
Comments
Abhinav Kishore M 25-Jun-23 6:16am    
why this need for =""?
Dave Kreskowiak 25-Jun-23 11:07am    
It's a bug in CP when you paste code.
Abhinav Kishore M 25-Jun-23 6:19am    
I am getting this exception:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at medhaword.main(medhaword.java:10)

1 solution

The code you provide doesn't compile as shown, I have fixed what it looks like it should be (and indented it correctly):
Java
import java.util.*;
import java.io.*;
import java.lang.*;
public class Main
{
	public static void main(String[] args) 
	{
        Scanner sc=new Scanner(System.in);
        System.out.println("Input the maximum no: of letters upto which u want to speak the word repeatedly:");
        int N=sc.nextInt();
        String s="Medha";
        int x=s.length();
        int noofrepetitions=N/x;
        int remletters=N%x;
        String t="";
        for(int i=0; i<noofrepetitions; i++)
        {
            t+=s;
        }
        if(remletters>0)
        {
            t+=s.substring(0, remletters);
        }
        System.out.println(t);	
	}
}
If I run that in an online Java IDE (Online Java Compiler - online editor[^]) it works fine:
Result
Input the maximum no: of letters upto which u want to speak the word repeatedly:
8
MedhaMed


...Program finished with exit code 0
Press ENTER to exit console.
So ... the problem is something in the environment at your end, and we have no access to that at all (or even any idea what it might be!)
 
Share this answer
 
Comments
Abhinav Kishore M 25-Jun-23 6:30am    
I am using this editor: https://www.jdoodle.com/online-java-compiler/
Abhinav Kishore M 25-Jun-23 6:31am    
thank u
OriginalGriff 25-Jun-23 7:24am    
You're welcome!
CPallini 26-Jun-23 4:36am    
5.

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