Java - Going Through Value In Multiple ArrayList (Nested For Loop)
Jan 24, 2014
I just started playing around with Java for awhile, but got caught up in a problem when using ArrayList.
CinemaAppMain
public class CinemaAppMain {
public static void main(String[] args) {
new CinemaApp().start();
[Code]....
I am trying to get the movie name and theatre title from their ArrayList, and I know I need to go through the movie list & theatre list to find their name & title, and compare it with the user input and print out the result.
I use for(int i=0;i<movies.size();i++) to go through the the movie list, and I tried using for(int i=0;i<theatres.size();i++) to go through the theatre list. But when I printing multiple time with different user input value, it will show me strange result (screening have same movie name & theatre name, the else if statement is printed below the user input, user input is printed more than once after the first time).
Edit: I know its wrong for me to use nested for loop,if not, another solution is needed to get the item inside both of the list. (getting the "title" from movie list & "name" from theatre list)
Four experiments are performed, each consisting of six tests. The number of test results for each experiment is entered by the user. Write a Java program using a nested loop to compute and display the average of the test results for each experiment.
You can run the program by entering the following test results:
There is some kind of issue here.in the gui it will ask you to give points in time (ms) it wants to click for you, although when for example i put in 3 at "How Much Clicks" it asks 4 times and then exits with an error.Here is the error:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 at relax.Relax.StartSetup(Relax.java:175) at relax.Relax$2.actionPerformed(Relax.java:78) at javax.swing.AbstractButton.fireActionPerformed(Unk nown Source) at javax.swing.AbstractButton$Handler.actionPerformed (Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source)
I am trying to execute the following method: This method takes a delivery date as one of the arguments and a list of calendar holidays as another argument. If the delivery date is present in the holiday list, I will add 1 day more to the delivery date and check again if the new delivery date is part of holiday list.
Issue is that the last 3 statements before 'return' are getting executed multiple times though there is no for or while loop. Why they are getting invoked multiple times.
@SuppressWarnings("rawtypes") private String fetchNextWorkingDay(String sDeliveryDate, Element eleCalendarDayExceptions, SimpleDateFormat sdf, Format formatter) throws Exception { System.out.println(""); System.out.println("Inside fetchNextWorkingDay method"); System.out.println("Del Date to compare is "+sDeliveryDate); Boolean isDateSet = true;
just trying to learn it on my spare time and I'm doing do-while loops
public class help { public static void main (String args[]) throws java.io.IOException { char choice, ignore; do{ System.out.println ("Choose one:"); System.out.println("1. if"); System.out.println("2.switch");
[code]....
It makes no difference in the program wither i delete this block or keep it..how while (choice < '1'| choice >'2'); translates? I would assume it would be while (choice >= '1'| choice =<'2');?
Question: you are only allowed to use numbers from 1-6. Write a program to find all the permutations when three numbers are multiplied together to give a result 8. one number cannot occur twice in any permutation.
public class number4 { public static void main(String[] args) { for(int a=1; a<=4; a++) { for(int b=1; b<=4; b++) {
[Code]...
my program also prints out 2 2 2. but i'm not allowed to do that. how can I stop it from printing 2 2 2 ?
Here's what "Why doesn't this work?" question. It concerns a small method which is part of a card game.
I'm trying to check a condition from a section of an array, without a predetermined number of times any given call to this method will require a check. How much of the array I'm checking could vary greatly.
Is it at all possible to nest a for loop yielding a variable number of boolean && conditions inside an if? Am I just missing the right bracketing or is this nesting (if that's the word) even possible in the language?
To clarify, below is broken code. Compiler isn't letting me accomplish this goal as I envision it.
public boolean isFlanking() { boolean f; int reach = Math.abs(selectorX - targetX); if(rival.getDeck()[selectorX].getPile().isEmpty() == true &&
//Main method public static void main(String args[])throws IOException{ boolean runProgram = true; Scanner keyboard = new Scanner(System.in); //runs program while runProgram is true while (runProgram){
I was told that the answer when this code segment is printed look's like this: $$$$ $$$ $$ $
Here's what I did:Looking at the outer for loop, (i) 0 < 4 so I went into the first inner nested loop. (k) 0 is not less than (i) 0 so I went to the 2nd inner nested loop and found that it worked, and I was able to repeat this loop 3 more times and then I exited the loop and printed the line out (giving me the first line of four $). I then went back to the outer for loop, increased i by 1 and (i) 1 < 4 so I went to the 1st inner nested loop. I used the 0 for the k first and (k) 0 < 1 so I printed out a space (now here's where I get lost) I then incremented k by 1, so k = 1, but 1 is not less than 1 (i) and so I moved on to the next nested for loop. So when this line is printed, I'll only have one space when there should be 4.
I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) ....
As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.
By default, the program is meant to have a starting inventory, which is why I have added them in the constructor
public class Inventory { private List<Product> products; public Inventory() { addProduct(new Product("Frying pan", 15, 20)); addProduct(new PerishableProduct("Apple", 5.8, 30, 7)); addProduct(new ItemisedProduct("Cereal", 5.8, 0)); // this is where I am having problems. Wanting to add // objects to the ItemisedProduct's ArrayList for cereal. }
Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.
public class ItemisedProduct extends Product { private ArrayList<String> serialNumbers = new ArrayList(); public ItemisedProduct(String name, double price, int qty) { super(name, price, qty)
[Code] .....
My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
Implementation of the nested for loop. I thought it was going to go smoothly but apparently not.Purpose: This program is meant to print out all the prime numbers up to a certain range using the algorithm Sieve of Eratosthenes.
Update output: Works as expected.
Java Code:
import java.util.Scanner; import java.util.ArrayList; public class PrimeFactors { static ArrayList<Double> primeList = new ArrayList<>(); static double range = 0; static double maxPrime = 0; static double primeTester = 0;
I am working on my java program it prints out a table of angles and the sin, cos, and tan. For extra credit he wanted us to use nest loops to create a space after every five lines. I have come real close to getting this to work but have a problem with the very end of the table. The table needs to stop at 180...
public static void printTable(double angle,double sin,double cos,double tan) { // Method to create a table for the values for (double j = 0; j <= 180;) { for (double i = 1; i <= 5; i++) {//loop to print five lines of code angle = Math.toRadians(j);
I've made my loop, but I am unable to get more then a 1 1 1 checkerboard properly. I am stuck on how to divide the filler characters to make the proper square size. As of now they are all one lined.
import java.util.*; public class Checker{ public static void main(String[] args) { int col, row, size; char filler; System.out.println("Please enter 3 numbers and a character."); //output
Now I have to write a method called printDesign that produces the following output but i am not even entirely sure how to start it out now of course i know how to make a for-loop but and i guess if i was to do something for this i would do this .....
I have to make a retail item class that has description, price and quantity. I then have to make a CashRegister class that calculates subtotal, tax, total and displays it (toString). I also have to make a driver class to call the methods from main. I have no clue what I am doing and what I have done does not work. I am trying to make it sort of like a cart, in the CashRegister it should ask the user to enter the description, quantity purchased and the price. Then it would send that to the arraylist of retailitem. I am having trouble using those items in my other methods and then do not know how to call those in the driver class.. So here are my classes:
public class RetailItem { private String description; private int quantity; private double price; public RetailItem(){ description = ""; quantity = 0;
Alright.. So I have to make a retail item class that has description, price and quantity. I then have to make a CashRegister class that calculates subtotal, tax, total and displays it (toString). I also have to make a driver class to call the methods from main. I have no clue what I am doing and what I have done does not work. I am trying to make it sort of like a cart, in the CashRegister it should ask the user to enter the description, quantity purchased and the price. Then it would send that to the arraylist of retailitem. I am having trouble using those items in my other methods and then do not know how to call those in the driver class...
I have an ArrayList of tens of thousands of elements, and want to remove the items at a given set of sorted indices like {1,5,29,318,499,583}. It seems like this operation could be performed in linear time and space by first finding a cumulative sum of the shifts needed to move everything to their right positions and then shifting every item. On the other hand, removing and shifting one by one would seem to require many more shifts. Is there a way to do this in Java's ArrayList?