It works well on Intellij.
However, NoSuchElement appears on the algorithmic solution site.
I know that NoSuchElement is a problem caused by trying to receive it even though there is no value entered.
But I wrote it so that the problem of NoSuchElement doesn't occur.
Because given str, the for statement executes. Given "END", the if statement is executed. And because it ends with "break;".
I don't know what the problem is.
Algorithm problem: Arrange the reverse sentence correctly.
My code for algorithmic problems
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
while(true) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
if(str.equals("END")){
break;
}
for (int i = str.length()-1; i >=0; i--) {
System.out.print(str.charAt(i));
}
System.out.println();
}
}
}
Output
!edoc doog a tahW
erafraw enirambus detcirtsernu yraurbeF fo tsrif eht no nigeb ot dnetni eW
END
Expected
What a good code!
We intend to begin on the first of February unrestricted submarine warfare
This happens when there is no input at all, for example when you hit Ctrl + d or run your code like echo "" | java Main.java.
To avoid this, check that the Scanner actually has input before trying to grab the next line. Pull scan out of the loop, there is no point to create a new Scanner for each line anyway. Then use hasNext to see if there is input.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String str = scan.nextLine();
if(str.equals("END")){
break;
}
for (int i = str.length()-1; i >=0; i--) {
System.out.print(str.charAt(i));
}
System.out.println();
}
}
}
Related
I am working on a simple project, I took it from a website that gives some challenge to improve my coding skill in java.
import java.util.Scanner;
public class Test1 {
public void test() {
Scanner in = new Scanner(System.in);
System.out.println("enter something :");
String str = in.nextLine();
StringBuilder sb = new StringBuilder(str);
if (str.isEmpty()) {
System.out.println("you should write something");
}
if(str.length()<=30){
System.out.println("reverse : "+sb.reverse());
}else {
System.out.println("Error");
}
System.out.println("----------------------");
}
public static void main(String[] args) {
Test1 c = new Test1 ();
for (int i = 1; i <= 10 ; i++) {
System.out.println("case number : " +i);
c.test();
}
}
}
case number : 1
enter something : ayoub
reverse : buoya
----------------------
...loop continue ..
My code works like I want in terminal of eclipse, but when I put it into the "code editor" of the web site, this last one gives me a runtime error that says:
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Test1.test(Test1.java:10)
at Test1.main(Test1.java:31)
I tried to search on StackOverflow for some solutions but I didn't find it.
You are probably using an online java code editor/compiler which does not have an stdin input.
As stated by arcy, you are probably using an IDE with a built-in console window which allows you to pass standard inputs to your program.
The following online editor will allow you to add inputs.
You should take care of the way you are using the nextLine method of Scanner:
The exception you are getting is the result of the scanner not getting any inputs, as can be seen here. I suggest you refactor your loop on the scanner using the method hasNextLine of Scanner.
I am trying to make a java program that given a string, I return the length of it, but I do not know why it does not catch me all. I'm starting with the programing. I've got here.
import java.util.Scanner;
public class lengt {
public static void main (String [] args) {
Scanner in = new Scanner (System.in);
String cad = in.next();
System.out.println (cad);
System.out.println ("The length is:" + cad.length ());
}
}
There is a problem in your solution. in.next(); just take the first word of the chain, the one that is most immediate. If instead of using that you put it in.nextLine(); It takes you all the line you enter. You also have to be careful with length, which goes from 0 to n-1, when it comes to taking the length, since you could take one more and unnecessary. The solution for the problem that you pose would be, and there may be more, but one valid
import java.util.Scanner;
public class Example {
public static void main (String [] args) {
Scanner in= new Scanner (System.in);
String cad = in.nextLine();
System.out.println (cad);
System.out.println ("The length is:" + cad.length() - 1); //you can remove -1
}
}
Hey there I got into some trouble with my java Code.
I try to code a bit around with java for a few hours and I dont know much thats why im asking. I learn best by trying but I get into so many problems.
So: I want the scanner to scan the next Statement and if its "ja" it should do the if thing etc.
The problem is, when i try to compile it it has an error with the = s.nextInt thing. In the console it says: "cannot find symbole". I tried so many things I dont know what to do. Allready tried so much.
import java.util.Scanner;
public class Brotcrunsher {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("A flag has more then 1 color right?");
String a = s.NextInt();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
} // end of if
else {
System.out.println ("You arentn a genie");
} // end of if-else
}
}
thanks in advance.
EDIT: Problem solved. Thank you for every awnser. I try my best to Tag my posts better and to format my code better
Here:
String a = s.NextInt();
You want a to be String (which makes sense, as you want to compare it against other Strings later on); so you better use:
String a = s.nextLine();
instead!
The other method a) does not exist and b) nextInt() ... returns a number, not a string
I can see two errors, firstly you are taking a string input from the command line user so your scanner must be "scanner.nextLine()" which takes a string, as it stands you are expecting an integer value.
Second your "s.scanner" is not calling anything, you have declared your scanner with the name "scan", so you need to change that to "scan".
import java.util.Scanner;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("A flag has more than one colour?");
String input = scan.nextLine();
if (input.equals("yes")) {
System.out.println("well done");
} else {
System.out.println("wrong answer");
}
}
Try:
import java.util.Scanner;
public class Brotcrunsher {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
System.out.println ("Hallo");
System.out.println ("A flag has more then 1 color right?");
String a = scan.nextLine();
if (a.equals("ja")) {
System.out.println ("You arent dumb, nice.");
} // end of if
else {
System.out.println ("You arentn a genie");
} // end of if-else
}
}
You have got a compilation error that should be
String a = scan.next();
Since scan is your scanner object where you are using String a = s.NextInt(); which is not at all an object of scanner.
Two issues, one is a is a String not an int and the second is Scanner.nextLine() (or nextInt() or next()). And, the local reference is scan (not s). Like,
String a = scan.nextLine();
You can use like this.
import java.util.Scanner;
class ScannerTest{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("Enter your rollno");
int rollno=sc.nextInt();
System.out.println("Enter your name");
String name=sc.next();
System.out.println("Enter your fee");
double fee=sc.nextDouble();
System.out.println("Rollno:"+rollno+" name:"+name+" fee:"+fee);
sc.close();
}
}
refrence: http://www.javatpoint.com/Scanner-class
As talked about above I am trying to create a balanced delimiter checker in java. I have seen several examples of how to make one all of which were well done, but I have not found any that deal with having to get input from the user with the stack. I have looked into it, but I have not been able to find much with it. Below is what I have so far. I have been working on it but I have done more to confuse myself than anything else. Am I looking at this the right way? Or is there a better route to go about this?
import java.util.Scanner;
import java.util.Stack;
public class BalancedParenthensies
{
public static void main (String [] args)
{
Scanner input = new Scanner(System.in);
String line = input.nextLine();
Stack<Character> S = new Stack<Character>();
while(input.hasNext())
{
System.out.println("Enter: ");
Character z= input.nextLine();//this line is not working
S.push(z);
}
for (int i=0; i<line.length(); i++)
{
char c = line.charAt(i);
// do something with c
}
}
}
I started learning java and basically wanted to do some coding for practice. My search landed me to javaranch and i did their first assignment which was not that hard. The hard part for me was the second part. The first part is that you just simply write a program that will read in a name from the command line and write it out 100 times. Now the second part wants me to display the word in such a way that it doesn't get split up at the edge of the screen. As soon as the word doesn't fit at the right side it wants me to move the whole word to the next line. Here is my code.
public class Hundred
{
public static void main( String [] args)
{
System.out.println();
for (int i = 0; i <=100; i++)
{
if (args.length == 0)
{
System.out.println("I've got nothing to show you");
}
else
{
System.out.print( args[0] + " ");
}
}
}
}
Separate the view from the model: create the thing you want to print before you print it.
Like this:
import java.io.StringWriter;
public class Hundred {
public static void main(String[] args) {
StringWriter sw = new StringWriter(1024);
for (int i = 0; i < args.length; ++i) {
sw.append(args[i]).append(' ');
}
System.out.println(sw.toString());
}
}
Add logic inside the loop to check the length of the current StringWriter and print and flush if it exceeds your requirements.