3
votes
2answers
46 views

Java Cross with characters

I managed to make a perfectly even cross with for loops. I'm trying to make this cross easier to code. I have to keep it with ...
10
votes
6answers
1k views

Find the maximum number of consecutive zeros in an array

Question: To find the maximum number of consecutive zeros in a given array. Example: Input: \${1, 2, 0, 0, 2, 4, 0, 2, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 7, 0}\$ Output: "Maximum number ...
9
votes
3answers
291 views

Optimizing “Herd Sums” problem using dynamic programming

I'm trying to solve a practice problem and all is well except for the last 2 inputs my solution is too slow. Just one loop is slowing it down I think. Herd Sums Execution Time Limit: 2 ...
4
votes
1answer
41 views

Baseline tagger

This is a baseline tagger coed I have written. How can I optimize this code? ...
10
votes
2answers
301 views

Using a Pythonesque range() generator function with the Java foreach loop

Now that we have the nice new foreach loop in Java, the old-style loop looks ugly be comparison. I like the way Python has a ...
2
votes
2answers
126 views

A matrix with square diagonals and transpose being increments of other

Construct a matrix with the following property: North-west to South East diagonals are squares. Matrix[i][j] + 1 = Matrix[j][i] for each i less than j. Example of such m a matrix is ...
25
votes
5answers
3k views

Is it OK to use while ((line = r.readLine()) != null) construct?

I want to refactor the following code because I don't feel comfortable about using assignment inside comparison operator. It looks like pretty idiomatic C, but do you think this is a good practice in ...
8
votes
2answers
403 views

Object Creation during loops

I'm trying to parse a CSV file into objects. I've already got a very efficient CSV parser (SuperCSV). The problem arises when trying to create the object with the data that is being read out. I've ...
5
votes
1answer
119 views

Game Loop and FPS

Some time ago I made my simple game loop, so here's the code: ...
5
votes
1answer
69 views

Read line and combine duplicate entries based on one of the fields

The following Java code reads lines from an input text file and will output the entry that has the highest number in field-2 to an output file: INPUT: William J. Clinton 6 Q1124 42nd President ...
5
votes
2answers
374 views

QuickSort of Comparable[]

Here is the code for the QuickSort class that I created. ...
10
votes
2answers
161 views

Thread-safe prime number source

I am working on some multi-threaded code that requires a prime-number source. The following code keeps an array of primes in memory, extending it as needed. It works in the 'int' domain, being ...
6
votes
1answer
270 views

How to fill an ArrayList of ArrayLists with a Left Join?

I have a class Employee that contains an ArrayList of Projects. I'm storing the Employees in one table, and the Projects in another. I'm trying to find the best way to create an ArrayList of ...
6
votes
2answers
4k views

Read an input text file and store the tokens in 2 different arrays

I am very new to Java so please ignore if there are obvious mistakes. If my question seems redundant then please guide me towards the correct link. However, I have surfed enough in order to find the ...
7
votes
1answer
319 views

Creating a thread for file transfer

I am creating an application in Java that runs at scheduled intervals and it transfer files from one server to another server. For SFTP I'm using ...
10
votes
5answers
2k views

Printing star greater symbol in Java

I need to print this in Java. I have written code for this, but I feel that my code is too big. * ** *** **** *** ** * My Code: ...
21
votes
5answers
2k views

Print an ASCII diamond

This takes a width specified by user and prints a diamond of that width. It uses only three for loops, but could I reduce that further? Is there a more elegant ...
41
votes
11answers
9k views

Nesting versus GOTO: which is better to avoid?

In Java they're not really known as GOTO statements and are rather referred to as Branching Statements, but I find that the former term is a bit more indicative of ...
19
votes
6answers
5k views

Guessing a unique 4 random digits number

I've created a simple game, in which the user needs to guess 4 digits number between 0-9, generated randomly using Random() Each of the 4 digits are different from ...
11
votes
3answers
804 views

Creating all possible combinations of points

I want to create all possible combinations of points (0,0),(0,1)...(12345,6789) and then all segments from these points. The code I have written is simple and with no optimization. Is there any ...
3
votes
3answers
918 views

Decrease CPU usage in while loop

I've a application which runs with two threads. The main thread runs in one infinity-while-loop: ...
5
votes
1answer
106 views

Optimising Funny Marbles

Problem statement is at http://www.codechef.com/DEC13/problems/MARBLEGF Lira is given array A, which contains elements between 1000 and 2000. Three types of queries can be performed on this ...
31
votes
10answers
2k views

Perform instruction in loop every time except the last time?

The specific issue is this: I am writing to a file and want to output a new line after each line written. If I use a normal loop without any further checks, this will create a blank line at the very ...
7
votes
4answers
188 views

Looping Strategy: Change behaviour after first iteration

