Conditionally concatenate strings in for-loop, or use predefined string - java

I have the following HashMap:
private HashMap<String, Team> allTeams = new HashMap<String, Team>();
I want to return as a String all teams in my league, otherwise return a message "no teams in league".
I have written this code:
public String getTeam()
{
String x = "";
for(Team tm : allTeams.values())
{
if(tm.getStatus().equals("Added"))
{
x = x + tm.toString();
}
else
{
x = "there are no teams in your league";
}
}
return temp;
}
If I remove the "else" part of the conditional statement the code works.
However if I keep the "else" part, I continuously receive "there are no teams in your league" and I understand it is because once all teams have been returned there will be no further teams to return hence the "else" part of the statement is always printed.
How can I get this to work?

One way would be to check if x is empty outside of the loop, and set it if it is.
public String getTeam()
{
String x = "";
for(Team tm : allTeams.values())
{
if(tm.getStatus().equals("Added"))
{
x = x + tm.toString();
}
}
if (x.isEmpty())
{
x = "there are no teams in your league";
}
return x;
}
I assumed you meant to return x, not temp, as temp isn't defined here.

Related

Using a for-each loop to return largest value in arraylist

I'm having trouble writing a for-each loop that searches the arraylist and returns the county's name within the continent that has the highest gdp. Here's my code for it right now. (ElementsList is the original ArrayList)
public Country highestGdp(String continent) {
boolean flag;
for (Country cont : ElementsList) {
if (cont.getContinent().equals(continent)) {
ArrayList<Country> TMP1 = new ArrayList<Country>();
TMP1.add(cont);
for (Country gdp : TMP1) {
double max = 0;
if (max < gdp.getGDP()) {
max = gdp.getGDP();
}
if (gdp.getGDP() == max) {
ArrayList<Country> TMP2 = new ArrayList<Country>();
TMP2.add(gdp);
}
return gdp;
}
}
}
return null;
}
Each time you find a country in the right continent, you can check to see if it is greater than the max so far. Don't need to loop through all of them each time.
public Country highestGdp(String continent) {
boolean flag;
Country maxCountry = null;
for (Country cont : ElementsList) {
if (cont.getContinent().equals(continent)) {
if (maxCountry == null) maxCountry = cont;
if (maxCountry.getGDP() < gdp.getGDP()) {
maxCountry = cont;
}
}
}
return maxCountry;
}
Sorry for saying it but Your code is a little messy ;)
To shortly solve Your problem, try to move max declaration before the loop like this:
[...]
double max = 0;
for(Country gdp : TMP1){
[...]
We can see that TMP2 is completely useless, remove it:
// ArrayList<Country> TMP2 = new ArrayList<Country>();
// TMP2.add(gdp);
You create TMP1 list always with only 1 element and then iterate over it. This is also useless, You can do the code directly on the element You are adding to the list.
First iteration over ElementList is a list of Country elements, but the element You iterate is called cont (=continent) which is a Continent and not the Country. Is it intended to use Country class to cover both: Countries and Continents? Do You plan to have a tree structure like "Continents contains many Countries"?
Final code to solve problem from Your original question should be like this:
public Country highestGdp(String continent){
Country countryWithMaxGdp = null;
for(Country cont: ElementsList ){
if(cont.getContinent().equals(continent)){
if(countryWithMaxGdp == null || countryWithMaxGdp.getGDP() < cont.getGDP()){
countryWithMaxGdp = cont;
}
}
}
return countryWithMaxGdp;
}

Is performance gained when using continue in a for-loop with many if-statements?

I have a for loop in a java program which iterates through a set of maps.
Inside the loop I have around 10 different if-statements which checks the name of each key inside the each map.
Example:
for (<String, Object> map : object.entrySet()) {
if (map.getKey().equals.("something") {
do_something;
continue;
}
if (map.getKey().equals.("something_else") {
do_something_else;
continue;
}
if ...
}
Do I gain any performance when adding continue-statements like this?
When I step through my code in my IDE and NOT have these continue statements, each if-statement will be tested even if the first one matches.
If I have them like this and the first if matches, the for loop will skip the next 9 if-statements and continue with the next object.
Maybe the compiled code will treat it differently and the added continue-statements actually makes the loop slower?
Instead of using continue all the time, do the getKey() just once and use else if:
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
if (key.equals("something")) {
// ...
} else if (key.equals("something else")) {
// ...
}
}
Or use a switch statement:
for (Map.Entry<String, Object> entry : map.entrySet()) {
switch (entry.getKey()) {
case "something":
// ...
break;
case "something else":
// ...
break;
}
If you want the current iteration to end after the first condition evaluates to true, you should use if-else-if-...-else. In my opinion, that's more clear than using continue, since that's what this syntax exists for.
for (<String, Object> map : object.entrySet()) {
if (map.getKey().equals.("something") {
do_something;
}
else if (map.getKey().equals.("something_else") {
do_something_else;
}
else if (...) {
...
}
... else {
...
}
}
With your current implementation, yes you are gaining a performance boost by skipping the remaining if statements using the continue keyword, although with only a constant of ten "if" statements, it's not that bad (10n = O(n) time). Having said that, the more practical way to approach this, as Eran stated, is to make use of else if statements, which will achieve the same result that you are currently using.
Because you have just a few values, IMO, you'll have a real performance improvement here if you map your strings to ints, since the int comparison is far faster than a String comparison.
Check this out
public class Lab1 {
public static void main(String[] args) {
usingStrings();
usingInts();
}
private static void usingInts() {
int[] samples = new int[100000000];
int[] values = {1,2,3,4};
for(int i=0;i<samples.length-1;i++) {
samples[i] = values[(int)(Math.random()*values.length)];
}
int total = 0;
long ini = System.currentTimeMillis();
for(int i=0;i<samples.length-1;i++) {
if (1 == (samples[i])) {
total+=doSomeJob();
}else if (2 == (samples[i])) {
total+=doSomeJob();
}else if (3 == (samples[i])) {
total+=doSomeJob();
}else {
total+=doSomeJob();
}
}
long end = System.currentTimeMillis();
System.out.println("Ints="+(end-ini));
}
private static void usingStrings() {
String[] samples = new String[100000000];
String[] values = {"one mule","two mules","three mules","four mules"};
for(int i=0;i<samples.length-1;i++) {
samples[i] = values[(int)(Math.random()*values.length)];
}
int total = 0;
long ini = System.currentTimeMillis();
for(int i=0;i<samples.length-1;i++) {
if ("one mule".equals(samples[i])) {
total+=doSomeJob();
}else if ("two mules".equals(samples[i])) {
total+=doSomeJob();
}else if ("three mules".equals(samples[i])) {
total+=doSomeJob();
}else {
total+=doSomeJob();
}
}
long end = System.currentTimeMillis();
System.out.println("Strings="+(end-ini));
}
/**
*
*/
private static int doSomeJob() {
int c = 0;
for(int i=0;i<1000;i++) {
c++;
}
return c;
}
}
output
Strings=962
Ints=6
which is actually how DBMS indexes work behind the scenes

Get the second same value in the ArrayList<String>

I am trying to add marker New direction to the arrayList mergeArray when I found the second 3 value in the buffer arrayList but I am always getting the first 3 value in the ArrayList. How can I get the second one after Amsterdam?
I appreciate any help.
output:
paris
3
water
ball
money
Amsterdam
3
door
output should looks like this:
paris
3
water
ball
money
New direction
Amsterdam
3
door
Code:
public static void main(String[] args) {
ArrayList<String> buffer = new ArrayList<String>();
ArrayList<String> mergeArray = new ArrayList<String>();
String route = "3";
String direction = "paris";
String start = "Amsterdam";
buffer.add("paris");
buffer.add("3");
buffer.add("water");
buffer.add("ball");
buffer.add("money");
buffer.add("Amsterdam");
buffer.add("3");
buffer.add("door");
for (String line : buffer) {
if (line.equals(route)) {
mergeArray.add(line);
int index = buffer.indexOf(line);
String prevElement = buffer.get(index - 1);
if (prevElement == direction) {
String addElem = buffer.get(index + 1);
mergeArray.add(addElem);
} else if (prevElement == start) {
mergeArray.add("New direction");
}
}
}
for (String key : mergeArray) {
System.out.println(key);
}
}
Do not use indexOf as it will always retrieve the index of the first appearance.
Keep an auxiliary index variable and use it in your loop:
int auxIndex = 0;
for (String line : buffer) {
if (line.equals(route)) {
mergeArray.add(line);
String prevElement = buffer.get(auxIndex - 1);
if (prevElement.equals(direction)) {
String addElem = buffer.get(auxIndex + 1);
mergeArray.add(addElem);
} else if (prevElement.equals(start)) {
mergeArray.add("New direction");
}
}
auxIndex++
}
also add safety checks so the index will not under/over-flow
Try using a clasic for, instead of one that uses Iterable
for (int i=0; i<buffer.size(); i++) {
String line = buffer.get(i);
if (line.equals(route)) {
mergeArray.add(line);
String prevElement = buffer.get(i - 1);
if (prevElement == direction) {
String addElem = buffer.get(i + 1);
mergeArray.add(addElem);
} else if (prevElement == start) {
mergeArray.add("New direction");
}
}
}
Also, consider checking for over/underflow problems. You refer to index-1 and index+1, which will cause trouble if the appearance is on the first or last position.

java: hangman game repeating letters

I´ve got a logic problem with a hangman project, it takes an letter from the user and searches if that letter is contained within the secret word. The problem is the way I´ve programmed it, if there are several occurrences of the letter the user guessed within the secret word. it will just go through and denote them all. Which is not what I want, I only want it to update the status of correctly guessed letter one at a time.
I tried some different stuff like setting a break after status(guessCh, but then the iterator will just go to the first occurrence where the letters match and stop there.
any simple fix to this?
private void compare(String str)
{
guessCh = str.charAt(0);
char secretCh = '0';
for (int i = i2; i < secretWord.length(); i++) // Cuts the secret word into individual chars to process.
{
secretCh = secretWord.charAt(i);
// Compare the two strings.
if (guessCh == secretCh)
{
status(guessCh, i); // Sends the letter & placement to status().
}
}
}
n
private String status(char guessCh, int placement)
{
/* Update and return status. */
if (guessCh >='A' && guessCh <= 'Z')
{
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement,guessCh).toString();
println("That guess is correct.");
canvas.displayWord(status);
return status;
}
return status;
}
You could test for a prior solution using your status variable from inside your compare method.
if (guessCh == secretCh && status.charAt(i) != secretCh)
{
status(guessCh, i);
break;
}
From what I can read (and understand), the basic problem you're facing is caused by the for loop in the compare method.
(Note, my examples are case sensitive, you will need to take that into account)
There are two basic approaches I can suggest...
The first is, match ALL occurrences with a single check...
private char guessCh;
private String secretWord;
private String status;
private String secretBuffer;
public TestStringCompare() {
secretWord = "This is a test";
// This is a copy of the secret word, this ensures that
// we always have a copy of the original.
secretBuffer = secretWord;
status = "______________";
guessCh = 'i';
compare("i");
}
private void compare(String str) {
while (secretBuffer.contains(str)) {
int foundAt = secretBuffer.indexOf(str);
status(str.charAt(0), foundAt);
// We want to remove the "guess" from our check string
// so it doesn't cause a false positive in the future
StringBuilder sb = new StringBuilder(secretBuffer);
sb.replace(foundAt, foundAt + 1, "_");
secretBuffer = sb.toString();
System.out.println(secretBuffer);
}
}
private String status(char guessCh, int placement) {
/* Update and return status. */
if (Character.isLetter(guessCh)) {
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement, guessCh).toString();
System.out.println("That guess is correct.");
System.out.println(status);
}
return status;
}
Which would produce:
That guess is correct.
__i___________
Th_s is a test
That guess is correct.
__i__i________
Th_s _s a test
or, replace the first occurrence of the guess (which from what I understand is what you're after)
public class TestStringCompare {
public static void main(String[] args) {
new TestStringCompare();
}
private char guessCh;
private String secretWord;
private String status;
private String secretBuffer;
public TestStringCompare() {
secretWord = "This is a test";
secretBuffer = secretWord;
status = "______________";
guessCh = 'i';
compare("i");
}
private void compare(String str) {
if (secretBuffer.contains(str)) {
int foundAt = secretBuffer.indexOf(str);
status(str.charAt(0), foundAt);
// Some where here you need to remove the "guess" character
// to ensure that it doesn't get repeated...
StringBuilder sb = new StringBuilder(secretBuffer);
sb.replace(foundAt, foundAt + 1, "_");
secretBuffer = sb.toString();
System.out.println(secretBuffer);
}
}
private String status(char guessCh, int placement) {
/* Update and return status. */
if (Character.isLetter(guessCh)) {
status = new StringBuffer(status).deleteCharAt(placement).toString();
status = new StringBuffer(status).insert(placement, guessCh).toString();
System.out.println("That guess is correct.");
System.out.println(status);
}
return status;
}
}
Which would produce this...
That guess is correct.
__i___________
Th_s is a test
I am guessing this is homework of some sort - but anyway, why not use indexOf

Calling Two Different Methods into One Method in Java

I wish to:
Reading in two files
Split the files into individual strings
Compare the two string lists and retrieve strings that are unique to a file.
At the moment I am running in to the problem of finding a way to call the two methods used to call in the files (one for each file) to the same method in order to be compared.
Both methods use a try-catch-while statement and if I try to read all of the entries after the while statement only a single is shown and not the entire list.
Is there a way to send parts of both methods as parameter to a single new method?
Here is the code for the program. I know that there are problems with the way that I am doing the program, but I am only doing it the way that I was taught.
File mainEmails = new File("Testrun.txt");
Scanner inputScanner = null;
int counter = 1;
String fullName = null;
String position = null;
String companyName = null;
String telNumber = null;
String emailAddress = null;
try
{
inputScanner = new Scanner(mainEmails);
}
catch(FileNotFoundException e)
{
System.out.println("File has not been found.");
}
while (inputScanner.hasNextLine())
{
String nextLine = inputScanner.nextLine();
String [] splitFile = nextLine.split(",");
for (int i = 0; i <splitFile.length;i++)
{
if(i==0)
{
fullName = splitFile[0];
}
else if(i==1)
{
position = splitFile[1];
}
else if(i==2)
{
companyName = splitFile[2];
}
else if(i==3)
{
telNumber = splitFile[3];
}
else if(i==4)
{
emailAddress = splitFile[4];
}
else if(splitFile[i] == null)
{
System.out.println("You have failed!");
}
}
}
public static void deletionList()
{
File deletionEmails = new File("Testrun1.txt");
Scanner inputScanner1 = null;
String deletionfullName = null;
String deletionposition = null;
String deletioncompanyName= null;
String deletiontelNumber = null;
String deletionemailAddress = null;
try
{
inputScanner1 = new Scanner(deletionEmails);
}
catch(FileNotFoundException e)
{
System.out.println("File has not been found.");
}
while (inputScanner1.hasNextLine())
{
String deletionnextLine = inputScanner1.nextLine();
String [] deletionsplitFile = deletionnextLine.split(",");
for (int i = 0; i <deletionsplitFile.length;i++)
{
if(i==0)
{
deletionfullName = deletionsplitFile[0];
}
else if(i==1)
{
deletionposition = deletionsplitFile[1];
}
else if(i==2)
{
deletioncompanyName = deletionsplitFile[2];
}
else if(i==3)
{
deletiontelNumber = deletionsplitFile[3];
}
else if(i==4)
{
deletionemailAddress = deletionsplitFile[4];
}
else if(deletionsplitFile[i] == null)
{
System.out.println("You have failed!");
}
}
}
}
What I am trying to do is to take the fullName, emailAddress from the first split and deletionfullName and deletionemailAddress from the second split and compare the first and second of each, respectively. Each file will have a number of fields in it, and I am only interested in the fullName and emailAddress fields.
It is quite confusing to understand how you are trying to implement your solution, so may I suggest you look at a different way of doing the whole read-and-compare process. For example, I would suggest doing something like this... (in psuedocode)
public void compareFiles(String file1, String file2){
// Read the lines of each file into String[] arrays
String[] file1Lines = readAndSplitIntoLines(file1);
String[] file2Lines = readAndSplitIntoLines(file2);
// compare the lines
for (int x=0;x<file1Lines.length;x++){
for (int y=0;y<file2Lines.length;y++){
if (file1Lines[x].equals(file2Lines[y])){
// match. set it to null
file1Lines[x] = null;
file2Lines[y] = null;
// break out of the inner loop and start comparing the next line
break;
}
}
// remove the duplicates (which are now null values), creating a smaller array of uniques.
String[] newFile1 = shrinkArrayByRemovingNulls(file1Lines);
String[] newFile2 = shrinkArrayByRemovingNulls(file2Lines);
}
Besides the fact that your question is not very clear, you have at least one glaring problem:
DO NOT use exception handling for logic! Exception handling should be only for exceptions.
Secondly, think about what you are really looking to do. In pseudocode it would look something like this:
list1 = split(file(name1).read())
list2 = split(file(name2).read())
list3 = unique(list1, list2)
What does your code look like?

Categories

Resources