Exception when code is run on Online Java compiler unlike on notepad - java

I have written the code to print the minimum number of insertions required to make any string a palindrome . The code runs perfectly when written on notepad and compiled through cmd. But it gives Exception when run at any online java compiler.
Here is the code:
import java.io.*;
import java.util.Scanner;
class Solution
{
public void disp(String s)
{
int l=s.length();
int pos=-1;
for(int i=l-1;i>0;i--)
{
char b=s.charAt(i);
char b1=s.charAt(i-1);
if(b!=b1)
{
pos=i;
break;
}
}
String w=s.substring(0,pos);
int l1=w.length();
int count=0;
for(int i=0;i<l1;i++)
{
char b=w.charAt(i);
count++;
}
System.out.println(count);
}
}
public class scanner_call
{
public static void main(String[] args)throws InterruptedException
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the one line string");
String s=sc.next();
Solution p1=new Solution();
p1.disp(s);
}
}
The online compiler shows this exception.
Enter the one line string
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at scanner_call.main(scanner_call.java:39)
Why this very program runs perfectly in notepad but raises Exception in online compilers? What should i do?

This is because your program requires input detection, but the online compiler you use might not have input capability.
Try this: https://www.tutorialspoint.com/compile_java_online.php
Put your Source Code into the Source Code tab
Put your input into the STDIN (fyi, I put "asd" in there and worked fine)
Then you should see some result, to change the input, change the STDIN

Related

Error while trying to pass a text file as argument

so I am learning how to pass files as argument in java. I am trying to implement a scheduling algorithm in java but my issue is the console shows the error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
Since I am totally new to this, I am not sure where I am doing wrong. I can get the program to scan the input text file normally using an object from the scanner class but when I try and pass it as an argument, I get the above error. How do I fix this error? The program below is not the full program but big enough to compile and show the error I am getting. I am using an online IDE atm and I have a 'file.txt' that contains integer values I want. I believe the error's got something to do with the args statement. Can anyone help me fix this please?
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
int i=0,j=0;
if(args.length<1)
{
System.out.println("ERRRRRRRORRRRRR!");
}
File file = new File(args[0]);
Scanner scan = new Scanner(new File(args[0]));
int n= scan.nextInt();
int process_id[] = new int[n];
int burst_time[] = new int[n];
int arrival_time[] = new int[n];
n=0;
while(n<3){
process_id[i]= scan.nextInt();
burst_time[i]= scan.nextInt();
arrival_time[i]= scan.nextInt();
n++;
i++;
}
}
}
you need to 'return' out of main when args.length<1.
Your code detects the empty args but do nothing with it.
either you need to put your logic in else part or you need to stop program in if condition by return or System.exit(0);

Program that returns input \t as *

The code is supposed to read an unidentified number of inputs from the keyboard and return any tabs as *. My program seems to work when I run it in eclipse and get no errors. When I turn in the code on the submission website, this is the error I get.
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1589) at replaceHW.main(replaceHW.java:9)
import java.util.Scanner;
public class replaceHW {
public static void main(String[] args) {
//write a program that converts all TABS in your code
//with STARS i.e. *
Scanner in = new Scanner(System.in);
String ans;
while(!(ans = in.nextLine()).equals(""))
System.out.println(ans.replace("\t","*"));
}
}
Your problem is simple: nextLine() works in tandem with hasNextLine(): the correct code is:
try (Scanner in = new Scanner(System.in)) {
while (in.hasNextLine()) {
String line = in.nextLine();
if (!"".equals(line)) {
System.out.println(ans.replace("\t","*"));
}
}
The try-with-resources is best practice. But be wary than with System.in, it will close it when done.
hasNextLine() will try to read has much input is needed to find a line.

Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862)

