Hello I'm having issues running my method bugScan which I think is because I've declared myBugs as an array of objects. If any can give me any hints or point me the right direction of sorting this out I would very much appreciate it :) Below is all the appropriate classes:
package mainFuncs;
import java.util.Scanner;
public class aBug {
String species, name;
int x, y, energy, id, foodpref;
char symbol;
public void bugScan(aBug[] bugObjects, Scanner scan) {
String inSpecies, inName;
int inX, inY, inEnergy, inId;
for(int i = 0; i < bugObjects.length; i++)
{
System.out.print("Species: ");
inSpecies = scan.nextLine();
System.out.print("Name: ");
inName = scan.nextLine();
System.out.print("X position: ");
inX = scan.nextInt();
System.out.print("Y position: ");
inY = scan.nextInt();
System.out.print("Energy: ");
inEnergy = scan.nextInt();
System.out.print("ID: ");
inId = scan.nextInt();
bugObjects[i].symbol = '*';
bugObjects[i].species = inSpecies;
bugObjects[i].name = inName;
bugObjects[i].x = inX;
bugObjects[i].y = inY;
bugObjects[i].energy = inEnergy;
bugObjects[i].id = inId;
}
}
}
This is the other class:
package mainFuncs;
import java.util.Scanner;
public class mainMenu {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
aBug[] myBugs = new aBug[5];
myBugs.bugScan(myBugs, scan);
System.out.print("hi");
}
}
My issue is occurs at the line:
myBugs.bugScan(myBugs, scan);
with the error message "Cannot invoke bugScan(aBug[], Scanner) on the array type aBug[]"
You defined the method in the aBug class, but you trying to call it on an array of aBug objects! Anyway, I think you can take the bugScam method out from the first class and place it inside the second class.
package mainFuncs;
import java.util.Scanner;
public class aBug {
String species, name;
int x, y, energy, id, foodpref;
char symbol;
}
Second class:
package mainFuncs;
import java.util.Scanner;
public class mainMenu {
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
aBug[] myBugs = new aBug[5];
bugScan(myBugs, scan);
System.out.print("hi");
}
public static void bugScan(aBug[] bugObjects, Scanner scan) {
String inSpecies, inName;
int inX, inY, inEnergy, inId;
for(int i = 0; i < bugObjects.length; i++)
{
System.out.print("Species: ");
inSpecies = scan.nextLine();
System.out.print("Name: ");
inName = scan.nextLine();
System.out.print("X position: ");
inX = scan.nextInt();
System.out.print("Y position: ");
inY = scan.nextInt();
System.out.print("Energy: ");
inEnergy = scan.nextInt();
System.out.print("ID: ");
inId = scan.nextInt();
bugObjects[i].symbol = '*';
bugObjects[i].species = inSpecies;
bugObjects[i].name = inName;
bugObjects[i].x = inX;
bugObjects[i].y = inY;
bugObjects[i].energy = inEnergy;
bugObjects[i].id = inId;
}
}
}
The typ parameter aBug will not be acceptable. By own written classes, the name should be written in camel case, so that could be the case of the exception.
the problem is with the line you mentioned!!
myBugs is an array and does not have any method bugScan
it should be something like this
new aScan().bugScan(myBugs, scan);
myBugs is an array, so you will only be able to perform array operations on it. Your method bugScan() is defined inside the aBug class. If you want to run it you can solve it by doing:
Bug tmp = new aBug();
tmp.bugScan(myBugs, scan);
But this smells like bad coding, the aBug class shouldn't have the responsibility the 'scan for bugs'.
On a side note: By convetion class names start with a capital and even if you apply the fix it will throw a NullPointerException because you never initialize your array.
Related
getting error that variable not found. not able to figure out exact problem. tried resetting preferences also in bluej.
import java.util.*;
class Electricity
{
public void Initialization()
{
int omr = 0;
int nmr = 0;
int cr = 0;
int rent = 0;
double cost = 0.0;
double sc = 0.0;
}
public void input()
{
System.out.println("Enter old meter reading");
omr = sc.nextInt();
System.out.println("Enter new meter reading");
nmr = sc.nextInt();
}
public void calculate()
{
cr = nmr - omr;
}
public static void main()
{
Scanner sc = new Scanner(System.in);
}
}
This is not error in bluej it's problem with the code
I'm guessing you are a student
The code is not working since variable only exist inside the method it was created in
You are trying to access variable defined in different methods.
Inside the input() method you trying to access variable that you defined in Initialization() method But it does not exist in input()
Jest to make this code work put all your code inside the main() method .
Or pass the method the variables they using from another method and of course you need to call those methods from main method but seems like you have some more learning to do to make the second option to work.
Good luck.
Somthing like that.
import java.util.Scanner; // Import the Scanner class
public class Main
{
static int calculate(int nmr, int omr)
{
int cr = nmr - omr;
return cr;
}
public static void main(String[] args)
{
int omr = 0;
int nmr = 0;
// int cr = 0;
// int rent = 0;
// double cost = 0.0;
// double sc = 0.0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter old meter reading");
omr = sc.nextInt();
System.out.println("Enter new meter reading");
nmr = sc.nextInt();
System.out.println("The resaults: " + calculate(nmr, omr));
}
}
I am trying to call a method, readAndFill(); in my main method. I am getting an error message saying " readAndFill cannot be applied to given types. readAndFill is a method to just put numbers into an array.
I've renaming the method, putting readAndFill(double[]array, int numOfNumbers) instead of readAndFill();.
package program9;
import java.util.Scanner;
import java.util.Arrays;
public class Program9 {
static void readAndFill(double[]array, int numOfNumbers) {
Scanner keyboard = new Scanner(System.in);
for(int i=0;i < numOfNumbers; i++){
System.out.print("Enter a number: ");
array[i] = keyboard.nextDouble();
}
}
public static void main(String[] args) {
int numOfNumbers;
Scanner keyboard = new Scanner(System.in);
double arrayNumbers;
System.out.print("How many numbers will be read in the array?:
");
numOfNumbers = keyboard.nextInt();
double[] array = new double[numOfNumbers];
readAndFill();
}
static void readAndFill(double[]array, int numOfNumbers) {
Scanner keyboard = new Scanner(System.in);
for(int i=0;i < numOfNumbers; i++){
System.out.print("Enter a number: ");
array[i] = keyboard.nextDouble();
}
}
readAndFill methods need two parameter.
Replace the method call in main() with below.
readAndFill(array , numOfNumbers);
call your function this way readAndFill(array , numOfNumbers);
I am new to Java and am wondering whats wrong with the code below. I am trying to create multiple objects as an array if this is possible? The code will run and ask for a name, however just end after this and im not sure why. Any help would be great, thanks in advance.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
ABug[] BugObj = new ABug[3]; //Creating object BugObj of class ABug
for (int i=1; i<4; i++){
Scanner reader = new Scanner(System.in);
System.out.println("Please enter the name of the bug:");
BugObj[i].name = reader.next();
System.out.println("Please enter the species of the bug:");
BugObj[i].species = reader.next();
System.out.println("Name: " + BugObj[i].name); //Printing bug information out
System.out.println("Species: " + BugObj[i].species);
}
}
}
class ABug {
int horpos, vertpos, energy, id;
char symbol;
String species, name;
}
You have two issues:
You need to have an instance of the object you are going to use.
The way to manage a for loop.
You can modify your source code to this:
Scanner reader = new Scanner(System.in); // Take out this from inside for loop
for (int i = 0; i < BugObj.length; i++) { // Notice we use BugObj.length instead of a number and start index at 0.
System.out.println("Please enter the name of the bug:");
BugObj[i] = new ABug(); // You need to initialize the instance before use it
BugObj[i].name = reader.next();
You have to create objects to store the data first.
import java.util.Scanner;
public class test {
public static void main(String[] args) {
ABug[] BugObj = new ABug[3]; //Creating object BugObj of class ABug
for (int i=1; i<4; i++){
BugObj[i] = new ABug(); // add this line
Scanner reader = new Scanner(System.in);
System.out.println("Please enter the name of the bug:");
BugObj[i].name = reader.next();
System.out.println("Please enter the species of the bug:");
BugObj[i].species = reader.next();
System.out.println("Name: " + BugObj[i].name); //Printing bug information out
System.out.println("Species: " + BugObj[i].species);
}
}
}
class ABug {
int horpos, vertpos, energy, id;
char symbol;
String species, name;
}
Two errors :
initialize object before use : you cannot write BugObj[i].name before writing this BugObj[i] = new ABug();
array bounds, Java array begin at [0] but not [1], so use for (int i=0; i<3; i++) instead of for (int i=1; i<4; i++)
The final code :
public class test {
public static void main(String[] args) {
ABug[] BugObj = new ABug[3];
for (int i=0; i<3; i++){
Scanner reader = new Scanner(System.in);
BugObj[i] = new ABug();
System.out.println("Please enter the name of the bug:");
BugObj[i].name = reader.next();
System.out.println("Please enter the species of the bug:");
BugObj[i].species = reader.next();
System.out.println("Name: " + BugObj[i].name); //Printing bug information out
System.out.println("Species: " + BugObj[i].species);
}
}
}
class ABug {
int horpos, vertpos, energy, id;
char symbol;
String species, name;
}
Hello I'm still new to Java and OOP and am having issues trying to get my code to compile. I understand the issue with my code is the instantiation of the same object twice however I'm not sure how I can work around that to compile my code.
package week2;
import java.util.*
public class aBug {
aBug theBug = new aBug();
String Inspecies, Inname;
int Inx, Iny, Inenergy, Inid;
char Insymbol;
Scanner scan = new Scanner(System.in);
aWorld newWorld = new aWorld();
public static void main(String[] args) {
aBug theBug = new aBug();
theBug.mainMenu();
}
public void mainMenu() {
int choice;
do {
System.out.print("1\t Create bug\n");
System.out.print("2\t Enter world\n");
System.out.print("3\t Quit\n");
choice = scan.nextInt();
switch (choice) {
case 1:
bugInit();
break;
case 2:
System.out.println();
newWorld.mapInit(theBug.Inx, theBug.Iny, theBug.Insymbol);
System.out.println();
break;
}
} while (choice != 3);
}
public void bugInit() {
String species, name;
int x, y, energy, id;
char symbol;
System.out.print("Species: ");
species = scan.nextLine();
System.out.print("Name: ");
name = scan.nextLine();
System.out.print("X position: ");
x = scan.nextInt();
System.out.print("Y position: ");
y = scan.nextInt();
System.out.print("Energy: ");
energy = scan.nextInt();
System.out.print("ID: ");
id = scan.nextInt();
theBug.Insymbol = 'b';
theBug.Inspecies = species;
theBug.Inname = name;
theBug.Inx = x;
theBug.Iny = y;
theBug.Inenergy = energy;
theBug.Inid = id;
System.out
.format("\nThe bug is of the species %s, called %s, "
+ "with positions %d & %d, with energy amount: %d, and %d as it's id number\n\n",
theBug.Inspecies, theBug.Inname, theBug.Inx, theBug.Iny,
theBug.Inenergy, theBug.Inid);
}
}
In the constructor you have:
public class aBug {
aBug theBug = new aBug();
...
}
So while creating an instance of aBug (for example in your main) you call new aBug(), which calls the constructor again recurrently with out end overflowing the stack.
I am not sure why do you think you need to create an instance of the object within itself. So it's hard to give any hints. If I guess correctly, you merged the idea of aBug and "main program" in one class. You should split it and put aBug internal stuff in its own class.
I keep getting the message
Exception in thread "main" java.lang.NoSuchMethodError: WeatherInput:
method ()V not found
in my client class for this project I'm working on and I'm not sure why, I was wondering if anyone here could see what I'm missing?
Here's my service class:
class WeatherInput
{
private static final String NEWLINE = "\n";
private double temp;
private double tempAverage;
private double tempHigh;
private double tempLow;
private double tempHundred;
public String newCity = new String();
public String city = new String();
public WeatherInput( String city) {
temp = 0;
tempAverage = 0;
tempHigh = 0;
tempLow = 0;
tempHundred = 0;
newCity = city;
}
public void setTemp( double temprature)
{
temp = temp;
}
public void tempCalc(String city)
{
while(!city.equals("*"))
{
city = newCity;
}
while(temp != 0)
{
tempAverage = (temp + temp)/2;
if(tempHigh > temp)
tempHigh = temp;
if(tempLow < temp)
tempLow = temp;
if(temp > 100)
tempHundred = temp;
temp++;
}
System.out.print(tempAverage);
}
public String printString(){
return NEWLINE + "Statistics for: " + newCity + "Average: " + tempAverage + "High: " + tempHigh + "Low: " + "Over 100: "
+ tempHundred + NEWLINE;
}
}
And this is my client:
import java.util.*; //For Scanner and class
//----------------------------------------------------------------------------------
//Class Name: KosmowsiProg2
//Description: This class has one method, the main method
//----------------------------------------------------------------------------------
public class KosmowskiProg2
{
//-------------------------------------------------------------------------------
//Method Name: main
//-------------------------------------------------------------------------------
public static void main(String[] args)
{
//-----------------------Local Constants--------------------------------------
//-----------------------Local Variables--------------------------------------
double newTemp;
//-----------------------Objects----------------------------------------------
Scanner scanner = new Scanner(System.in);
String city = new String();
String inputCity = new String();
WeatherInput input = new WeatherInput();
WeatherInput input2 = new WeatherInput();
//---------------------Method Body--------------------------------------------
System.out.print("Please enter the names of the cities, ending in a *");
inputCity = scanner.next();
System.out.print("Please enter the tempratures, ending in a 0");
newTemp = scanner.nextDouble();
input.setTemp(newTemp);
input.tempCalc(inputCity);
}//End main method
private static void printResults(WeatherInput in){
System.out.print(in.printString());
}
}//End class KosmowskiProg2
I'm creating a service class that I can then pass to a client class that will read a list of cities that ends in a "*" and then for each city read a list of tempratures that ends with a "0". It then has to calculate the average temperature, highest and lowest, and then output them along with any temperatures over 100. I've talked to my professor and she told me that the solution is a nested while loop, and that the prompts for the final program should be in a separate client class. I also need to output the totals for each category. The thing is, I'm having a lot of trouble trying to figure out how to use this service class I've created in the client to get the data I need. My professor can't seem to explain it in a way I can understand, so I'm hoping for some help.
WeatherInput requires a String as part of it's constructor...
public WeatherInput( String city)
But you're trying to insansiate it with out any...
WeatherInput input = new WeatherInput(); // This is bad, must have a String value...
If you have your service class in a different file, you need to make sure it's declared public.
public class WeatherInput
{
... etc
}
If you don't you may not be able to access the constructor in the other file.
EDIT: Oh, i just noticed, you don't have a default constructor (one with no arguments), which is why it throws the NoSuchMethodError exception. Your only constructor needs a city name:
public WeatherInput(String city) {
... etc
}
You'll need to declare your classes after you have the city names:
// WeatherInput input = new WeatherInput();
// WeatherInput input2 = new WeatherInput();
//---------------------Method Body--------------------------------------------
System.out.print("Please enter the names of the cities, ending in a *");
inputCity = scanner.next();
System.out.print("Please enter the tempratures, ending in a 0");
newTemp = scanner.nextDouble();
WeatherInput input = new WeatherInput(inputCity);
input.setTemp(newTemp);
input.tempCalc(inputCity);
Or create a constructor that doesn't take any arguments:
public WeatherInput() {
temp = 0;
tempAverage = 0;
tempHigh = 0;
tempLow = 0;
tempHundred = 0;
newCity = null;
}