cannot find the symbol method console(); [duplicate] - java

This question already has answers here:
java.lang.System error in Console() [closed]
(3 answers)
Closed 4 years ago.
This is my code :---
import java.lang.*;
class Console
{
public static void main(String args[])
{
char i;
i=System.console().readLine("this is how we give he input to the string");
System.out.println("this is what we want to print:0)");
System.out.println(i);
}
}
and the output I am getting is this:-
Output:-
.java:7: cannot find symbol
symbol : method console()
location: class java.lang.System
i=System.console().readLine("this is how we give he input to the string");
^
1 error
Tool completed with exit code 1
If anyone can help me out...

Mistake with the jdk version, because it must be jdk1.6 or later, and when changed to a newer jdk,
there's a compilation problem, System.console().readLine() returns a String, but you assigned char

Also, some IDE's have trouble with the console class (possibly because they are using it themselves to redirect output to a window/dialog)
So a really good work around is using:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readline();
//or if you want a char
char i = str.charAt(0);
Hope that helps

Related

String method not giving the right output [duplicate]

This question already has answers here:
Java String replace not working [duplicate]
(6 answers)
String replace method is not replacing characters
(5 answers)
Closed 5 years ago.
So I was solving a question from a coding website that required me to replace ? with an a or b provided there are no two consecutive a... I have tried a lot and this is the code that i have written. although the code seems correct to my knowledge, whenever I run this code the output is same as the input itself. No changes are made to it.
Example case:
input: ?ababa?b?
output: babababba
input: ababb?b
output:ababbab
The input has to be wither a,b or ?
The output must be such that it has the highest precedence in dictionary.
However whatever input i give i get the same output. If I give ?ab as input I get the same as output please help me
package Beginner;
import java.util.Scanner;
public class ExplRuin {
public static void main(String args[]){
String s;
Scanner in = new Scanner(System.in);
s = in.nextLine();
if(s.length()==1){
if(s.equals("?"))
s.replace("?", "a");
} else
{
if(s.toString().startsWith("?")){
if(s.contains("?b"))
s.replace("?b","ab");
else
if(s.contains("?a"))
- s.replace("?a", "ba");
}
if(s.endsWith("?")){
if(s.contains("a?"))
s.replace("a?", "ab");
else
if(s.contains("b?"))
s.replace("b?","ba");
}
if(s.contains("?a")||s.contains("a?")){
s.replace("?", "a");
}
else{
s.replace("?", "a");
}
}
System.out.print(s);
}
}
You have to assign the result to your variable, because String in java is immutable :
s = s.replace("?", "a");
^^^---------------------------assign the result to your variable

Java. First loop doesnt work [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i got a little problem with my code. When i write "exit" still shows statement "Unknown command" and i want to show "Bye". Can you help me?
import java.util.Scanner;
public class Hello{
public static void main(String[] args){
Scanner odczyt = new Scanner(System.in);
String word;
do{
word = odczyt.nextLine();
System.out.println("Unknown command");
}
while(word!="exit");
System.out.println("Bye");
}
}
"Unknown command" will always be printed. Besides that you shouldn't use = to compare Strings. You should use .equals() or .equalsIgnoreCase().

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 Error [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I am new to programming, so just consider my mistakes. I am writing a program where the user gives two input numbers in one line separated by whitespace. I have to assign the first input to an integer variable and second to a double and have to perform some mathematics and show the result. Following is my code:
import java.util.Scanner;
public class foo{
public static void main(String[] args){
String b = null;
Scanner sc = new Scanner(System.in);
b = sc.next();
String[] split = b.split(" ");
int i = Integer.parseInt(split[0]);
double d = Double.parseDouble(split[1]);
System.out.println(i+20);
System.out.println(d-1.50);
}
}
And following is the error i am getting while running it.
F:\java\work\codechef>javac foo.java
F:\java\work\codechef>java foo
20 300.50
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at foo.main(foo.java:9)
First I tried making it with b=sc.readLine(); but there i got the following error while compiling:
error: cannot find symbol
b = sc.readLine();
^
symbol: method readLine()
location: variable sc of type Scanner
1 error
Why I am getting these errors and how to solve the above problem.
You used sc.next() which returns the next token (a token is something that had delimiter before and/or after) so it contains only the 20 or 300.50 in your case.
You should use sc.nextLine() to use split later on, this will return the full line.
Or use:
int i = Integer.parseInt(sc.next());
double d = Double.parseDouble(sc.next());
And get rid of lines:
b = sc.next();
String[] split = b.split(" ");

why cant java find a symbol i have initialized? [duplicate]

This question already has answers here:
Take a char input from the Scanner
(24 answers)
Closed 8 years ago.
according to the following:
triangles.java:17: error: cannot find symbol
shape = keyboard.nextChar();
^
symbol: method nextChar()
location: variable keyboard of type Scanner
1 error
why is it not finding symbol? i have keyboard initialized as the scanner. how would i fix this?
this is how i initialized it:
char shape;
shape = scanner.nextChar();
i have changed char to String, but that did nothing. i have imported a scanner, so thats not an issue.
If you're wanting to find the char within a String variable, just do something like:
String str = scanner.nextLine(); // or scanner.next();
char shape = str.charAt(i); // replace i with index of char you're trying to find.
hope that helps

error: '.class' expected [duplicate]

This question already has an answer here:
What does "error: '.class' expected" mean and how do I fix it
(1 answer)
Closed 4 years ago.
Hi friends while compiling the below error is coming
error: '.class' expected
I'm not able to find what is the error. I checked in many websites but not able to find why this error is coming. Plz Help
Thanks in advance.
import java.io.*;
class Test
{
boolean isGoodEmployee(boolean ismarried,int noofchild,String middlename,String childnames[])
{
boolean child,letter,last,child;
if (noofchild <= 2)
child=true;
boolean firstletter = middlename.startsWith("k");
boolean lastletter = middlename.endssWith("e");
if (firstletter && (!lastletter))
letter = true;
int lastnameletterscount = lastname.length();
if (lastnameletterscount > 4)
last = true;
String name = raju;
for (int i=0; i < noofchild; i++)
if(name.equalsIgnoreCase(childnames[i]))
{
child = true;
break;
}
}
}
class GoodEmployee
{
public static void main(String args[]) throws IOException
{
String firstname, middlename, lastname;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the first name of employee");
firstname = br.readLine();
System.out.println("enter the middle name of employee");
middlename = br.readLine();
System.out.println("enter the last name of employee");
lastname = br.readLine();
//System.out.println("full name of employee is:"+first+middle+last);
System.out.println("enter employee is married or not");
boolean ismarried = Boolean.parseBoolean(br.readLine());
if (ismarried)
System.out.println("enter number of childrens");
int noofchild = Integer.parseInt(br.readLine());
String childnames[] = new String[noofchild];
System.out.println("enter children names");
for (int i=0; i < noofchild; i++)
childnames[i] = br.readLine();
Test t = new Test();
t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);
}
}
I'm using "javac GoodEmployee.java" to compile the program
You have a mis-matching method call in isGoodEmployee.
The childnames argument is defined as an array, so you can simply pass in the argument, replace:
t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);
with
t.isGoodEmployee(ismarried,noofchild,middlename,childnames);
Aside from that, do not ignore other compilers errors such as
middlename.endssWith("e");
^---------extra 's'
Familiarize yourself with the javadocs.
there's plenty wrong with this code:
you've declared boolean child twice in isGoodEmployee() (1st line)
endssWith should be endsWith()
and so on, but nont of them are the issue you stated. what command line are you using for compiling this class?
#Reimeus has nailed it.
The strange compilation error can be explained as follows. In this line:
t.isGoodEmployee(ismarried,noofchild,middlename,childnames[]);
the childnames[] phrase is supposed to be a Java expression. But in fact, the syntax most closely resembles a type ... in fact an array of some (non-existent) class called childnames. The Java compiler's syntax error recovery code has decided that if it inserts .class after the typename (i.e. childnames[].class) that will give a syntactically valid expression.
Of course, the correction that the compiler is suggesting is nonsensical ... not least because there won't be a class or interface named childnames in your application.
The moral of the story is that unusual syntax errors sometimes cause a compiler to produce peculiar error messages that only make sense if you can figure out how the parser would have parsed the erroneous input.
Make the class with the main method public if you are using an IDE.
In case you are trying to run it using command window I don't think a '.class' file is required for compiling rather it would be required for execution. So after you have a .class file by entering javac GoodEmployee and you have no errors enter java GoodEmployee which will execute the .class file you have.

Categories

Resources