Java Scanner from second method [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 2 years ago.
Improve this question
I am having problem to invoke Scanner class from second method in main method. My code is this:
import java.util.Scanner;
public class Main{
static void checkAge(int age){
Scanner new_age = new Scanner(System.in);
age = new_age.nextInt();
System.out.println("Enter your age");
if(age < 18){
System.out.println("You are a minor");
} else {
System.out.println("You are of apropriate age");
}
}
public static void main(String[] args){
checkAge();
}
}
I get an error:
Main.java:18: error: '.class' expected
checkAge(Scanner.new_age(int));
^ 1 error

Call you function with arguments : checkAge(18); or checkAge(15);as you checkAge() requires parameters to check the conditions.

Related

I want to print the output using method but then i cant print it. WHY? [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 3 years ago.
Improve this question
This is my code.
What I think my error is that method code.
import java.util.Scanner;
public class Question1 {
public static void main (String[]args){
String major;
Scanner read = new Scanner (System.in);
System.out.print("Enter character : " );
major=read.next();
}
}
public static void mn(){ //method
if (major==M){
System.out.println("Mathematic");
}
else if (major==C){
System.out.println("Computer Science");
}
else if (major==I){ technology
System.out.println("Information Technology");
}
else {
System.out.println("Invalid Input");
}
}
A couple of things are wrong with your code.
Your method mn lies outside of your class. This is invalid.
Strings should be compared with equals
Your method doesn't take any arguments but requires one in this case.
M, C and I need to be String literals, otherwise Java can interpret them as something different.
You actually need to call the method mn to see its output.
So all in all, your code should look like this
import java.util.Scanner;
public class Question1 {
public static void main (String[]args){
String major;
Scanner read = new Scanner(System.in);
System.out.print("Enter character : " );
major=read.nextLine(); // better nextLine, because otherwise the press of "Enter" will not be registered.
mn(major);
}
public static void mn(String major){ //method
if (major.equals("M")){
System.out.println("Mathematic");
}
else if (major.equals("C")){
System.out.println("Computer Science");
}
else if (major.equals("I")){
System.out.println("Information Technology");
}
else {
System.out.println("Invalid Input");
}
}
}
You don't have any calls to mn() in your main function, so the code inside the function won't run. Add the following line to the end of your main function to make the mn() function run, and add "String major" inside the parenthesis of your mn() function declaration:
mn(major);
I hope this helps!

Unexpected error in simple Java program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
I'm learning Java, and I wanted to make a very basic calculator. But looks like I got a problem right a way!
Here is the code:
import java.util.Scanner;
public class apples{
public static void main(String args[]){
int test = 6;
if(test != 9){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}
So in Eclipse I tried to run this but the problem is it does not work and show this error:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
at apples.main(apples.java:4)
Your second last bracket has some invisible character that you can see a red point(very thin) and if you delete the same your program is perfectly ok.
Incorrect Code having special character in it
import java.util.Scanner;
public class AdvanceCollection {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = scan.nextDouble();
System.out.println("Enter second number: ");
snum = scan.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}
Correct code with out that issue
import java.util.Scanner;
public class AdvanceCollection {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter first number: ");
fnum = scan.nextDouble();
System.out.println("Enter second number: ");
snum = scan.nextDouble();
answer = fnum + snum;
System.out.println(answer);
}
}
Change your character at the end to }.
There is no compilation error, just noticed last } is having some invisible character with it. delete it and type again }. Hopefully it should work.
It works for me.

String cannot be converted to char, how to fix it? [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 5 years ago.
Improve this question
It says "incompatible types: String cannot be converted to char"
How to fix it?
Here is the example.
This is your solution :
public class NewMain {
public static void main(String args[]) throws ParseException {
Scanner s = new Scanner(System.in);
char yes;
do {
System.out.println("Hi");
yes = s.next().charAt(0);
} while (yes == 'Y'); // if u enter 'Y' the it will continue
}
}
To exit enter any thing other then 'Y'

I would like to know how to insert my name into a message. eg:“xxx is taking the course CSCP1014" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
//This program prints welcome to April Semester 2014!
public class April {
public static void main (String [] args) {
System.out.println("Welcome to April Semester 2014!");
}
}
Not sure what your intention is, but maybe this is what you were aiming for:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
String name;
Scanner kb = new Scanner(System.in);
System.out.println("Please enter your name: ");
name = kb.next();
System.out.println(name + " is now taking the course CSP1014");
}
}
You need to take in an input and then process that input into your output.
One way to take in the input would be the Scanner.
Scanner in = new Scanner(System.in);
String name = in.nextLine();

simple java user input, where did i go wrong?? [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 9 years ago.
Improve this question
Hi guys im working on a basic java program to get somebodys name, it comes up with a lot of errors could you correct me where i went wrong. Sorry and thanks. I also need to basicly set the user input to a string variable which i can then later store into a text file.
import java.io.Console;
import java.util.Scanner;
public class HelloJello {
public static void main(String[] args) {
System.out.print("Hi using this program i will find out"
+ "what your name is and store it in document");
Scanner scanner = new Scanner (System.in);
System.out.print("Enter your name");
name = scanner.next(); // Get what the user types.
}
}
what is name ?
You need to declare the variable reference properly
You need to declare data type of name. It should be a String.
public static void main(String[] args) {
String name=""; // always good to initialize
System.out.print("Hi using this program i will find out"
+ "what your name is and store it in document");
Scanner scanner = new Scanner (System.in);
System.out.println("Enter your name");
name = scanner.nextLine(); // Get what the user types. Reads entire line..
}

Categories