Java ArrayList printing out empty? -



Java ArrayList printing out empty? -

my problem simple. when sending value of x when input == 1, method supposed add together value of x al arraylist. this, when effort print values of al , prints out empty.

first class:

import java.util.scanner; public class todo { scanner s = new scanner(system.in); scanner g = new scanner(system.in); void start(){ incompletetasks incompletetasks = new incompletetasks(); completetasks completetasks = new completetasks(); alltasks alltasks = new alltasks(); int input = 999; string x = ""; while (input != 0){ system.out.println("what do? type '0' cancel"); system.out.println(); system.out.println("1. add together task"); //done system.out.println("2. view current tasks"); system.out.println("3. delete task"); //done input = s.nextint(); if (input == 1){ while (!x.equals("quit")){ system.out.print("enter task: (type 'quit' finish! "); x = g.nextline(); if (x.equals("quit")){ start(); } else{ incompletetasks.incompletetasksadd(x); } } } else if (input == 2){ system.out.println("\t1. tasks"); system.out.println("\t2. finish tasks"); system.out.println("\t3. incomplete tasks"); input = s.nextint(); if (input == 1){ alltasks.alltasks(); } else if (input == 2){ completetasks.completetasks(); } else if (input == 3){ incompletetasks.incompletetasksdisplay(); } else if (input == 0){ system.exit(0); } else{ system.out.println("\t\t\t\tinvalid choice! seek again!"); start(); } } else if (input == 3){ system.out.println("hello"); x = s.nextline(); incompletetasks.incompletetasksdelete(x); } else if (input == 0){ system.exit(0); } else{ system.out.println("\t\t\t\tinvalid choice! seek again!"); start(); } } }

}

second class:

import java.util.arraylist; import java.util.scanner; public class incompletetasks { int counter = 0; scanner g = new scanner(system.in); arraylist<string> al = new arraylist<string>(); void incompletetasksdisplay(){ system.out.println("----------------------------------------------------------"); (string f: al){ counter++; system.out.println(f); // system.out.println(counter+". " +al.get(f)); } system.out.println("----------------------------------------------------------"); } void incompletetasksadd(string x){ al.add(x); system.out.println("\ttask added!"); } void incompletetasksdelete(string x){ (int k = 0;k<al.size();k++){ if (al.get(k) == x){ al.remove(x); } } }

result:

what do? type '0' cancel 1. add together task 2. view current tasks 3. delete task 1 come in task: (type 'quit' finish! test task added! come in task: (type 'quit' finish! test 1 task added! come in task: (type 'quit' finish! test 2 task added! come in task: (type 'quit' finish! quit do? type '0' cancel 1. add together task 2. view current tasks 3. delete task 2 1. tasks 2. finish tasks 3. incomplete tasks 3 ---------------------------------------------------------- ---------------------------------------------------------- do? type '0' cancel 1. add together task 2. view current tasks 3. delete task

i new java, don't surprised if have bad coding in other sections or if i'm missing obvious. appreciate help can get, i'm stumped, don't know i'm doing wrong.

this line:

incompletetasks incompletetasks = new incompletetasks();

declares local variable incompletetasks, specific current phone call start(), , initializes new empty list of tasks.

this line:

start();

creates new, separate phone call start(), hence have separate incompletetasks variable, , not have info incompletetasks variable you've been adding to.

there's no real reason start() should need phone call itself.

the quickest way prepare alter this:

while (input != 0){

to this:

mainloop: while (input != 0) {

(where mainloop label, giving while-loop name can refer it) , every occurrence of this:

start();

to this:

continue mainloop;

(meaning roughly, "go origin of loop named mainloop").

a better prepare split out method smaller parts. allow much clearer code. in general, it's best method not have nested loops (though there plenty of exceptions, don't take hard rule).

java arraylist

Comments

Popular posts from this blog

c# - ASP.NET MVC Sequence contains no matching element -

java - Parsing XML, skip certain tags -

rest - How to invalidate user session on inactivity in a stateless server? -