I made a simple java program that works on console and I have a error I never had before.
There are no errors in my code but for some reason I can't run the program cause of my 'public class serie' that is never used.
this my code:
import java.math.BigInteger;
import java.util.Scanner;
public class serie {
public final void main(String[] args) throws Exception {
final int BASE = 36;
final BigInteger MODULO = new BigInteger("ZV", BASE);;
Scanner keyboard = new Scanner(System.in);
String strChassisNummer;
String input = "y";
while (input == "y"){
try{
System.out.print("Geef een chasis nummer in:");
strChassisNummer = keyboard.nextLine();
BigInteger chassisNummer = new BigInteger(strChassisNummer,
BASE);
BigInteger remainder = chassisNummer.remainder(MODULO);
System.out.print(strChassisNummer);
System.out.print(";");
String paddedRemainder = remainder.toString(BASE);
if (paddedRemainder.length() == 1)
{
System.out.print("0" + paddedRemainder.toUpperCase());
}
else
{
System.out.print(paddedRemainder.toUpperCase());
}
System.out.println();
System.out.print("Wenst u nog een chasis nummer in te geven ? (y/n): ");
input =keyboard.nextLine();
if (input != "y"){
break;
}
}
catch (Throwable t){
t.printStackTrace();
}
}
}
}
Thanks in advance !
Declare your main method as static, not final. Why have you declared it final in the first place?
Instead of final use static modifier to the main method. The main method should be static with signature allows it to be the entry point of the runnable class.
Your main needs to be static.
Non-static methods require the class to be instansiated and Java doesn't instantiate your class by magic.
Java starts running a program with the specific
public static void main(String[] args)
signature.
So your main method should be
public static void main(String[] args)
and not
public final void main (String[] args)
Related
I am new to coding and I am trying to figure out how to call a method from a separate class and use it in my main class. I know that it is easier to do it in the main class but I was asked to do it in a separate class. The random number that is printed to range from 1 to 3 but using this method I just get 0.
here's the main class:
package example;
public class Example {
public static int number;
public static void main(String[] args) {
otherClass a = new otherClass();
a.assignNumber();
System.out.println(number);
}
}
and my separate class:
package example;
import java.util.Random;
public class otherClass {
public int assignNumber(){
Random num = new Random();
int number = num.nextInt(4) + 1;
return number;
}
}
Default value of number variable is 0 since its type is int. You need to assign the return value of function to number variable.
package example;
public class Example {
public static void main(String[] args) {
otherClass a = new otherClass();
int number = a.assignNumber();
System.out.println(number);
// Or
System.out.println(a.assignNumber());
}
}
The number variable in 'Example' class differs from the number in 'otherClass'.
The easiest way to fix your code is to fix the assignment part:
number = a.assignNumber();
But you should probably check your code again: It is better to use local int variable instead of static field.
package example;
public class Example {
public static void main(String[] args) {
otherClass a = new otherClass();
int number = a.assignNumber();
System.out.println(number);
}
}
There is no real use for the number variable.
It can all be condensed to
package example;
public class Example {
public static void main(String[] args) {
System.out.println(new otherClass().assignNumber());
}
}
I'm taking a coding class using BlueJ, and decided to surprise my teacher with a Text Adventure that uses a Class Runner and another class to have the methods to call. The problem is, I don't know how to use a variable that I established in the Runner, into a method in the Method Class, which I will then call into the Runner. Here is my code:
(This is the runner)
import java.util.*;
public class TextAdventureRunner
{
public static void main (String[]Args)
{
TextAdventureCode run = new TextAdventureCode();
Scanner kb = new Scanner(System.in);
String x = "";
System.out.print("Enter Your Name: : ");
x = kb.nextLine();
System.out.println(x);
run.Hi();
run.HiTwo();
}
}
(This is the code that contains the methods)
import java.util.*;
public class TextAdventureCode extends TextAdventureRunner
{
Scanner kb = new Scanner(System.in);
public static void Hi()
{
System.out.println("Hi" + x);
}
public static void HiTwo()
{
System.out.println("");
}
}
You see, In my method Hi(), there is an error where the x should be. the error reads "cannot find symbol - variable x" even though i extended the class and declared an object in the other class... any help?
You can declare your TextAdventureCode like this:
import java.util.*;
public class TextAdventureCode extends TextAdventureRunner
{
Scanner kb = new Scanner(System.in);
public static void Hi(String x) //Modification
{
System.out.println("Hi" + x);
}
public static void HiTwo()
{
System.out.println("");
}
}
And declare TextAdventureRunner like this:
import java.util.*;
public class TextAdventureRunner
{
public static void main (String[]Args)
{
TextAdventureCode run = new TextAdventureCode();
Scanner kb = new Scanner(System.in);
String x = "";
System.out.print("Enter Your Name: : ");
x = kb.nextLine();
System.out.println(x);
run.Hi(x); // Modification
run.HiTwo();
}
}
i tried to declare a new scanner, it works fine but only at the main.
when i write methods (out of the main of course) it wont recognize the scanner.
import java.util.Scanner;
public class Exe1GenericSort {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
start();
int i = input.nextInt();
}//end main
here it works fine, but at the method "start" it wont let me use "input.next....
tried to write the "Scanner input = new Scanner.... above the main and still wont work...
You need to declare the Scanner as an object outside the main function and then you can use it in other functions.
import java.util.Scanner;
class ScannerTest {
private static Scanner scanner;
public static void main(String[] args){
scanner = new Scanner(System.in);
start();
}
private static void start(){
String input = scanner.nextLine();
System.out.println("Input: " + input);
}
}
NOTE: The scanner object as well as the start function need to be static in order for you to access them inside the main function.
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int x= start(input);
System.out.println("enter another number");
int i = input.nextInt();
System.out.println("a number:"+x);
System.out.println("another number"+i);
}
public static int start(Scanner scan)
{
System.out.println("Please enter a number");
int x = scan.nextInt();
return x;
}
to use Scanner in another method
accept a parameter in the method start() and then return x to test the value then print the value in the main method
solved:
import java.util.Scanner;
public class Exe1GenericSort {
static Scanner input = new Scanner (System.in);
public static void main(String[] args) {
this one works great !
thank for help ppl.
problem solved ! :)
I am building an easy Knock Knock game. I have it finished but I would like to add a small feature. It is currently set to accept only a few words/phrases as the correct response and everything else it produces an "error" of sorts. I want to create a class with a list of acceptable works or phrases and have the users input checked against that class.
Here is the test class I created to play with.
import java.util.*;
public class test
{
static Scanner sc = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Choose a word");
String userEntry = sc.next().toLowerCase();
if (userEntry.equals(test1.*))
{
System.out.println("We found a match");
}
else if (!userEntry.equals(test1.*))
{
System.out.println("We did not find a match");
}
}
}
The code is of the class that will house the variables.
public class test1
{
public static String a = "yes";
public static String b = "hello";
public static String c = "boo";
}
In the first class, I tried using a wild card to call all of the variables in the class but it produces an error. Any help would be greatly appreciated.
You can't do a wildcard search like that in Java.
You really don't need another class, you can just put all the words/phrases in a List and check against the List.
Example:
import java.util.*;
public class test
{
static Scanner sc = new Scanner(System.in);
// add all the words you need into this array
static String [] wordArr = new String[] { "yes", "hello", "boo" };
// this converts the array to a List
static final List<String> WORDS
= new ArrayList<String>(Arrays.asList(wordArr));
public static void main(String[] args)
{
System.out.println("Choose a word");
String userEntry = sc.next().toLowerCase();
// check if the word is in the list
if (WORDS.contains(userEntry))
{
System.out.println("We found a match");
}
else
{
System.out.println("We did not find a match");
}
}
}
Side Note: You should consider creating the Scanner in the main method and close the scanner when you are done with it.
try this
class test1
{
public static list<String> addElement(){
List<String> list=new ArrayList<>();
list.add("yes");
list.add("boo");
list.add("hello");
}
}
class test{
public static void main(String[] args0)
{
Scanner sc=new Scanner(System.in);
String match=sc.next();
ArrayList<String> list=test1.addElement();
for(String test : list)
{
if(test.equels(match)
print("we have found match");
else
print("no match found"):
}
}
}
Hi and hope someone can help. I'm doing a short Java course and need to set up 3 classes that basically communicate between each other but I'm failing.
You'll spot from the code below that I'm trying to split the tasks of reading user's input and doing whatever maths is required into different classes but something's wrong
Any ideas? Thanks for your interest.
Here's the simple Main class:-
public class Main {
public static void main(String[] args) {
Calculator calculator = new Calculator();
calculator.start();
}
}
The Calculator class (or part of it):-
public class Calculator {
private Reader reader;
public void Calculator(Reader reader) {
this.reader = reader;
System.out.println("Calculator set up.");
}
public void start() {
while (true) {
System.out.print("command: ");
String command = reader.readString(); // It fails here:
if (command.equals("end")) {
break;
}
if (command.equals("sum")) {
sum();
} else if (command.equals("difference")) {
difference();
} else if (command.equals("product")) {
product();
}
}
And finally the Reader class:-
public class Reader {
private Scanner input;
public void Reader(Scanner input) {
this.input = input;
System.out.println("Reader set up.");
}
public String readString() {
return input.nextLine();
}
public int readInteger() {
return Integer.parseInt(input.nextLine());
}
}
You have an issue with all the constructors you are using. Constructors are special methods with no return type, so in your case, public void Calculator(Reader reader) needs to be public Calculator(Reader reader) (remove void). The same applies to the other constructors.
Once you do that, you would need to make amendments on how you are instantiating your Calculator class:
Calculator calculator = new Calculator();
Should become:
Calculator calculator = new Calculator(new Reader(new Scanner(System.in)));
provide constructor not mothod.
Calculator(Reader reader) {
this.reader = reader;
System.out.println("Calculator set up.");
}
after that you can change your main method with #subhrajyoti suggested.
apart from above suggestion ,
1> your constructors (Reader and Calculator) is returning void. But constructor cannot return any value. So, remove void keyword.
you have to import Scanner class (i.e. java.util.scanner).