I am creating an ArrayList object that contains a class. However, when I call a method from the WordList() class, it says "error: cannot find symbol obj.numWordsOfLength(2)"
import java.util.ArrayList;
public class WordList{
public static void main(String []args){
ArrayList obj = new ArrayList<WordList>();
obj.add("bird");
obj.add("wizard");
obj.add("to");
obj.numWordsOfLength(2);
}
private ArrayList<String> myList;
public WordList(){
myList = new ArrayList<String>();
}
public int numWordsOfLength(int len){
//Code
}
public void removeWordsOfLength(int len){
//Code
}
}
When you call obj.numWordsOfLength(2); you're calling the method numWordsOfLength from ArrayList (which does not exist), not from your WordList class.
First of all, you are adding String to ArrayList, not you're WordList object.
I think what you're trying to achieve will look more like this:
public class WordList {
private List<String> wordList = new ArrayList<>();
public static void main(String[] args) {
WordList wordList = new WordList();
wordList.add("bird");
wordList.add("wizard");
wordList.add("to");
wordList.numWordsOfLength(2);
}
public void add(String word) {
wordList.add(word);
}
public int numWordsOfLength(int len) {
//Code
}
public void removeWordsOfLength(int len) {
//Code
}
}
Related
I have an assignment says printing a object list with given main class:
public static void main(String[] args) throws IOException {
ArrayList<LoaiPhong> ds = new ArrayList<>();
Scanner in = new Scanner(new File("xx.in"));
int n = Integer.parseInt(in.nextLine());
while(n-->0){
ds.add(new LoaiPhong(in.nextLine()));
}
Collections.sort(ds); //this only include object list
for(LoaiPhong tmp : ds){
System.out.println(tmp);
}
}
static class LoaiPhong {
String line;
public LoaiPhong(String line) {
this.line = line;
}
}
So that's mean i have to do something outside the main class to sort the list. Can anyone suggest what to do to sort the object list by its property?
Edit: Thanks to Gus i have the answer, the new class look something like this
static class LoaiPhong implements Comparable<LoaiPhong> {
...
#Override
public int compareTo(LoaiPhong_.LoaiPhong o) {
return <Some logic here>;
}
}
It is necessary for a HashSet to be defined in one class, and for me to access this HashSet from other classes and add data to it. However, it doesn't go very easily, because every time it's as if the HashSet is reset and when I add a new element, the old one is gone.
Output:
[]
[[sdadsa, fafafsa, ghfhgf, kjhkjh]]
[[], [sdadsa, fafafsa, ghfhgf, kjhkjh]]
First class:
public class listaKlasa {
protected static Set<ArrayList<String>> unikatna = new HashSet<ArrayList<String>>();
public static void setUnikatna(ArrayList<String> ista) {
unikatna.add(ista);
}
public static Set<ArrayList<String>> getUnikatna() {
return unikatna;
}
}
Secund class:
public class novitest {
public static void main(String[] args) {
System.out.println(listaKlasa.getUnikatna());
ArrayList<String> li = new ArrayList<>();
li.add("sdadsa");
li.add("fafafsa");
ArrayList<String> la = new ArrayList<>();
li.add("ghfhgf");
li.add("kjhkjh");
listaKlasa.setUnikatna(li);
System.out.println(listaKlasa.getUnikatna());
listaKlasa.setUnikatna(la);
System.out.println(listaKlasa.getUnikatna());
}
}
The mistake is adding elements, instead of adding 2: 2, you added everything to one list.
First class:
public class listaKlasa {
protected static Set<ArrayList<String>> unikatna = new HashSet<ArrayList<String>>();
public static void setUnikatna(ArrayList<String> ista) {
unikatna.add(ista);
}
public static Set<ArrayList<String>> getUnikatna() {
return unikatna;
}
}
Secund class:
public class novitest {
public static void main(String[] args) {
System.out.println(listaKlasa.getUnikatna());
ArrayList<String> li = new ArrayList<>();
li.add("sdadsa");
li.add("fafafsa");
ArrayList<String> la = new ArrayList<>();
la.add("ghfhgf");
la.add("kjhkjh");
listaKlasa.setUnikatna(li);
System.out.println(listaKlasa.getUnikatna());
listaKlasa.setUnikatna(la);
System.out.println(listaKlasa.getUnikatna());
}
}
Try this:
System.out.println(listaKlasa.getUnikatna());
ArrayList<String> li = new ArrayList<>();
li.add("sdadsa");
li.add("fafafsa");
ArrayList<String> la = new ArrayList<>();
la.add("ghfhgf");
la.add("kjhkjh");
I am trying to write a method to capitalize words ending with an "s" in an arrayList
import java.util.*;
import java.io.*;
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("speech.txt"));
ArrayList<String> myList = new ArrayList<String>();
public void input() {
while (input.hasNext()) {
myList.add(input.next());
}
}
public void capPlurals() {
for (int i = 0; i < myList.size(); i++) {
String element = myList.get(i);
if (element.endsWith("s")) {
System.out.println(element.toUpperCase());
}
}
}
}
Test.java:12: error: illegal start of expression
public void input() {
^
Just remove those useless method declarations inside your main() method - as mentioned in the comments you cannot define methods inside other methods.
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("speech.txt"));
ArrayList<String> myList = new ArrayList<String>();
while (input.hasNext()) {
myList.add(input.next());
for (int i = 0; i < myList.size(); i++) {
String element = myList.get(i);
if (element.endsWith("s")) {
System.out.println(element.toUpperCase());
}
}
}
}
}
You can't declare a method inside another method. Declare them outside the main and pass proper arguments.
public class Test {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new File("speech.txt"));
ArrayList<String> myList = new ArrayList<>();
addToList(input, myList);
capPlurals(myList);
}
public static void addToList(Scanner input, ArrayList<String> myList) {
while (input.hasNext()) {
myList.add(input.next());
}
}
public static void capPlurals(ArrayList<String> myList) {
for (int i = 0; i < myList.size(); i++) {
String element = myList.get(i);
if (element.endsWith("s")) {
System.out.println(element.toUpperCase());
}
}
}
}
Basically, I want to make sure that I'm doing this right. I'm calling these methods in another class. The methods specify to return a variable, so when instantiating them, should I be placing null in the parenthesis? I just want to make sure I'm doing this right.
public class IntSLList {
protected static IntSLLNode head, tail;
public void createInst(){
new IntSLList().isEmpty();
new IntSLList().addToHead(null);
new IntSLList().addToTail(null,null);
new IntSLList().deleteFromTail();
new IntSLList().printAll();
new IntSLList().isInList(null);
new IntSLList().delete(null);
}
public IntSLList() {
//code
}
public static boolean isEmpty() {
//code }
public static void addToHead(String AN) {
//code
}
public static void addToTail(String AN, Double AB) {
//code
}
public static String deleteFromHead() { // delete the head and return its info;
//code
}
public static String deleteFromTail() { // delete the tail and return its info;
//code
}
public static void printAll() {
//code
}
public static boolean isInList(String AN) {
//code
}
public static void delete(String AN){
//code
}}
Thanks everyone in advance!
You don't need to instantiate methods at all, they are already "prepared for use" when your list gets instantiated.
What you want to do is remove the static keyword from the methods: It means that the methods are defined "global" for the whole class instead of just for one specific list.
Update: This is the list class as I imagine it should be
import java.util.ArrayList;
public class IntSLList {
protected int head,tail;
//I'll use this as examplelist, so I can omit the list implementation
private ArrayList<Integer> dataList;
public IntSLList(){
//Do initalization
dataList = new ArrayList<>();
}
public void addToHead(int node){
dataList.add(0, node);
}
public void addToTail(int node){
dataList.add(node);
}
public boolean isEmpty(){
return dataList.isEmpty();
}
// ... snip other list methods here ...
}
You can use/access it like this:
public class ListMain {
public static void main(String[] args){
IntSLList myList = new IntSLList();
myList.addToHead(1);
myList.addToTail(2);
System.out.println("myList.isEmpty() = " + myList.isEmpty());
}
}
This is my Data class:
public class Data {
private String name;
private int age;
Data(String n,int a){
name = n;
age = a;
}
public String getName(){
return(name);
}
public void setName(String n){
name = n;
}
public int getAge(){
return(age);
}
public void setAge(int a){
age = a;
}
public void Print(){
System.out.print(("("+GetName()));
System.out.print(",");
System.out.print(GetAge());
System.out.print(") ");
}
}
This is my CS1702_Lab5 class that uses ArrayList and that is supposed to print the content of ArrayList:
import java.util.ArrayList;
public class CS1702_Lab5 {
public static void main(String args[]){
ArrayList<Data> data = new ArrayList<Data>();
data.add(new Data("Fred", 21));
}
private static void PrintDataArray(ArrayList<Data> data) {
for(int i=0;i<data.size();++i){
data.get(i).Print();
}
}
}
I am trying to add new Data which is a StringName and IntAge and then display it but it doesn't seem to work. The console is empty nothing is printed.
Thank you in advance!
You never called your print method from main().
You have to call PrintDataArray() method from main():
import java.util.ArrayList;
public class CS1702_Lab5 {
public static void main(String args[]) {
ArrayList<Data> data = new ArrayList<Data>();
data.add(new Data("Fred", 21));
PrintDataArray(data); // YOU NEED TO ADD THIS LINE.
}
private static void PrintDataArray(ArrayList<Data> data) {
for (int i = 0; i < data.size(); ++i) {
data.get(i).Print();
}
}
}
public static void main(String args[])
{
ArrayList<Data> data = new ArrayList<Data>();
data.add(new Data("Fred", 21));
PrintDataArray(data);
}
you never called PrintDataArray
Call the Print PrintDataArray method in the main method
public static void main(String args[]) {
{
ArrayList<Data> data = new ArrayList<Data>();
data.add(new Data("Fred", 21));
here>PrintDataArray(data);
}
}
You haven't call the method PrintDataArray(data) in your main method...
public static void main(String args[])
{
ArrayList<Data> data = new ArrayList<Data>();
data.add(new Data("Fred", 21));
CS1702_Lab5.PrintDataArray(data);
}
Note: please read naming conventions in Java and try to use them.
Add PrintDataArray() inside your main method
public static void main(String args[])
{
{
ArrayList<Data> data = new ArrayList<Data>();
data.add(new Data("Fred", 21));
PrintDataArray(data);
}
}
private static void PrintDataArray(ArrayList<Data> data)
{
for(int i=0;i<data.size();++i)
{
data.get(i).Print();
}
}
}