I really don't see what the problem could be. This is the error I'm getting:
$javac Palindrome.java
$java -Xmx128M -Xms16M Palindrome
Enter your word
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Palindrome.main(Palindrome.java:28)
This is the code:
import java.io.*;
import java.util.Scanner;
import java.util.*;
import java.lang.StringBuffer;
// Java program to illustrate checking of a string
// if its palindrome or not using reverse function
public class Palindrome
{
public static void checkPalindrome(String s)
{
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();
// check whether the string is palindrome or not
if (s.equals(reverse))
System.out.println("Yes");
else
System.out.println("No");
}
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your word");
String output = scanner.next();
}
}
I ask for the word and then get the input to check if it is a palindrome
In online editor this problem occurs in input.
Try writing before getting inputs:
if(sc.hasNext())
The code can be written as:
public class Palindrome
{
public static void checkPalindrome(String s)
{
// reverse the given String
String reverse = new StringBuffer(s).reverse().toString();
// check whether the string is palindrome or not
if (s.equals(reverse))
System.out.println("Yes");
else
System.out.println("No");
}
public static void main (String[] args) throws java.lang.Exception
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your word");
if(sc.hasNext())
String output = scanner.next();
}
}
I cannot post comments, since I dont have enough reputation. But the solution is that you have no input source.
The line your stacktrace refers to is:
private void throwFor() {
skipped = false;
//since you are using an online tool, you dont actually have an
//input unless you click on the stdin tab and provide an input.
if ( (sourceClosed) && (position == buf.limit()))
throw new NoSuchElementException();
else
throw new InputMismatchException();
}
Just press on the stdin tab and type something in it before executing your code in your online ide and you should not receive an exception anymore. But you should provide some sort of output that reflects your result wheather it is a palindrom :).

Copied this exact code from my book and does not work

I copied this exact code from my textbook, and when I try to run it it does nothing but load, the file is in the same location of the java file, and the name is correct. Im using Dr. Java. So im just wondering why it wont run and just keeps loading. The book I am using is Java Illuminated 3rd edition. Also, the newscores.txt file just has 10 numbers, seperated by spaces.
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class TestScoresAndSummaryStatistics {
public static void main(String[] args) throws IOException {
int number;
File inputFile = new File("newscores.txt");
Scanner scan = new Scanner(inputFile);
while (scan.hasNext()); {
number = scan.nextInt();
System.out.println(number);
}
System.out.println("End of file.");
}
}
you have a semicolon end of while statement .you should remove it.because of this semicolon your while loop run repeatedly and your code inside while loop become separate block from the loop.
while (scan.hasNext()); {
number = scan.nextInt();
System.out.println(number);
}
change to
while (scan.hasNext()) {
number = scan.nextInt();
System.out.println(number);
}

How to stop printing input at 42?

I'm doing a problem on a website where it inputs the numbers:
1
2
88
42
99
and it's supposed to output
1
2
88
The code is supposed to stop printing the input when it hits 42, and it works, but when I submit it to the site, it tells me that it gave the wrong answer.
Here's my code:
http://pastebin.com/y5e8DyHz
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scan = new Scanner(System.in);
int res;
for(int i=0;i <5; i++) {
res = scan.nextInt();
if (res!=42) {
System.out.println(res);
} else {
System.exit(0);
}
}
}
}
It works when I run it in IDEOne, so I'm not sure what the problem is. Thanks!
Note that a solution to the TEST problem is available in the SPOJ forums. It's a good example of how to process input as fast as possible in java.
Based on your requirement, you have to use it like shown below, even in your IDE
Scanner scan = new Scanner(System.in);
int res;
while (scan.hasNext()) {
res = scan.nextInt();
if (res != 42) {
System.out.println(res);
} else {
System.exit(0);
}
}
Reason being what #jrbeverly mentioned above as his comment
update 1:
If you meant "stop printing the input" as the program must exit on encountering '42',you are good. But if your requirement is just to discard printing the number, and let the program run and accept the next number, then remove System.exit(0) . because System.exit(0) means to terminate the JVM from further execution of the program
update 2:
As #Giovanni Botta mentioned below, the exact solution is provided in the mentioned link, snippet adding here
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
String s;
while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
}
}

Categories

Resources