This question already has answers here:
Trying to read from the console in Java
(4 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I just started learning some Java and I made this simple program but I dont know whats wrong with my code.
This is what the error I see when I run:
Exception in thread "main" java.lang.NullPointerException
at treehousejava.Main.main(Main.java:9)
This is my code:
package treehousejava;
import java.io.Console;
public class Main {
#SuppressWarnings("unused")
public static void main (String[] args){
Console console = System.console();
String q = console.readLine("Hi there! what's your name? ");
console.printf("hi there %s ! my name is console! ");
}
}
I suspect you want to use System.in and System.out for this type of learning program. System.console() might not be available on your platform and the call will return null, hence the NullPointException. Standard input and output will always be available.
Scanner scanner = new Scanner(System.in);
System.out.println("Hi there! what's your name? ");
String q = scanner.nextLine();
System.out.printf("hi there %s ! my name is console! ", q);
Related
This question already has answers here:
Semicolon at end of 'if' statement
(18 answers)
Closed 4 years ago.
I got to code Java for an assignment where am I going wrong with this "if statement" surely if I enter anything but 10 in won't print out Variable information. Picture attached.
import java.util.Scanner;
public class JavaApplication7 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int code = 0;
code = input.nextInt();
if (code == 10); {
System.out.println(code);
}
}
}
Never mind I think I figured it out I was ending my statement to early or something this stuff is not very forgiving. Edit: Just saw snr answered haha thank you.
This question already has an answer here:
java.lang.IllegalStateException: Scanner closed
(1 answer)
Closed 4 years ago.
import java.util.Scanner;
public class Main {
public static void main(String[]args) {
double num = 0;
double counter = 0;
double ncot = 0;
Scanner scan = new Scanner(System.in);
while (!(num == -1)) {
ncot = scan.nextDouble();
if (ncot == -1) {
System.out.println("The average is: " + (double)(num/counter));
}
else {
num = num+ncot;
counter++;
}
}
scan.close();
}
}
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.next(Scanner.java:1465)
at java.util.Scanner.nextDouble(Scanner.java:2413)
The Code it seems to be triggering a error when im running it on Ideone.com but when i run in eclipse it's fine however.
This is really puzzling. According to the published source code for the standard versions of Scanner (Java 6 and Java 8), the only way that you can get that exception at that point is if something has already called close() on the Scanner object.
(Note that if you instantiated a Scanner around an input stream that was already closed, or a dummy, or null, you would get a different exception. This particular exception only happens if the Scanner object's private closed flag is true, and that only happens when you call Scanner.close().)
But with the code you have shown us, that is not possible. The Scanner can only be closed after you exit the loop.
I can think of only two explanations:
This isn't the code that you ran on ideone.
The guys have implemented ideone have tweaked the behavior of the standard Scanner class. In a way that is inexplicable ... to me.
This question already has answers here:
NoSuchElementException with Java.Util.Scanner
(10 answers)
Closed 6 years ago.
I am trying to build a very basic program in java to print all the unique characters from the string but I am getting runtime error.
Input - amanda
output -amnd
import java.util.*;
class uniquechars {
public static void main(String[] args) {
Scanner inp = new Scanner(System.in);
System.out.print("Enter a string:");
String str = inp.nextLine(); // input from user
String res="";
for (int i=0;i<str.length();i++){
int count=0;
for(int j=0;j<res.length();j++){
if(str.charAt(i)==res.charAt(j)){
count++;
}
}
if(count==0){
res = res+str.charAt(i);
}
}
System.out.println("Output string with only unique characters:"+res);
}
}
Error
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at uniquechars.main(Main.java:6)
If you are using any online tool to test your code, be sure to provide input to the program.
My guess is you are forgetting to give the input to the program while running it on an online tool.
It works on codechef.com/ide, you just have to select your programming language from the dropdown list. as shown here.
This question already has answers here:
How can I read input from the console using the Scanner class in Java?
(17 answers)
Closed 7 years ago.
I'm trying to make it so when the user writes Start the program does something, but I'm unsure of how to what the user actually wrote.
This was my first attempt at it:
import java.util.Scanner;
public class Suhwag {
public static void main (String args[]){
Scanner scanNer = new Scanner (System.in);
System.out.println("Please write \"Start\" to begin.");
String stinky = "Start";
if (stinky == scanNer);
But with this, I got the error message:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Incompatible operand types String and Scanner
After I saw the error, I tried to convert scanNer to a string as seen here:
import java.util.Scanner;
public class Suhwag {
public static void main (String args[]){
Scanner scanNer = new Scanner (System.in);
System.out.println("Please write \"Start\" to begin.");
String stinky = "Start";
String input = scanNer.nextLine();
if (stinky == scanNer);
But the same error message still appears. Anyone know what I could do to make it work?
You're trying to compare a Scanner object with a String object. First, you could input the string with the following line:
String myString = scanNer.next()
Then, compare it with "Start":
if ( myString.equals( "Start" ) )
{
...
}
You are comparing a String to a Scanner object.
You should use the equals method to compare String's
No need for the semi-colon after the if (see below)
In reference to your last code snippet:
if (stinky.equals(input)){
//do something
}
in the latter code area you said in your if statement:
stinky == scaNer
it should be
stinky.equals(input)
in your if statement, you compared still your scanner with ur stinky
change your if statment to this
if (input.equals(stinky)){<code here>}
your previous code didnt work because you compare a scanner with a string
This question already has answers here:
Printing array error
(3 answers)
Closed 8 years ago.
Whenever I execute my code in java with eclipse (code is as follows)
import java.util.*;
import java.io.*;
public class PracticeOne {
public static void main(String[] args) throws FileNotFoundException {
Scanner scn = new Scanner (System.in);
int first = scn.nextInt();
scn.nextLine();
for(int i =0; i<first; i++){
String preSplt = scn.next();
String postSplt[] = preSplt.split("--");
System.out.println(postSplt);
}
}
}
and once I type in
3
Honda element--19--17950
Ford Edge--19--18130
it gives me this
[Ljava.lang.String;#c0fa1f5
[Ljava.lang.String;#4e15f6af
in between "Honda element" and "Ford Edge" as I press enter and then it spits out
[Ljava.lang.String;#3f68336
after "Ford Edge" and for the record it does not split the input I give it or do anything after it spits out that last bit although this may be due to ineffective code (I am not done with this program yet) I thought it might be a little helpful
I find assembly language quite fascinating but I would like to know whats causing this and if it even has anything to do with assembly
The problem here is that you're attempting to print the array object itself instead of its contents. Use Arrays.toString:
System.out.println(Arrays.toString(postSplt));