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();
}
}
}
Related
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
}
}
I wanted to create a simple program for user to insert 3 strings to a private string array in a class and then print it back by creating a new object using object reference but I think I am facing problem in the setter/getter.(Pretty new to class and setter/getter) Here is what I have so far:
import java.util.Scanner;
public class Stringtest {
public static void main(String[] args)
{ Scanner input=new Scanner(System.in);
Stringer Strung=new Stringer();
System.out.println("Strings:"+Strung.print());
}
}
class Stringer
{ Scanner input=new Scanner(System.in);
private String[] aa=new String[3];
aa[0]="zero";
aa[1]="one";
aa[2]="two";
Stringer()
{}
{ System.out.println("Please enter 3 strings:");
for(int i=0;i<4;i++)
{
aa[i]=input.next();
}
}
public void setaa(String[] a)
{
aa=a;
}
public String[] getaa()
{
return aa;
}
public void print(String[] a)
{
for(int b=0;b<4;b++)
{
System.out.printf("%s",a[b]);
}
}
}
Due to populating the array while creating a class instance, you don't require any setters. The only getter requires.
Divide the logic from the runner.
Always use array.length() while looping or use a simple for loop otherwise you'll be getting an indexOfBoudException error.
Didn't get why you are using printf() while printing results.
My solution:
import java.util.Scanner;
public class App {
public static void main(String[] args) {
App.run();
}
private static void run() {
Stringer stringer = new Stringer();
stringer.print(stringer.getStrings());
}
}
class Stringer {
private String[] strings = new String[3];
Stringer() {
System.out.println("Please enter 3 strings:");
for (int i = 0; i < 4; i++) {
Scanner scanner = new Scanner(System.in);
strings[i] = scanner.next();
}
}
String[] getStrings() {
return strings;
}
void print(String[] strings) {
System.out.println("Strings are:");
for (String string : strings) {
System.out.println(string);
}
}
}
Hey i want to write out table in second method.
In first i changed int x into table(each digit in other array index) and in second method i want to write out the table. How to do it ?
int tab [] =new int [4];
public int[] change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());
return tab;
}
public void writeout(int a[]) {
this.tab=a;//how to connect tab from change() with int a[]
for( int i=0;i<=3;i++) {
System.out.println(a[i]);
}
You can use newest Java 8 API to print your array. Your code may look like this:
import java.util.Arrays;
public class App {
int tab[] = new int[4];
public int[] change(int x) {
tab[0] = x/1000;
tab[1] = (x/100)%10;
tab[2] = (x/10)%10;
tab[3] = x%10;
return tab;
}
public void writeout(int array[]) {
Arrays.stream(array).forEach(System.out::println);
}
public static void main(String[] args) {
App app = new App();
app.writeout(app.change(2));
}
}
I fiddled with it. It appears to work after adding a closing brace at the end. tab.toString() doesn't result in sensible results though.
public class MyClass {
public static void main(String args[]) {
MyClass c = new MyClass();
c.writeout(c.change(3));
}
public int[] change(int x) {
int tab [] =new int [4];
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
System.out.println(tab.toString());
return tab;
}
public void writeout(int a[]) {
for( int i=0;i<=3;++i) {
System.out.println(a[i]);
}
}
}
If you want to use class fields, then you could do it like this:
public class MyClass {
public static void main(String args[]) {
MyClass c = new MyClass();
c.change(3);
c.writeout();
}
private int tab [] = new int [4];
public void change(int x) {
System.out.print(tab[0]=x/1000);
System.out.print(tab[1]=(x/100)%10);
System.out.print(tab[2]=(x/10)%10);
System.out.println(tab[3]=x%10);
}
public void writeout() {
for( int i=0;i<=3;i++) {
System.out.println(tab[i]);
}
}
}
How would I write the toString method? And how could I change the way users can fill numbers?
Here's the code:
public class NArray
{
private int[] intArray;
private NArray[] array;
public NArray(int n, int size, int fillNum)
{
if(n==1)
{
intArray = new int[size];
for(int i=0;i<size;i++)
{
intArray[i]=fillNum;
}
}
else
{
array=new NArray[size];
for(int i=0;i<size;i++)
{
array[i] = new NArray(n-1,size, fillNum);
}
}
}
}
For the fill method, you could use Arrays.fill (see the java API javadocs).
What do you want the toString() method to do? Can't really answer that part without a spec.
To use toString. An Example is shown Below. Please refer and implement..:) Happy Coding
public class Test{
public static void main(String args[]){
Integer x = 5;
System.out.println(x.toString());
System.out.println(Integer.toString(12));
}
}
For a tricky use..:)
class Bank
{
String n;
String add;
int an;
int bal;
int dep;
public Bank(String n,String add,int an,int bal)
{
this.add=add;this.bal=bal;
this.an=an;this.n=n;
}
public String toString()
{
return "Name of the customer.:" + this.n+",, "+"Address of the customer.:"
+this.add +",, "+"A/c no..:"
+this.an+",, " +"Balance in A/c..:"+this.bal;
}
}
public class Demo2
{
public static void main(String[] args)
{
List<Bank> l=new LinkedList<Bank>();
Bank b1=new Bank("naseem1","Darbhanga,bihar",123,1000);
Bank b2=new Bank("naseem2","patna,bihar",124,1500);
Bank b3=new Bank("naseem3","madhubani,bihar",125,1600);
Bank b4=new Bank("naseem4","samastipur,bihar",126,1700);
Bank b5=new Bank("naseem5","muzafferpur,bihar",127,1800);
l.add(b1);
l.add(b2);
l.add(b3);
l.add(b4);
l.add(b5);
Iterator<Bank> i=l.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
I have been trying to verify if the ThreadLocal members are indeed different in different threads.
This is my TestClass whose object I am sharing among multiple threads.
public class TestClass {
private static Set<Integer> setI;
private static ThreadLocal<Set<String>> setS;
public TestClass() {
Set<String> temp = new HashSet<String>();
for (int i=0; i<=4; i++) {
setI.add(i);
temp.add(Integer.toString(i));
}
setS.set(temp);
}
static {
setI = new HashSet<Integer>();
setS = new ThreadLocal<Set<String>>() {
protected Set<String> initialValue() {
return new HashSet<String>();
}
};
}
public static void addToIntegerSet(int i) {
synchronized(setI) {
setI.add(i);
}
}
public static void addToStringSet(String str) {
Set<String> sets = setS.get();
sets.add(str);
setS.set(sets);
}
}
the following is the class I use to test this out :-
package personal;
import java.util.*;
import personal.TestClass;
import java.lang.reflect.Field;
public class Test2 {
private static TestClass testObj;
private static Set<Set<String>> testStringSet;
private static Set<Set<Integer>> testIntegerSet;
static {
testObj = new TestClass();
testStringSet = new HashSet<Set<String>>();
testIntegerSet = new HashSet<Set<Integer>>();
}
private static void addToStringSet(Set<String> sets) {
synchronized(testStringSet) {
testStringSet.add(sets);
}
}
private static void addToIntegerSet(Set<Integer> sets) {
synchronized(testIntegerSet) {
testIntegerSet.add(sets);
}
}
private static int getTestIntegerSetSize() {
synchronized(testIntegerSet) {
return testIntegerSet.size();
}
}
private static int getTestStringSetSize() {
synchronized(testStringSet) {
return testStringSet.size();
}
}
private static class MyRunnable implements Runnable {
private TestClass tc;
private String name;
public MyRunnable(TestClass tc, int i) {
this.name = "Thread:- " + Integer.toString(i);
this.tc = tc;
}
#Override
public void run() {
try {
Field f1 = tc.getClass().getDeclaredField("setS");
Field f2 = tc.getClass().getDeclaredField("setI");
f1.setAccessible(true);
f2.setAccessible(true);
Set<String> v1 = (Set<String>)(((ThreadLocal<Set<String>>)(f1.get(tc))).get());
Set<Integer> v2 = (Set<Integer>) f2.get(tc);
addToIntegerSet(v2);
addToStringSet(v1);
} catch (Exception exp) {
System.out.println(exp);
}
}
}
public static void main(String[] args) {
for (int i=1; i<=2; i++) {
(new Thread (new MyRunnable(testObj,i))).start();
}
try {
Thread.sleep(5);
} catch (Exception exp) {
System.out.println(exp);
}
System.out.println(getTestStringSetSize());
System.out.println(getTestIntegerSetSize());
}
}
thus the 1st print statement should print out 2 and the second one should print out 1.
how ever the 1st print statement also prints out 1.
what is wrong ?
For a test class, I'd start with something much, much simpler. Just store a String or something in the ThreadLocal to start with, and avoid the reflection calls (setAccessible, etc.). Your issue is most likely in all of this extra code, and nothing due to the ThreadLocal itself.