Results 1 to 13 of 13
- 05-20-2010, 03:45 AM #1
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
One big arraylist and static mess
ok, so i have an assignment that starts with an empty arraylist and using user input, call various methods to do things to the arraylist. but im having trouble with static methods and static variables or just the general layout. heres my code so far:
it says that non-static variable quit cannot be referenced from a static context, and that the non static method selection() cannot be referenced from a static context, and i dont understand how to fix itJava Code:import java.util.ArrayList; import java.util.Scanner; import java.io.*; import java.lang.String; public class Part2 { private ArrayList<Collection> collections = new ArrayList<Collection>(); private boolean quit = false public static void main(String[] args) { while(quit == false) mainMenu(); selection(); } public static void mainMenu() { System.out.println("Please make a selection, by entering the letter which corresponds to the desired action:"); System.out.println(""); System.out.println("[P] Print out details of list"); System.out.println("[E] Is the list empty?"); System.out.println("[N] Number of elements in list"); System.out.println("[C] Create empty list"); System.out.println("[I] Insert element into list"); System.out.println("[D] Remove element from list with certain variable"); System.out.println("[R] Remove element from list with a certain index"); System.out.println("[F] Read data from file"); System.out.println("[W] Write current list to file"); System.out.println("[A] Print out average collection size"); System.out.println("[M] Print out maximum collection size"); System.out.println("[V] Print elements in list with a collection of a certain size"); System.out.println("[U] Find whether a certain name owns a collection"); System.out.println("[H] Change collection size of a certain person"); System.out.println("[Q] Quit program "); } public boolean selection() { Scanner in = new Scanner(System.in); String selection = in.next(); selection = selection.toUpperCase(); if(selection.length() > 1) { System.out.println("That is not a valid input. Please make sure you enter only one character. Try again:"); selection(); } if(selection.equals("I")) insertElement(); else if(selection.equals("P")) printListDetails(); else if(selection.equals("E")) listEmpty(); else if(selection.equals("N")) numberOfCollections(); else if(selection.equals("C")) createEmptyList(); else if(selection.equals("D")) removeElementCertainVariable(); else if(selection.equals("R")) removeElementCertainIndex(); else if(selection.equals("F")) readFile(); else if(selection.equals("W")) WriteCurrentList(); else if(selection.equals("A")) System.out.println(calculateAverage()); else if(selection.equals("M")) System.out.println(findGreatestCollection()); else if(selection.equals("V")) findElementsCertainSize(); else if(selection.equals("U")) findElementCertainName(); else if(selection.equals("H")) changeCollectionSize(); else if(selection.equals("Q")) quit = true; else System.out.println("That is not a valid input. Please make sure you enter a character that corresponds to an action. Try again:"); quit = false; } public void insertElement() { System.out.println("Who is the collection owned by?"); Scanner in = new Scanner(System.in); String name = in.next(); for(int i=0; i<collections.size(); i++) { Collection a = collections.get(i); if(name.equals(a.getOwner())) { System.out.println("There is already an account owned by that name. Try a different name next time"); } } System.out.println("How many CD's are in the collection?"); Scanner in2 = new Scanner(System.in); int quantity = in2.nextInt(); Collection c1 = new Collection(name); c1.addCD(quantity); System.out.println(c1.getOwner()); }
if i can get all that working, i should be able to do all the methods that manipulate the arraylist without too much hassle
EDIT: also, any hints on better layout/design are more than welcome
- 05-20-2010, 03:49 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
So in my mind, I think that you have to ask yourself, is this project going to be a completely static project, or is it going to be an OOP-based project, and you'll have to make this decision first before trying to make any changes to this program.
- 05-20-2010, 04:18 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-20-2010, 08:09 AM #4
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
so what is OOP-based? and how is it different from the static one?
- 05-20-2010, 09:13 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
OOP - object oriented programming. It relies on objects doing certain tasks to solve the problem at hand. A static aproach instead has a few functions that take parameters and return the results. Both aproaches solve a problem, it isn't really a question of what's right and what's wrong. It more of an organisational decision you have to make. I myself swear by OOP, since the programs I write now are actually legible and easy to maintain.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 05-20-2010, 10:26 AM #6
Member
- Join Date
- May 2010
- Posts
- 6
- Rep Power
- 0
ahk, so how would i change my code to create an OOP project?
- 05-20-2010, 03:57 PM #7
Remove all static methods except for main(). Have main() create an object or objects that has the methods you need. Call the methods as needed using object references:
object.method()
- 05-20-2010, 04:51 PM #8
- 05-20-2010, 05:05 PM #9
You could use a switch statement instead of that giant else if group also. It doesn't change much but makes it a little easier to read.
Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-20-2010, 05:13 PM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-20-2010, 05:16 PM #11
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 05-20-2010, 06:57 PM #12
One problem with converting to switch is that the expression is a primitive like int. The users values are Strings. Looks like a one for one conversion from a single letter String to an int however.
- 05-21-2010, 02:23 PM #13
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
We can keep kind of a mapping in such cases. But have to size if first of all to check that way is feasible or not.
Similar Threads
-
non-static method getType cannot be referenced from a static contex
By Dekkon0 in forum New To JavaReplies: 4Last Post: 05-12-2010, 11:05 AM -
non-static variable grade cannot be referenced from a static context
By pictianpravin in forum New To JavaReplies: 3Last Post: 02-11-2010, 09:59 AM -
Help with my mess...using multiple delimiters and string splits...
By A5i19 in forum New To JavaReplies: 5Last Post: 11-04-2009, 02:25 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks