error while taking String input in java [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
package test;
import java.util.*;
public class NewClass {
public static void main (String[] args)
{
String s;
Scanner sc = new Scanner (System.in);
s=sc.nextLine();
System.out.println(s);
}
}
I don't know why this error is coming.
Exception in thread "main" java.util.InputMismatchException at
java.util.Scanner.throwFor(Scanner.java:864) 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
test.Test.main(Test.java:12) C:\Users\MOHIT KUMAR
SINGH\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:
Java returned: 1 BUILD FAILED (total time: 2 seconds)

java.util.InputMismatchException-In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.

I think it's your main class but there no argument/parameter in your main method. You should do like this
public static void main(String[] arr) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println(s);
}

Related

Why am I getting an error for actual and formal argument lists differ in length? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Trying to make it so pasting a 60 character long url, will print only the first 56 characters. I have only ever used Java in high school, so I am very inexperienced.
import java.util.Scanner;
public class CopyOfCopyOfethanrun
{
public static void main()
{
Scanner kb = new Scanner(System.in);
link();
}
static void link(Scanner kb, String[] args)
{
String link;
System.out.print("\n\n Enter Link: ");
link = kb.nextLine();
String input = link;
String firstfiftysix = "";
if (input.length() > 56)
{firstfiftysix = input.substring(0, 56);}
else{firstfiftysix = input;
}
System.out.println(firstfiftysix);
}
}
[Here is an image showing the error I am experiencing][1]
[1]: https://i.stack.imgur.com/T6ux7.png
Your link method has 2 arguments: When invoking link, you need to provide [A] a Scanner instance, and [B] an instance of an array of Strings.
When you call link();, you provide neither of those. Presumably you want link(kb, args); there. Your main must look like:
public static void main(String[] args)
it currently doesn't. Once you fix that, voila, you have your args. But, you should just get rid of that, you don't use it anywhere in link.

Why `.read()` Method of a number return different number [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Please explain the use of System.in.read() method in this example that I'm learning about from another post. As a beginner, I find it unclear when I input a number and get different one in the output, please clarify.
import java.io.IOException;
public class MainClass {
public static void main(String[] args) {
int inChar;
System.out.println("Enter a Character:");
try {
inChar = System.in.read();
System.out.print("You entered ");
System.out.println(inChar);
}
catch (IOException e){
System.out.println("Error reading from user");
}
}
System.in.read() reads values as their binary value; for example reading "a" will result in the value of "97" - the mapping of this is available here https://www.asciitable.com/.
In order to get the textual representation of this in Java you want to read this as either a Character or a String - Character is a single value, while a String is a combination of Characters one after the other. For example:
public class MainClass {
public static void main(String[] args) {
System.out.println("Enter a Character:");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.print("You entered ");
System.out.println(input);
}
Have a look at the Scanner class to see other options, you can use scanner.nextInt() to get an Integer back.

Can anyone help in Java Program Regarding String method [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Can anyone tell me how to debug this program. I have encountered an error using string method.
import java.util.Scanner;
class StringUser{
public void show();
{
System.out.print("\nYou Entered: "+a);
}
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.print("Enter a String: ");
StringUser c=new StringUser();
String a=obj.nextLine();
c.show();
}}
Your a variable is local to the main method. If you want to access it in another method, you can pass it to that method :
public void show(String a) // also note that you mistakenly had a semi colon here
{
System.out.print("\nYou Entered: "+a);
}
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.print("Enter a String: ");
StringUser c=new StringUser();
String a=obj.nextLine();
c.show(a);
}

Chopper Java: what is wrong with this? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
import java.util.Scanner;
public class linecounter {
public static void main(String[] args) {
System.out.print("Enter a line of integers");
Scanner chopper = first Scanner();
int x =chopper.nextInt();
while (chopper.hasNextInt()) {
System.out.println(chopper.nextInt());
}
}
}
This keeps telling me that a ';' is expected on the line that starts with scanner chopper, what could the problem be?
P.S. Do you know how i can get it to keep count of how many integers were typed in?
You have written first Scanner() instead of new Scanner(System.in).
You need to specify an input source, and since you want the user to input the numbers, you should use System.in as argument.
This line:
Scanner chopper = first Scanner();
should be:
Scanner chopper = new Scanner(System.in);
new Scanner(System.in) creates a new Scanner object that take input from the console, first Scanner() is syntactically incorrect and is what gives the error.
Why does your code say first Scanner(); instead of new Scanner();? I think that's the problem.

Multiple Errors on test code with Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I am following this tutorial, for testing and this was a little test/lesson we were doing. I keep getting errors on the 11th line. Can someone tell me whats going on? This is the code, except its not complete.
import java.util.Scanner;
public class Test2Coding {
public static void main(String[] args) {}
{}
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
}
You have some extra brackets in the code. Your 3 lines of code should be directly inside the brackets after the main method declaration.
public static void main(String[] args) {
// code here
}
Just remove the brackets you are not using
import java.util.Scanner;
public class Test2Coding {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int age;
System.out.println("How old are you?");
}

Categories

Resources