import java.util.Scanner;
public class KillBill {
public KillBill() {
// TODO Auto-generated constructor stub
}
public static void Main(String[] args) {
// TODO Auto-generated method stub
Scanner Scan = new Scanner (System.in);
}
}
IN 11th LINE .IT SAYS THE VALUE OF LOCAL VARIABLE SCAN IS NOT USED
If a variable is set and not used, it gives an error. You could correct the warning by using Scan, with something like:
public static void Main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner (System.in);
String text = scan.nextLine();
System.out.println(text);
}
The idea here is that every variable is being used now.
Well, to be entirely honest, you're Scanner object ISN'T BEING USED elsewhere in the code you've provided. But don't worry, this is just a warning (yellow jagged underline) and not an error (red jagged underline). In this case, it's just telling you that your program, at the moment, has an unused variable, whose deletion won't affect the code than what it currently is. Remember, a computer still has to read each line, and reading unused variables, especially objects, might slow your program down, even though by only a fraction of a millisecond. So it is advised to delete this unnecessary lines of code.
But remember, this is all relevant for the CURRENT situation, and not if you're gonna make changes to your program, by literally USING your unused variable.
Try this.
public class KillBill {
public KillBill(Scanner scan) {
// TODO Auto-generated constructor stub
int value = scan.nextInt();
System.out.println(value);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner Scan = new Scanner (System.in);
new KillBill(Scan);
}
}
That problem/warning is caused by unused variable. Not a serious issue.
This is not a problem to declare Scanner and not use it, the real problem is the signature of your main method signature, it should not UpperCase you have to use :
public static void main(String[] args) {
// ^^-----------------In your program it is M
Note java use CamelCase your names of variables shold start with lower case for the good practice (Scan should be scan)
Related
I've done a bunch research into trying to solve this issue (for about 2.5 hours), but I'm still not able to compile my program. I have tried making the method not static, but when attempting to run it, it gives me this error:
"Error: Main method is not static in class prog6, please define the
main method as: public static void main(String[] args)"
When the main method is static, I get following error in a compiler
Error: "non-static variable input cannot be referenced from a static
context
usd = input.nextDouble();"
I'm sorry if this question comes off redundant, I don't mean to ask without looking for an answer on my own, but I've been working at this for hours now and I don't understand what I'm doing wrong.
Some extra info on this program: it's meant to take inputs from the user to find out what currency they want to convert to, and how much USD they would like to convert. Then, I would invoke a method in order to do the calculations and return them. (Any amount trying to be converted over $200, will need 5% fee.)
import java.util.Scanner;
public class prog6
{
Scanner input = new Scanner(System.in);
public static void main (String[] args)
{
char curr = 0;
double usd;
double result;
while (curr!='Q' || curr!='q') { //loop
System.out.println("What type of currency would you like to buy?");
curr = input.next().charAt(0);
System.out.println("How many dollars would you like to convert?");
usd = input.nextDouble(); //asking user for info needed to convert
if (usd>200) {
usd = (usd)*(0.95);
}
result = calc (curr,usd); //invoke the method
}
}
public double calc (char mCurr,double mUsd) //method
{
if (mCurr=='E' || mCurr=='e') {
return (mUsd)*(0.88);
}
else if (mCurr=='P' || mCurr=='p') {
return (mUsd)*(0.77);
}
else if (mCurr=='Y' || mCurr=='y') {
return (mUsd)*(113.17);
}
return 0;
}
}
The main method will need to be static. From there, create an instance of your class and call a non-static method from the static main method. eg..
public class Prog6 {
private Scanner input = new Scanner(System.in);
public static void main (String[] args) {
Prog6 prog6 = new Prog6();
prog6.start();
}
public void start() {
char curr = 0;
double usd;
double result;
// etc...
}
}
You could make the member variable static but it's better form to use regular non-static members and methods and call this from the static main method.
There are two ways to solve your prolem
Change the input variable to static;
or
In main method, prog6 myprog= new prog6(); and refer input as myprog.input ....
public class practiceclock{
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 5;
addTonum(x);
System.out.println(gby);
}
public static void addTonum(int gby) {
gby = gby + 1;
}
}
Why doesn't this print out 6? I'm trying to print out the integer gby from my "addTonum" function. I know it's pretty basic.
In java, when you pass a variable as an argument into a method, you are passing a copy of it, not the original instance. To fix this, you can either make the variable global, or you can have the method return the integer.
Also, use
gby +=1;
As a shortcut.
I'm running this simple program to play around with arrays and nested for loops. For some reason my compiler can't identify the variable "r"? I don't know why it is doing this. Any suggestions?
public class ForLoop {
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] mat = new int[4][8];
for(int r=0;r<mat.length;r++);
{
for(int c=0;c<mat[r].length;c++)
{
mat[r][c]=r*c+c/2+r*(c+1);
}
System.out.println(mat[0][2]);
}
}
}
The semicolon terminates the for body immediately (as an empty expression) here
for(int r=0;r<mat.length;r++);
{ //<-- not part of the for.
remove the semicolon so that the next block is part of the for loop
for(int r=0;r<mat.length;r++) {
I was doing some programming (self taught) and I have been struggling with an else statement but I fixed it. Now when I run it, it runs the wrong reply.. If that makes sense. I hope I am clear enough for you to help!w
import java.util.Scanner;
public class Test {
private static Scanner scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
String answer;
scanner = new Scanner(System.in);
System.out.println("Hello Human");
System.out.println("Do you want to build a snowman?");
answer = scanner.next();
if (answer.equals("Yes"))
System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
{
else
System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.
}
}
}
You provided the wrong braces for if-else block. Please check the below code.
You can use equalsIgnoreCase() for case in-sensitivity for the user input.
import java.util.Scanner;
public class Test {
private static Scanner scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
String answer;
scanner = new Scanner(System.in);
System.out.println("Hello Human");
System.out.println("Do you want to build a snowman?");
answer = scanner.next();
if (answer.equalsIgnoreCase("Yes")) {
System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
}
else {
System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.
}
}
}
PS: The if andelse` block here, are having only single statements, you if you want then you can remove them completely. But you have more than one then you must use the brackets.
The position of your brace: { is wrong. The message doesn't fall inside the if block and it'll always get printed. It should be as below,
import java.util.Scanner;
public class Test {
private static Scanner scanner;
public static void main(String[] args) {
// TODO Auto-generated method stub
String answer;
scanner = new Scanner(System.in);
System.out.println("Hello Human");
System.out.println("Do you want to build a snowman?");
answer = scanner.next();
if (answer.equals("Yes")) {
System.out.println("Yay, Now you must think hard of what Olaf Looks like Okay? (say okay to coninue)");
{ else
System.out.println("Go away! I hate you"); //This happens when I try to say Yes.. This is meant for no.
}
}
}
My code so far:
import java.util.*;
import java.util.Scanner.*;
public class Project{ // The Main Method
public static void main(String [] args){ // Creates the Main Method
System.out.println("Name a Method (Stability, efficiency ..)"); // Asks the user to select a method
Scanner scan = new Scanner(System.in); // Creates the Scanner
String splash = scan.nextLine(); // Transitions the user to the next line after choosing a method
if(splash.equals("efficiency")) // If users chooses Efficiency then it goes to the Efficiency method
{
efficiency(); // Calls the Efficiency method
}
if(splash.equals("Stability")) // If user chooses Stability then it goes to the Stability Method
{
stable(); // Calls the Stability method
}
else // What happens if the input wasnt recognized
{
System.out.println("I don't recognize this"); // what happens if an irrelevant method is chosen
}
}
}
How would I make it so that instead of:
else // What happens if the input wasnt recognized
{
System.out.println("I don't recognize this"); // what happens if an irrelevant method is chosen
}
It will refresh or restart the main method?
Wrap your code in a while loop which you leave when the user chooses the exit command:
public static void main(String [] args){
while (true) {
System.out.println("Name a Method (Stability, efficiency ..)");
Scanner scan = new Scanner(System.in);
String splash = scan.nextLine();
if (splash.equals("exit")) {
break;
} // else if (splash.equals("efficiency")) ...
}
}