cannot find symbol - ArrayList; cannot find symbol - Integer [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I have imported both ArrayList and Integer. In my code I use the symbols in multiple places but I only get an error on one line.
Here are the import statements:
import java.util.ArrayList;
import java.lang.Integer;
Here are the pieces of code that compile properly:
ArrayList<Integer> primes = new ArrayList<Integer>();
primes.add(2);
primes.add(3);
primes.add(5);
primes.add(7);
private static void addPrime(ArrayList<Integer> primes)
{
int newNumber;
int x;
}
This piece of code return the symbol errors listed above:
while (lastValue < half)
{
addPrime(ArrayList<Integer> primes);
lastValue = primes.get(primes.size()-1);
}
I have done research on the problem but the only answer I can find is that the symbols were not imported properly at the beginning of the program.

A little background if you are not aware of already:
private static void addPrime(ArrayList<Integer> primes)
{
int newNumber;
int x;
}
is the method definition, where we define what the method will do for us. To make it do something for us we call the method passing the arguments, eg. addPrime(argument). Here you got to pass actual argument variable of type which you have defined in method definition.
So addPrime(primes) will work for you.

You don't need to specify the value type when calling a function. So instead of addPrime(ArrayList<Integer> primes); just simply do addPrime(primes);

The addPrime(...) function you have does not do anything in the example you gave.
import java.util.ArrayList;
import java.lang.Integer;
public class test
{
private static ArrayList<Integer> addPrime(ArrayList<Integer> primes, int num)
{
//Seems like you are trying to add primes to the list?
primes.add(num);
return primes;
}
public static void main(String[] args)
{
ArrayList<Integer> primes = new ArrayList<Integer>();
primes.add(2);
primes.add(3);
primes.add(5);
primes.add(7);
//Usage looks like
primes = addPrime(primes, 13);
// This will return 13 as the number 13 is the 5th element
System.out.println(primes.get(4));
}
}
Coded a bit better would be creating an object class so you do not keep passing around an array list
import java.util.ArrayList;
import java.lang.Integer;
public class IntArrList
{
private ArrayList<Integer> primes;
public IntArrList() {
primes = new ArrayList<Integer>();
}
public void addPrime(int num)
{
primes.add(num);
}
public int getPrimeListValue(int index)
{
return primes.get(index);
}
public ArrayList<Integer> getPrimeList()
{
return primes;
}
}
public class test
{
public static void main(String[] args)
{
IntArrList arrL = new IntArrList();
arrL.addPrime(2);
arrL.addPrime(3);
arrL.addPrime(5);
arrL.addPrime(7);
// This will return 7 as the number 7 is the 4th element
System.out.println(arrL.getPrimeListValue(3));
}
}

Related

Java ArrayList size() method return 0 instead of actual length of the ArrayList [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed last year.
Improve this question
I am learning Java and followed a tutorial like this:
package segovia.java.learn;
import java.util.ArrayList;
public class GroceryList {
private ArrayList<String> groceryList = new ArrayList<String>();
public void addGroceryItem(String item) {
groceryList.add(item);
}
public void printGroceryList() {
System.out.println("You have " + groceryList.size() + " items in your grocery list.");
for (int i=0; i < groceryList.size(); i++) {
System.out.println((i+1) + ". " + groceryList.get(i));
}
}
public int getLength(){
return groceryList.size();
}
}
package segovia.java.learn;
public class Main {
public static void main(String[] args) {
GroceryList myGroceries = new GroceryList();
myGroceries.addGroceryItem("apple");
myGroceries.addGroceryItem("orange");
myGroceries.addGroceryItem("pork");
myGroceries.addGroceryItem("beef");
myGroceries.printGroceryList();
System.out.println(myGroceries.getLength());
}
}
which worked fine. However, I thought that since the myGroceries is an ArrayList, so it should have the .size() method. So I added a line of
System.out.println(myGroceries.size());
but IntelliJ told me it's not working. So I thought that MAYBE I should use inheritance so I changed to
public class GroceryList extends ArrayList {
now the .size() method is working, but it gave me a wrong value.
Final codes are like this:
package segovia.java.learn;
import java.util.ArrayList;
public class GroceryList extends ArrayList{
private ArrayList<String> groceryList = new ArrayList<String>();
public void addGroceryItem(String item) {
groceryList.add(item);
}
public void printGroceryList() {
System.out.println("You have " + groceryList.size() + " items in your grocery list.");
for (int i=0; i < groceryList.size(); i++) {
System.out.println((i+1) + ". " + groceryList.get(i));
}
}
public int getLength(){
return groceryList.size();
}
}
package segovia.java.learn;
public class Main {
public static void main(String[] args) {
GroceryList myGroceries = new GroceryList();
myGroceries.addGroceryItem("apple");
myGroceries.addGroceryItem("orange");
myGroceries.addGroceryItem("pork");
myGroceries.addGroceryItem("beef");
myGroceries.printGroceryList();
System.out.println(myGroceries.getLength());
System.out.println(myGroceries.size());
}
}
and the getLenth() gave me 4, which is correct, while the .size() gave me 0, which is obviously wrong.
Can anyone help me understand why the codes work like this? I guess it's related to the class inheritance but not sure.
I think there is a misunderstanding of inheritance vs composition. In this case, before you added extends ArrayList to GroceryList, your GroceryList has a list. That's why .size() wasn't working on it, because the GroceryList you defined doesn't have a size method. It does have a list called groceryList though. That's why after you addGroceryItem, the getLength method works correctly. It's getting the size of groceryList.
After you added extends ArrayList, GroceryList now is an ArrayList, that's why it has a size method now. But it is not the same list as groceryList. Hence getting the size of myGroceries, after addGroceryItem doesn't give you the size you expected. Because you never added anything to the ArrayList that is myGroceries. You've only added thing to the groceryList that is a field in myGroceries.

Anyone can help to fix this run time error? [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 1 year ago.
Improve this question
Using arraylist and trying to get the last element, i am getting some runtime error.
import java.util.ArrayList;
public class MyProgram{
private ArrayList<String> list = new ArrayList<String>();
public void printLastThing(){
int lastIndex = list.size() - 1;
String thing = list.get(lastIndex);
System.out.println(thing);
}
public static void main(String[] args){
MyProgram example = new MyProgram();
example.printLastThing();
}
}
There are no elements added to the list and so it is empty. YOu are trying to fetch the element at -1 which will raise java.lang.IndexOutOfBoundsException.
In this case you should keep a check for going out of bounds.
I have added a method to check the lastindex. It will return -1 if list is emtpy and you can display that List if empty once you get -1.
import java.util.ArrayList;
public class MyProgram{
private ArrayList<String> list = new ArrayList<String>();
public MyProgram() {
list.add("a");
list.add("z");
}
public void printLastThing(){
int lastIndex = getLastIndex();
if(lastIndex >= 0)
System.out.println(list.get(lastIndex));
else
System.out.println("List is empty");
}
private int getLastIndex() {
if(list.size()==0) {
return -1;
}
return list.size() - 1;
}
public static void main(String[] args){
MyProgram example = new MyProgram();
example.printLastThing();
}
}

how to make an array function return a value [duplicate]

This question already has answers here:
How can I convert List<Integer> to int[] in Java? [duplicate]
(16 answers)
Closed 2 years ago.
So to summarize my problem, I want the function int[] get_marks() to be able to return the array, but I don't know how to get my ArrayList marks to convert into an array which I can use the return thing for. I need the arraylist to be converted because I need to be able to find the frequency and mode of the array (parts of the code I have not started because I can't figure out how to convert the arraylist).
Below is my comment less code so far.
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
class Main {
public static int[] get_marks(){
Scanner input = new Scanner(System.in);
ArrayList<Integer> marks = new ArrayList<>();
final int FLAG = -1;
int entries = 0;
System.out.println("Enter your marks. Type -1 when done.");
while (entries != FLAG){
entries = input.nextInt();
if (entries >=0 && entries <= 100){
marks.add(entries);
} else if (entries == FLAG) {
break;
} else {
System.out.println("Invalid entry. Marks must be between 0-100.");
}
}
input.close();
System.out.println("Your Marks are: " + marks);
}
public static void main(String[] args) {
get_marks();
System.out.println();
}
}
There's no reason not to simply return an ArrayList<Integer> or even better a List<Integer>. If you insist on returning an int[], your question is a duplicate of this.
Why not just return the ArrayList marks?
public static ArrayList<Integer> get_marks(){return marks}
Also, you could make the method return void. Is there any reason you need it to return a value in main?
To elaborate, if you make it void, it will just do all the calculations and culminate in System.out.println("Your Marks are: " + marks);. Given your code, is that not what you want? If so, make get_marks() return void:
public static void get_marks() {//run the code, print the marks, etc.}
Why do you need the get_marks() method? You may not have posted all your code, but if this is all your code, just get rid of get_marks() and put it all in main(). If all main() does is call get_marks(), consolidate the methods.

Why is my program just terminating? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
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.
Closed 6 years ago.
Improve this question
Program runs than terminates, why? Kind of confused because there are no syntax errors, Please explain why it terminates, thank you. fairly new to java and using arrays.
import java.text.*;
import java.util.*;
public class randomizer {
public void randomizer() throws InterruptedException
{
randomizer r = new randomizer();
int[] numbers = {3,7,2,62,1,53,16,563,12,13,75};
Calendar rightNow = Calendar.getInstance();
int hour = rightNow.get(Calendar.HOUR_OF_DAY);
int minute = rightNow.get(Calendar.MINUTE);
int seconds = rightNow.get(Calendar.SECOND);
int[] numbers2 = {10,32,61,2,5};
int[] date = {hour,minute,seconds};
int RandomNumber = (r.getRandom(date) * r.getRandom(numbers)) + r.getRandom(numbers2);
while(true) // just for test case purposes
{
Thread.sleep(1000);
System.out.println(RandomNumber);
}
}
public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
public static void main(String[] args) {
randomizer r = new randomizer();
}
}
Your randomizer class has no constructor defined, so constructing it in your void main method does nothing.
Of note, your class does have a void randomizer() method defined - this is probably a bug. Note, classes should be Pascal case (Randomizer and methods should be camel case (randomizer).
the app is terminated and doing nothing because of this:
public static void main(String[] args) {
randomizer r = new randomizer();
}
here you create an object of the class randomizer so far so good..
but in the code you have this:
public void randomizer() throws InterruptedException
{....
....
}
and that is not a constructor, that is a method... so as long as you dont call it is not executed...
you need to either change that to a constructor by doing
randomizer() throws InterruptedException
{....
....
}
or calling it in the main method
public static void main(String[] args) {
randomizer r = new randomizer();
r.randomizer();
}
after that modify the method because in ther you have
public void randomizer() throws InterruptedException
{
randomizer r = new randomizer(); //this will recursive create objects until overflows....
int[] numbers = {3,7,2,62,1,53,16,563,12,13,75};
Calendar rightN
You ve created a randomizer object but didnt actually do anything else. The method you implement (randomizer()) is not constructor so you need to invoke it. So inside your main method you need to do those 2 things right now:
randomizer r = new randomizer();
r.randomizer();
in order for your logic inside class to start.
PS1 read the naming conventions in java
PS2 it looks like you want to implement a constructor. if your method 'public void randomizer() ' was supposed to be one then remove the keyword (returning type) 'void' and when you use the 'randomizer r = new randomizer();' inside main, then your logic will execute..

Vector Method Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
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.
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.
Improve this question
I am trying to create a vector method called readfromfile which would potentially read the input from a different text file. Why does it give an error?
Edit: Thanks for the help, I have edited the code and it works!
Looks like I was confusing parameters and methods! :P
Thanks guys :D
package cas.lab1.firsteclipsePackage;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Vector;
public class FirstEclipseClass {
public static void main(String[] args) {
Vector input = new Vector();
input.add("A");
input.add("B");
input.add("C");
input.add("D");
printVectorElements(input, 3);
Vector<String> results = readFromFile();
}
public static void printVectorElements(Vector input, int count) {
for (int i = 0; i < count; i++) {
System.out.println(input.get(i));
}
}
public static Vector<String> readFromFile(){ //yeah I did confuse methods and parameters
Vector<String> result = new Vector<String>();
try{
File f = new File("input.txt");
Scanner s = new Scanner(f);
while(s.hasNextLine()) {
int i = s.nextInt();
if(i % 2 == 0)
result.add("Even");
else
result.add("Odd");
System.out.println(i);
}
s.close();
}
catch(FileNotFoundException e){
e.printStackTrace();
}
return result;
}
}
I guess that you are confused here. From your method call, I see that you don't need to pass any parameters, and instead want a Vector back. So I suggest you to change this line:
public static readFromFile(Vector<String> results){
To this line:
public static Vector<String> readFromFile(){
First thing: you didn't specified the return type. You should have :
public static Vector<String> readFromFile()
if you do not need any parameters in the function.
Second, for future, you cannot have this same name in the function and as a function parameter

Categories