I have a code
class testArrayList
{
ArrayList<String> auto = new ArrayList<String>();
auto.add("MITSUBISHI");
auto.add("Hyundae");
auto.add("Ford");
auto.add("Ferrari");
auto.add("Mazda");
auto.add("Mustang");
auto.add("Lamborghini");
for(String cars : auto)
{
System.out.println(cars);
}
}
but when I compiled it, theres an error saying
Im confused why it has an error saying IDENTIFIER EXPECTED or ILLEGAL START OF TYPE tho I already imported import java.util.ArrayList;
You have to change your code as below. You need to add array list inside to the main method.
public class testArrayList {
public static void main(String[] args) {
ArrayList<String> auto = new ArrayList<String>();
auto.add("MITSUBISHI");
auto.add("Hyundae");
auto.add("Ford");
auto.add("Ferrari");
auto.add("Mazda");
auto.add("Mustang");
auto.add("Lamborghini");
for(String cars : auto)
{
System.out.println(cars);
}
}
}
You can put logic ONLY inside methods. The main-method is the method that will be executed when u run the program. You can only put declarations and methods inside a class, thats why you get the runtime errors. I won't do a duplicate, just take Anuradha's solution.
auto is not a keyword and it’s fine in java. Change theSyntax of arrayList it to something
List<String> auto= new ArrayList<>();
Related
I am trying to initialize objects in my ArrayList
The following is my code and what it does is that it receives an ArrayList, already defined in the main method. However, it is not executing the readInput method in the code below. readInput is a method declared in another class called customer that takes in keyboard input from the user. Its a complete class BTW. I believe that there is no object in the ArrayList at the moment as i have run some test to see whats the problem. Im new to using Arraylist. Therefoere i am unsure of how to solve this proble.
What custs.set(i, new customer()); does is actually to initialize the object in index.
public static void freeCustomer(ArrayList<customer> custs,
ArrayList<supplement>supps)
{
for(int i=0;i<custs.size();i++)
{
custs.set(i, new customer());
custs.get(i).readInput();
for(int s=0;s<supps.size();s++)
{
supps.set(s, new supplement());
supps.get(s).readInput();
}
}
}
If I understood right,you want to add user input from a customer method.I would use add method.I just made a fast example of add method.From here you can expand to loops,etc.
public static void freeCustomer(ArrayList<customer> custo,customer c)
{
custo.add(new customer(c.readInput()));
}
public static void main(String[] args) {
ArrayList<customer> custo = new ArrayList<customer>();
customer c=new customer("");
freeCustomer(custo,c);
}
}
I'm trying to make a very basic software simulation of a router that reads in a text file and acts on the commands and other information given to it. I made a new class called groupclass to hold an ArrayList
package router;
import java.util.ArrayList;
public class groupclass
{
public ArrayList<String> member;
}
Made a HashMap with it as the value
static Map<Integer, groupclass> groupmap = new HashMap<Integer, groupclass>();
And tried this code
private static void groupadd(int groupnum, String address)
{
out.println("debug groupadd");
try
{
groupmap.get(groupnum).member.add(address);
}
catch(NullPointerException e)
{
groupmap.put(groupnum, new groupclass());
groupmap.get(groupnum).member.add(address);
}
}
Which throws a NullPointerException at
groupmap.get(groupnum).member.add(address);
The idea was to make a map and associate a new groupclass object with each group number, and each groupclass would have a list of IPs stored as strings. I'm at a complete loss here, and any tweaks I do cause weirder problems and build errors I don't understand.
Thanks in advance!
Your member variable "member" is not initialized. Add
public member=new ArrayList<>();
i am on the creation of an app in android. its a calculator app. the main activity is where the user could input the equation, and the second activity is where the user can add/edit/delete variables. so i made a new class in another file named Global.java. then i extended it to application, imported everything i need, made s private string, made some public functions, edited the manifest, and initialized it right on my main activity. everything works fine while im only using a string to be passed by the functions but when i started adding what i need, an ArrayList, and made some functions so i could access the list then run it, the app closes. i think its because the arraylist is not allowed to be passed to different classes? am i right or am i just missing something?
please dont downvote my post if i didn't post something needed. i am using aide so there is no log output. code:
Global.java
...
import android.app.*;
import java.util.*;
public class Global extends Application
{
private String s;
public static ArrayList<String> sList;
public String getS() {
return s;
}
public void setS(String ss) {
s=ss;
}
public void add() {
sList.add(s);
}
}
MainActivity.java
...
String s;
...
global=(Global)getApplicationContext();
...
global.setS("jian"); //this one works
global.sList.add("jian"); // this one dont
...
Are you sure you initialized sList, like this:
sList = new ArrayList<String>();
If you didn't, you might want to change its declaration to include this initialization.
public static ArrayList<String> sList = new ArrayList<String>();
Just do
global.add("jian");
since you have an add function to take care of the addition of item to arraylist.
Also, try with this:
public void add(String ss) {
sList.add(ss);
}
You are not instantiating your arraylist.
public static ArrayList<String> sList = new Arraylist<String>();
Also you should read beginner tutorials on Java and android, using a public extension of application like this is a bad idea and you can get log outputs from different apps if Aide doesn't provide that, search play store
This may sound silly, but, I get this error while casting from List to Array:
ArrayStoreException: null
The code that i am using right now is :
public void onSuccess(List<Agent> resultList) {
Agent[] array = new Agent[resultList.size()];
resultList.toArray(array);
Agent its a class that i have defined with their own field definitions, being all of them private final Strings
But i dont know what i could be missing atm.
Thank you in advance for your help.
Kind regards,
You're probably passing your ArrayList<Agent> to some method which
has just an ArrayList or List parameter (untyped). This method
can pass compilation but mess things up at runtime. Example below.
If you comment out the call to messUp in my example things are OK.
But messUp can add things which are not Agents to your list,
and cause problems this way.
This is my best guess without seeing your code.
Hope it helps.
import java.util.ArrayList;
import java.util.List;
public class Test009 {
public static void main(String[] args) {
List<Agent> resultList = new ArrayList<Agent>();
resultList.add(null);
resultList.add(new Agent1());
resultList.add(new Agent());
messUp(resultList);
test(resultList);
}
private static void messUp(List lst){
lst.add("123");
}
public static void test(List<Agent> resultList){
Agent[] array = new Agent[resultList.size()];
resultList.toArray(array);
System.out.println("done");
}
}
class Agent {
protected int x;
}
class Agent1 extends Agent{
protected int y;
}
Additional Note: OK, well, this doesn't explain
the "null" part of your error message.
So the root cause is somewhat different.
I am very new to java and haven't learnt much at the moment but I have a issue.
I am trying to pass a variable that runs in a thread to another java class.
in the client thread I am running:
UserReg UR = new UserReg();
UR.addUser(userID);
and in the separate java class I have got:
import java.util.ArrayList;
public class UserReg {
ArrayList userList = new ArrayList();
void addUser (String UserID) {
userList.add(UserID);
System.out.println(userList);
}
}
the issue I have is that each time a new thread runs then it overwrites the array list and doesn't add to it.
That is because it has got do with this:
UserReg UR = new UserReg();
UR.addUser(userID);
Each time, it initializes the userReg and hence the ArrayList inside.
What you can probably do is that (temporarily but not a decent solution) would to make the userList static