Reputation:
I want to make a program that will read from the keyboard a sequence of numbers and will create a dynamic list of even numbers and another dynamic list of odd numbers, and I have a problem, the even numbers are added to the even list and the odd ones to the odd list, but the display does not display the numbers I added.
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class lab13_1 {
static ArrayList<lab13_1> odd = new ArrayList<>();
static ArrayList<lab13_1> even = new ArrayList<>();
static int number;
public lab13_1(int number) {
this.number=number;
}
public String toString()
{
return "number"+number;
}
static void calc() {
try {
Scanner cin = new Scanner(System.in);
System.out.println("How many numbers do you want to add : ");
int nr = cin.nextInt();
for (int i = 0; i < nr; i++) {
System.out.println("<<< nr. " + (i + 1) + " >>>");
number = cin.nextInt();
if (number % 2 == 0) {
even.add(new lab13_1(number));
} else odd.add(new lab13_1(number));
}
} catch (Exception e) {
e.printStackTrace();
}
}
static void afish(){
System.out.println("Nr. odd");
for(lab13_1 i : odd) {
System.out.println(i.toString());
}
System.out.println("Nr. even");
for(lab13_1 i : even) {
System.out.println(i.toString());
}
}
public static void main(String[] args) throws Exception {
calc();
afish();
}
}
the data it displays :
How many numbers do you want to add :
5
<<< nr. 1 >>>
1
<<< nr. 2 >>>
2
<<< nr. 3 >>>
3
<<< nr. 4 >>>
4
<<< nr. 5 >>>
5
Nr. odd
number5
number5
number5
Nr. even
number5
number5
Who knows why it doesn't display numbers normally?
Upvotes: 0
Views: 746
Reputation: 2113
This is a cleaner answer to your desired goal
public class lab13_1 {
static ArrayList<Integer> odd = new ArrayList<>();
static ArrayList<Integer> even = new ArrayList<>();
static int number;
static void calc() {
try {
Scanner cin = new Scanner(System.in);
System.out.println("How many numbers do you want to add : ");
int nr = cin.nextInt();
for (int i = 0; i < nr; i++) {
System.out.println("<<< nr. " + (i + 1) + " >>>");
number = cin.nextInt();
if (number % 2 == 0) {
even.add(number);
} else odd.add(number);
}
} catch (Exception e) {
e.printStackTrace();
}
}
static void afish() {
System.out.println("Nr. odd");
for (Integer i : odd) {
System.out.println(i.toString());
}
System.out.println("Nr. even");
for (Integer i : even) {
System.out.println(i.toString());
}
}
public static void main(String[] args) throws Exception {
calc();
afish();
}
}
Upvotes: 0
Reputation: 163
You are using a static variable. Static means that it does not depend by the instance of the class.. Usually you use static variables for counting the instances of an object or the nr. of times a function is called. For e.g.
class TestStatic
{
public static int count = 0;
int mNumber;
void doSomething(int number)
{
mNumber=number;
count++;
}
int getNumber()
{
return mNumber;
}
}
Using it
public static void main()
{
List<TestStatic> tsList = new ArrayList<TestStatic>();
for(int i=0; i<5; i++)
{
TestStatic ts = new TestStatic();
ts.doSomething(i);
tsList.add(ts);
}
for(TestList ts : tsList)
{
System.out.println("Count = " + ts.count + "mynumber = " + ts.getNumber() );
}
}
In the output you will see count always at 5, number 0,1,2,3,4 This should let you explain how to use static variables.
Note that you should access static variables with
TestStatic.count
not with the instance.
P.S. code should run but i've not tested it
Upvotes: 1
Reputation: 1462
It's because your variable number
is static. Remove static
keyword and it should be fine.
int number;
Upvotes: 2