I have often encountered the following situation. I want to call a method in a Loop, but want to skip the call at the first run. It do not have to be a method, it also can be some lines of code that I ...
19
votes
6answers
1k views

How can I make this mess of Java for-loops faster?

I'm trying to find all the 3, 4, 5, and 6 letter words given 6 letters. I am finding them by comparing every combination of the 6 letters to an ArrayList of words ...
-2
votes
1answer
92 views

How would I improve this class in terms of responsiveness and performance?

I'm about to release this application as a hobby and I'm wondering how would I improve the speed of the calculations and overall responsiveness of the class? I have covered a module briefly in data ...
2
votes
1answer
245 views

how to test if for loop executed all iterations successfully

This method finds the smallest positive number that is evenly divisible by all of the numbers from 1 to 20. But I am wondering if there is a better way to test if all iterations of a for loop ...
1
vote
2answers
1k views

Calculating a total price by using switch statement and sentinel controlled loop

My homework is to make a program that calculates the total value of products sold. It works, but I use the if statement because it was asking for the quantity on ...
3
votes
2answers
1k views

Differences between using a do-while vs. while and initializing variables

When I use a do-while loop, I can create a variable userInput, but don't need to initialize it until the ...
5
votes
1answer
1k views

Reducing cyclomatic complexity

I ran a sonar analysis on my code and it told me the cyclomatic complexity is too high (sonar's limit is ten branches). Here is my code: ...
3
votes
4answers
796 views

Detecting Tic-Tac-Toe win: Enumerate all possibilities or use nested loops?

Currently I have a game that does this and then checks for victory conditions in victoryTable ...
4
votes
2answers
322 views

Optimizing if-statement with for-loop

I have been staring at this code for awhile now and I am thinking there is a way to optimize it (namely the if-else statement with the for-loops). Any advice would be greatly appreciated. ...
4
votes
3answers
285 views

Does this loop unrolling make sense?

I am coding a Hanning filter which is a recursive relation: y[i] = 0.25*(x[i] + 2*x[i-1] + x[i-2]) And here is my code were I attempt to unroll 3 times: ...
1
vote
2answers
167 views

Optimizing Java code for setting color of specific keywords in a textview

I'm developing an Android app to view C-programs.I wanted to give a simple color scheme to the text which is stored in database,retrieved as string and then passed on to the textview. The code I ...
2
votes
2answers
142 views

Refactoring same loop logic with different input data

I have these two methods: ...
2
votes
2answers
189 views
-1
votes
3answers
404 views

dead code and infinite loops with java for loop [closed]

I have a for loop that loops infinity when i don't breakout of it and only loops once when i do break out of it. I'm sure im missing something simple. The if and else should be identical aside from ...
1
vote
1answer
113 views
3
votes
2answers
148 views

New MD5 algorithm

Can you please try my algorithm and give comments or feedback about it? ...
1
vote
2answers
181 views

Setting a OnClickListener in a loop

I am trying to set a OnClickListener to a image in a loop. If the params platform is "android" then use market app, instead of default browswer. Is there a better ...
3
votes
1answer
51 views

Code to find the proper index comparing 3 values for max one

I have an algorithm and the idea is to move over an array choosing as index the index of the neighboring cell that has the max value. I.e. if array[i + 1][j + 1] has the largest value among the 3 ...
1
vote
2answers
236 views

How to Format Clean Accumulation Code?

I often find myself writing methods in languages such as Java or C++ that are only meant to loop over an array or something similar, accumulate the values, and then return the total. The problem is, ...
5
votes
1answer
891 views

Efficient looping procedure to find the longest common substring

I retrieved 200 tweets using jersey API. I want to find two tweets which have the longest common substring. This is what I have ...
2
votes
1answer
137 views

Rewriting nested for loops to give better formatted output

The following is code from a university practical I am doing. It reads in a txt file of twenty clients, whose information is stored in the txt file like this: ...
7
votes
7answers
936 views

Looping over a list, checking a boolean and return value

There are a number of different ways to do this, what do people prefer and why? ...
2
votes
2answers
80 views

Request for help to tidy up code involving arrays and loops

I posted the following code on here a few days ago: ...
5
votes
2answers
261 views

Understanding the use of for loops as counting loops in homework review [closed]

Newbie here, first time programmer. I have a homework assignment that I am working on. Instructions: Write an application that allows input of an integer n between 1 and 71, and (using a for or ...
5
votes
1answer
3k views

Java 2d array nested loops

Okay, So I have this method I made that searches a 2d array of user input column length and row length comprised of integers. The method is suppose to find 4 alike numbers in rows, columns, and both ...
5
votes
3answers
858 views

Enhanced for loop

I much prefer the Java enhanced for each loop, but I often find there are certain times where I need to break from a loop, take the following contrived example: ...
2
votes
2answers
305 views

Java - Object declaration out of while loop

Could you explain to me which code is better (more optimal) and why? Vesion 1: ...