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;
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:
I'm making a program that selects classes from a database via a jcombo box and then populates a time table with the class times.
I've used a custom cellrenderer that extends DefaultTableCellRenderer to text wrap the information and change the background of the cells.
It works but when i minimize the program it goes all black and if I double click I get a new JTextArea opening up on the timetable and when I minimize the program and go to open it again i get an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException" and the cell render seems to be called again even though i haven't rerun the program.
This is the cell renderer that I built
public class TimesTableCellRenderer extends DefaultTableCellRenderer { private int minHeight = -1; private final JLabel label = new JLabel(); private final JTextArea textArea = new JTextArea(); public TimesTableCellRenderer(){ super(); textArea.setLineWrap(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 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)
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 am using what is known as a Depth First Search to solve a maze using my own Stack created class for an assignment. The reason I believe I am getting this error is because my program is never meeting the `finishSpot` condition. I have used the debugger and it looks like it is infinitely stuck at the Point before the `finishSpot`. My logic seems to be mostly correct except until the point where my maze solver tries to finish the maze.
This is a sample maze:
***** *s* * * * * * f* *****
Here is my Main which uses my created Stack class.
//Creates a Stack and determines the start and endpoints. public static void solveDFS( char [][] maze ){ LinkedStack stack = new LinkedStack(); Point currentSpot = findPoint( maze,'s' ); Point finishSpot = findPoint( maze, 'f' ); findPath( maze,currentSpot, finishSpot,stack );
protected void randomise() { int[] copy = new int[]; // used to indicate if elements have been used boolean[] used = new boolean[array().length]; Arrays.fill(used,false); for (int index = 0; index < array().length {
I am working on an Eclipse based project that uses WTP Eclipse plugin. Since I have switched security on in that plugin (by checking "Enable Security" check box on) I'm having multiple problems. I get a stack of security errors - attached in this post. I also show my java.policy file. Because the of the mass exceptions I list only the top, although I have attached a docx file with full thing.
Here is my java.policy file:
grant codeBase "file:${catalina.base}/wtpwebapps/App/WEB-INF/lib/hibernate-core-4.3.5.Final.jar" { permission java.util.PropertyPermission "*", "read, write"; }; grant codeBase "file:${catalina.home}/lib/catalina.jar" { permission java.lang.RuntimePermission "*";
[Code] ....
Exceptions:
15/08/2014 10:12:43 PM org.apache.catalina.core.AprLifecycleListener init INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:Javajdk1.6.0_33in;C:WINDOWSSunJavain; C:WINDOWSsystem32;C:WINDOWS;
Write a java program that uses a While loop and persistent output to count down from 33 to 0 by threes, and put each value on a separate line, also do this in for loop.
Alright so I'm trying to write a code that implements a for loop and if statements that displays any number from 100-200 if the number is divisible by either 5 or 6 in rows of ten numbers each row. If it is not divisible by that number then it should go back to the beginning of the loop until it reaches 200. My main problem is that it doesn't display anything. I don't get any errors or anything but every time I run the program it just displays nothing. Sample output is at the bottom of the code.
public class Exercise5_11 { public static void main(String[] args) { int count = 0; int i = 100; //for (the numbers from 100 to 200) for (i = 100; i>100 && i<200; i++){
having some trouble doing this. Here are the requirements:
1)Write a program using loop and break to calculate the value of PI. 2)You can use the following equation to calculate PI. PI=4/1-4/3+4/5-4/7+4/9-4/11+4/13 etc.. The program shouldn't loop until the criteria is satisfied. 3)The criteria is the value of PI you get is very close to the “real” value (3.1415926), meaning that their difference should be smaller than a very small numeric number, e.g., 0.0001). 4)The control structures you may need for this program are loop, if-else, and unlabeled break. 5)The output should have the format: for each iteration, it has iteration number and the corresponding value of PI until that iteration. The last line would be the final value of PI.
Here is my code so far:
package ids; public class IDS { public static void main(String[] args) { double pi = 0; for(int i=1; i<=100000; i++){ if ((i%2)==0){ pi+=(4/(2*i-1));