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);
}
Related
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.
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);
}
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.
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 8 years ago.
Improve this question
Every time I try running this I get a java exception code. Any Ideas?
package employ;
import java.util.Scanner;
public class Employ {
public static void main(String[] args) {
String []empna={};
int numofemp;
int []empnu;
String []empadd;
int []emphd;
//Scanner sc = new Scanner(System.in);
System.out.println("how many employees do you have?");
Scanner sc = new Scanner(System.in);
numofemp=sc.nextInt();
for (int j=0;j<numofemp;j++){
empnam (empna,j);
// System.out.println(empna[0]);
}
}
public static void empnam(String empna[], int j ){
System.out.println("What is your employees first and last name?");
Scanner n = new Scanner(System.in);
//String ns=n.nextLine();
empna[j]=n.nextLine();
}
}
You didn't initialize the array with the correct size, this line is wrong:
String []empna={};
Try this instead, right after the line where you read the value of numofemp:
String[] empna = new String[numofemp];
Remember, an array in Java is of fixed length and its size must be specified at the creation time, it won't grow as elements are being added to it. If a variable-length array were needed, then use an ArrayList.
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?");
}