What is identifier expected error?
import java.util.Scanner;
class MyClass {
public static void fizzBuzz(Integer)
{
int x=0,n;
System.out.println("give any number");
Scanner Scan = new Scanner(System.in);
int n = Scan.nextInt();
for(x=0;n<x;x++)
{
if(x==3)
{
System.out.println("fizz");
x=x+1;
}
else if(x==5)
{
System.out.println("buzz");
x=x+1;
}
else
{
System.out.println("x");
x=x+1;
}
}
}
}
error
user_file.java:5: error: <identifier> expected
public static void fizzBuzz(Integer)
^
Two changes
1.) You should have if not done already public static void main(String[] args) { // call your method here}
2.) n is declared twice.
3.) public static void fizzBuzz(Integer) is wrong, variable name is missing.
change to public static void fizzBuzz(Integer a)
int x=0,n; and int n = Scan.nextInt();
Here public static void fizzBuzz(Integer)
You have given only Type Integer not the variable which will hold Integer type value.
public static void fizzBuzz(Integer)
app a variable like code below
public static void fizzBuzz(Integer z)
You have declared n tow time int x=0,n; and at int n = Scan.nextInt();
remove int from second declation.
Related
I want to make a random number guessing game but I keep getting thrown this error:
"java: cannot find symbol
symbol: variable randNum
location:
class Main".
How can I resolve this?
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main (String[] args) {
makeRandNum();
readInput();
checkInput();
}
public static void makeRandNum() {
// creating number generator
Random randNumGen = new Random();
// generates the number
int randNum = randNumGen.nextInt(11);
System.out.println(randNum);
}
public static void readInput() {
//creates a new instance of the util scanner class
Scanner userInput = new Scanner(System.in);
// reads user input (int)
int guessedNum = userInput.nextInt();
}
public static void checkInput(){
if (guessedNum.equals(randNum)) {
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
}
You are using randNum and guessedNum in checkInput() function but the variables are declared in makeRandNum() and readInput() function. To make the values accessible, make the return type of makeRandNum() and readInput() as int and then pass the returned value to the checkInput function as parameter.
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int randNum = makeRandNum();
int guessedNum = readInput();
checkInput(randNum, guessedNum);
}
public static int makeRandNum() {
// creating number generator
Random randNumGen = new Random();
// generates the number
int randNum = randNumGen.nextInt(11);
System.out.println(randNum);
return randNum;
}
public static int readInput() {
// creates a new instance of the util scanner class
Scanner userInput = new Scanner(System.in);
// reads user input (int)
int guessedNum = userInput.nextInt();
return guessedNum;
}
public static void checkInput(int randNum, int guessedNum) {
if (guessedNum == randNum) {
System.out.println("Correct");
} else {
System.out.println("Incorrect");
}
}
}
and the output is as follows :
5
15
Incorrect
I am building an Interactive-Fiction-Game in Java, but am having a trouble using a second class to be used in the main class. I am using Eclipse.
Here is my main Class:
import java.util.Scanner;
import static java.lang.System.out;
public class mainClass {
public static void main(String[] args) {
String cmdIF;
Scanner input = new Scanner(System.in);
int x = 0;
int y = 0;
out.print("Welcome to the world! Which way do you want to go?");
String northD = "You walk into a forest.";
if(x=1) {
out.print(northD);
}
cmdIF = input.nextLine();
choosePath();
}
public void choosePath(actionClass cmdCenter) {
actionClass.cmdCenter();
}
}
And the class that contains the method:
public class actionClass {
public void cmdCenter() {
if(cmdIF.equalsIgnoreCase("NORTH") || cmdIF.equalsIgnoreCase("GO NORTH")) { out.println(northD); x++; }
else if(cmdIF.equalsIgnoreCase("EAST") || cmdIF.equalsIgnoreCase("GO EAST")) { out.println(eastD); y++; }
else if(cmdIF.equalsIgnoreCase("SOUTH") || cmdIF.equalsIgnoreCase("GO SOUTH")) { out.println(southD); x--; }
else if(cmdIF.equalsIgnoreCase("WEST") || cmdIF.equalsIgnoreCase("GO WEST")) { out.println(westD); y--; }
else { out.println("You can't do that."); }
}
}
Whenever I execute the code I get this error:
Exception in thread "main" java.lang.Error: Unresolved compilation
problem: The method choosePath(actionClass) in the type mainClass is
not applicable for the arguments ()
at mainClass.main(mainClass.java:14)
How can I make these methods work together?
in first step you access the method like this
actionClass.cmdCenter();
is not static so just make cmdCenter static like this
public static void cmdCenter
and choosePath() have parameter so you have to use it like this choosePath(actionClass) and since you use it in static method you have to create instance or make choosePath static
public static void choosePath(actionClass)
but you don't have to just remove paramater from choosePath
//public void choosePath(actionClass cmdCenter) {
public static void choosePath()
and seems you have to pass x and y variable to cmdCenter too so its become like this
public static void cmdCenter(String cmdIF, int x, int y)
and rest the code its fine
fixed version of your code :
public class Main {
static String cmdIF;
static int x = 0;
static int y = 0;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Welcome to the world! Which way do you want to go?");
String northD = "You walk into a forest.";
if(x ==1) {
System.out.print(northD);
}
cmdIF = input.nextLine();
choosePath();
}
public static void choosePath() {
actionClass.cmdCenter(cmdIF, x, y);
}
}
and
public class actionClass {
public static void cmdCenter(String cmdIF, int x, int y) {
if(cmdIF.equalsIgnoreCase("NORTH") || cmdIF.equalsIgnoreCase("GO NORTH")) { out.println("GO NORTH"); x++; }
else if(cmdIF.equalsIgnoreCase("EAST") || cmdIF.equalsIgnoreCase("GO EAST")) { out.println("GO EAST"); y++; }
else if(cmdIF.equalsIgnoreCase("SOUTH") || cmdIF.equalsIgnoreCase("GO SOUTH")) { out.println("GO SOUTH"); x--; }
else if(cmdIF.equalsIgnoreCase("WEST") || cmdIF.equalsIgnoreCase("GO WEST")) { out.println("GO WEST"); y--; }
else { System.out.println("You can't do that."); }
}
}
Change the cmdCenter() method in actionClass to be declared as:
public void cmdCenter(String cmdIF)
Then change choosePath() to be
public void choosePath(actionClass action, String cmdIf) {
action.cmdCenter(cmdIf);
}
and obviously change your main() method's call to choosePath() appropriately. And you'll need to new up an instance of actionClass to pass into choosePath().
Hello So I have a entire class called tractor with different data's stored in it but now I'm suppose to create an object call tractor with a zero parameter constructor but This is the code I have so far and its giving em errors
First off this my Tractor Class which is in a different file:
import java.util.Scanner;
class Tractor
{
private int RentalRate;
private int RentalDays;
private int VehicleID;
private int RentalProfit;
public void setRentalRate(int r)
{
Scanner input = new Scanner(System.in);
System.out.println("What's the Rental Rate?");
int num = input.nextInt();
num = r;
if(r<0 || r >1000)
RentalRate = r;
RentalRate= 1;
}
public int getRentalRate()
{
return RentalRate;
}
public void setVehicleID(int v)
{
Scanner input = new Scanner(System.in);
System.out.println("What's the vehicleID?");
int num1 = input.nextInt();
num1 = v;
if(v<0)
VehicleID = v;
VehicleID = 1;
}
public int getVehicleID()
{
return VehicleID;
}
public void setRentalDays(int d)
{
Scanner input = new Scanner(System.in);
System.out.println("How many rental days?");
int num2 = input.nextInt();
num2 = d;
if(d<0)
RentalDays = d;
RentalDays = 1;
}
public int getRentalDays()
{
return RentalDays;
}
public String toString()
{
String str;
str = "RentalDays:" + RentalDays +"\nRenalRate:" + RentalRate + "\nVehicleID " + VehicleID;
return str;
}
public void RentalProfit(int RentalRate, int RentalDays)
{
RentalProfit = RentalRate * RentalDays;
}
}
import java.util.Scanner;
public class testTractor
{
public static void main(String[] args)
{
public tractor()
{
this.RentalDays = d;
this.RentalRate = r;
this.VehicleID = v;
}
}
}
The error is :
testTractor.java:7: error: illegal start of expression
public tractor()
^
testTractor.java:7: error: ';' expected
public tractor()
^
2 errors
You have compilation errors. You need to first declare the Tractor class then add the constructor inside it. One way to do is declare in a separate file. Also in Java unless you had defined d you couldnt have assigned it. Maybe you wanted to assign the day as a String look in the examples I provide below.
You need to to first create a file call Tractor.java and then define variables there. For example contents of Tractor.java:
public class Tractor {
String rentaldays,someOtherValue;
public Tractor(){
rentaldays ="monday";
someOtherValue="value";
}
//or
public Tractor(String rentalDays){
this.rentaldays = rentalDays;
someOtherValue = "asf";
}
}
Then in your main method You can do Tractor trac = new Tractor(); or Tractor trac = new Tractor("tuesday"); also after that you can print the rentaldays of trac using System.out.println(trac.rentaldays);
From the looks of it you will probably be making a tractor rental system. In that case, rentalDays may be an array of Strings. And then you would have an array of Tractor objects to store in the rental system. You can look at these terms and keywords to point you in the right direction.
You are defining it wrong, define your methods inside class then call them in main() method.
class Test{
public void greeting(){
System.out.print("hello to JAVA..");
}
public static void main(String[] args){
Test testObj = new Test();
testObj.greeting();
}
}
you use an illegal of java syntax, if you already have class tractor in your project. for calling it to in other class, try below code
public class TestTractor(){
Tractor objTractor;
public static void main(String[] args){
//create new tractor object with no parameter
objTractor = new Tractor();
//create new tractor object with parameter
objTractor = new Tractor(parameter here);
//do some action of object here
...........
}
}
//This is just a sample
in your tractor class add below code
public tractor()
{
this.RentalDays = d;
this.RentalRate = r;
this.VehicleID = v;
}
And keep your TestTractor class as
public class TestTractor(){
public static void main(String[] args){
Tractor objTractor = new Tractor();
// objTractor.yourMethodName
}
}
When compiling this java program, I get errors like cannot find symbol... any suggestions?
import java.io.*;
import java.util.Scanner;
public class joel001
{
public int d;
// find the smallest number of an array
public static int small(int a[])
{
int smallest=0;
for(int i=0;i<a.length();i++)
{
if(a[i]<smallest)
{
smallest=a[i];
}
}
return smallest;
}
// subtract the smallest number of an array from all its elements
public static int[] sub(int a[],int d)
{
this.d=d;
for(int i=0;i<a.length();i++)
{
a[i]=a[i]-d;
}
return a;
}
// count the array's non zero elements
public static int count(int a[])
{
int countn=0;
for(int i=0;i<a.length();i++)
{
if(a[i]!=0)
{
countn=countn+1;
}
}
return countn;
}
public static void main(String args[])
{
int b,c,z,k=1;
int a[]=new int[1000];
Scanner s=new Scanner(System.in);
b=s.nextInt(); //input
for(i=0;i<b;i++)
{
a[i]=s.nextInt();
}
while(k==1)
{
z=count(a);
if(z==0)
{
break;
}
System.out.println(z);
c=small(a);
a=sub(a,c);
}
}
}
For a start, the length of an array is arr.length, not arr.length().
Secondly, in sub(), there is no this because it's a static function.
Thirdly, in main(), you need to declare i before trying to use it.
That will take care of all your compile-time errors. Run-time, or logic, errors are something you need to learn to fix in a debugger.
You are declaring all your methods of joel001 as static. Static methods are not related to an instance or object of the joel001 class. You need to research OOP before you continue.
Try something like this:
import java.io.*;
import java.util.Scanner;
public class joel001
{
public int d;
// find the smallest number of an array
public int small(int a[])
{
int smallest=0;
for(int i=0;i<a.length();i++)
{
if(a[i]<smallest)
{
smallest=a[i];
}
}
return smallest;
}
// subtract the smallest number of an array from all its elements
public int[] sub(int a[],int d)
{
this.d=d;
for(int i=0;i<a.length();i++)
{
a[i]=a[i]-d;
}
return a;
}
// count the array's non zero elements
public int count(int a[])
{
int countn=0;
for(int i=0;i<a.length();i++)
{
if(a[i]!=0)
{
countn=countn+1;
}
}
return countn;
}
public void main2() {
int b,c,z,k=1;
int a[]=new int[1000];
Scanner s=new Scanner(System.in);
b=s.nextInt(); //input
for(int i=0;i<b;i++)
{
a[i]=s.nextInt();
}
while(k==1)
{
z=count(a);
if(z==0)
{
break;
}
System.out.println(z);
c=small(a);
a=sub(a,c);
}
}
public static void main(String args[])
{
joel001 obj = new joel001();
obj.main2();
}
}
I ran my code... there was an error on line 28. it's the one that says
hey.remove(whatnumber);
I can't figure out what's wrong with it. I tried using debug, but
I don't get how to use it.
Here is the code.
import java.util.ArrayList;
import java.util.Scanner;
public class ACSL_Grid_Fit {
public static void main(String args[]) {
run();
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
}
public static ArrayList<Integer> hey = new ArrayList<Integer>();
public static int howmany;
public static int whatnumber;
public static int choice;
public static Scanner sc = new Scanner(System.in);
public static void run() {
input();
countcalc();
}
public static void input() {
{
sc.useDelimiter(", |\n");
howmany= sc.nextInt();
for(int x = 1; x<=howmany;x++) {
whatnumber = sc.nextInt();
hey.remove(whatnumber);
}
}
choice = sc.nextInt();
switch(choice) {
case 1:
int i = 0;
while(i<hey.get(0)) {
i++;
}
System.out.println(i);
hey.remove(i);
case 2:
case 3:
}
}
public static void countcalc() {
}
}
To avoid an IndexOutOfBoundsException, you need to add values to list before you try and run()
And with this code here, you have to keep in mind there is no index 0
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
You are starting the index at 1, so 0 is null;
Edit: I just caught this
You're trying to run() before you even have any values in your list
public static void main(String args[]) {
run();
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
}
Just switch them around
public static void main(String args[]) {
for(int w=1;w<=25;w++) {
hey.add(w,w);
}
run();
}
EDIT: I knew this was a problem to begin with
hey.add(w,w);
Your trying to add at index w when no index w exists. hey has a size of 0, until you add to it.
Just do this.
for(int w=1;w<=25;w++) {
hey.add(w);
}
When you want to remove the numbers use this
hey.remove(whatnumber - 1)
// example, since 20 is at index 19, you want to remove whatnumber - 1