Out of boundaries error with a 2D Array Java [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am making a game four in a row, where the number of rows and colomns is determined by the player(so can be anything, as long as more than four). So users input is stored in the variables "rows" and "column". The field looks smth like this, so the first row is always numbers.
I have trouble with the search algorithm for the winner.
While my algorithm for the horizontal search works fine, the vertical one with the same logic gives me the out of bound error.If you could please help me to spot the mistake, I would be very grateful. Thank you!
1 2 3 4 5
|-|-|-|-|
|-|-|-|-|
|-|-|-|-|
public static String checkWinner(String [][]field){
//horizontally, which works
for(int i=1; i<=rows; i++){
for (int j=0; j<=column-1;j++){
if (((j>=3 || j==column-1) && field[i][j]!="|_" && field[i][j]==field[i][j-1] && field[i][j]==field[i][j-2] && field[i][j]==field[i][j-3])
|| (field[i][j]!="|_" && field[i][j]==field[i][j+1] && field[i][j]==field[i][j+2] && field[i][j]==field[i][j+3]))
{
return field[i][j];
}
}
}
//vertically which doesn't work
for(int i=0; i<column; i++){
for (int j=1; j<=rows-1;j++){
if (((j>=4 || j==rows-1) && field[j][i]!="|_" && field[j][i]==field[j-1][i] && field[j][i]==field[j-2][i] && field[j][i]==field[j-3][i])
|| (field[j][i]!="|_" && field[j][i]==field[j+1][i] && field[j][i]==field[j+2][i] && field[j][i]==field[j+3][i]))
{
return field[i][j];
}
}
}
return null;
}

In your Horizontally loop logic you are running your loop for column from j=0 to column-1 but then you are accessing field[i][j] with values like j+1, j+2, j+3 which will go beyond column
for example : user enters column value as 5 but your logic of j+2 at j=4 & i=0 will make it to access 6th column (field[0][6]) which is obviously OutOfBound.
check for same error in Vertically Loop logic for rows value
and as NOTE: compare strings using equals() not using equality operators like ==

Related

Using ASCII to find valid Anagram [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
Why doesn't this solution work for finding valid anagram?
26/36 test cases get passed in LeetCode.
class Solution {
public boolean isAnagram(String s, String t) {
int sASCII = 0, tASCII = 0;
if(s.length() != t.length()) {return false;}
else{
for(int i = 0 ; i < s.length(); i++){
sASCII += (int)s.charAt(i);
tASCII += (int)t.charAt(i);
}
}
if(sASCII == tASCII){
return true;
}
return false;
}
}
The sums tASCII and sASCII can be equal even if the numbers are not anagrams. Let's say that you can get the number 100 by adding 60+40, but you can also get it by adding 70+30, so i recommend to use a HashMap to note every occurence of every letter or to sort the strings as arrays of chars and then compare them.

opposite of if condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to rewrite my if else statement so I skip the //do nothing part but I can't get my around to find the opposite of the if statement.
someone please help?!
if (decision.equals("repay")){
String riskClass = null;
if (doc.hasItem("riskclass")){
riskClass = doc.getItemValueString("riskclass");
}
if ( (null == riskClass) || (riskClass.equals("")) || (riskClass.equals("repay")) ){
//do nothing
} else{
//do something
}
}
You can simply invert the condition. Try the snippet below.
if ( !((null == riskClass) || (riskClass.equals("")) || (riskClass.equals("repay"))) ){
//do something
}
Another way to invert the check is invert individual conditions and replace or's with and's:
if ( (null != riskClass) && (!riskClass.equals("")) && (!riskClass.equals("repay")) ){
Another point:
Avoid call equals on variables passing constants: the constant will never be null. So in this particular case would be better write:
if (!"repay".equals(riskClass) && !"".equals(riskClass))

I'm trying to use this "If else" condition without using "System...println" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hey guys I'm completely new to Java/programming and I need help with one of my assignments. I'm suppose to use a if else condition without using System.out.println() or main method.
Here's the question "The card ranks inside the hand are given as characters inside the string, but sometimes it would be more convenient to handle these ranks as numerical values from 2 to 14. The spot cards from two to ten have their numerical values, and the face cards jack, queen, king and ace have the values of 11, 12, 13 and 14, respectively. Write a method int getRank(char c) that returns the numerical value of the card given as the character parameter c. For example, when called with argument Q, this method would return 12. You must write this method as an if-else ladder."
So far I created a program but I can't seem to get it to return char c = str.charAt(2); if char c is 4 in the String.
I've posted an image of my code and i would really appreciate the help. If there is anything I should redo or not do please tell me. I want to learn as much as I can and not just get by my course out of pure luck. I really do want to succeed in the computer science field. Thank you in advanced.
First of all as pointed out by #brk, java and javascript are not the same. They do have similar syntax but are two completely different programming languages.
Regarding your question, if we assume 2,3,4,5,6,7,8,9,T for numerical cards and J,Q,K,A for face cards, then the code would look like:
public static int getRank(char c){
if(c=='2'){
return 2;
}else if(c=='3'){
return 3;
}else if(c=='4'){
return 4;
}else if(c=='5'){
return 5;
}else if(c=='6'){
return 6;
}else if(c=='7'){
return 7;
}else if(c=='8'){
return 8;
}else if(c=='9'){
return 9;
}else if(c=='T'){
return 10;
}else if(c=='J'){
return 11;
}else if(c=='Q'){
return 12;
}else if(c=='K'){
return 13;
}else if(c=='A'){
return 14;
}
return -1;
}
To call the function you can either call it as
System.out.println("Numerical value of Queen = " + getRank('Q'));
or
store it in a variable like
int value = getRank('A');

How to write junit for this method: [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
public void declareWinner()
{
compare and declare winner
if(getLive_player1()==0 && getLive_player2()>0)
{
System.out.println("Player 2 wins with "+getLive_player2()+" cells alive.");
}
else if(getLive_player2()==0 && getLive_player1()>0)
{
System.out.println("Player 1 wins with "+getLive_player1()+" cells alive.");
}
else
{
System.out.println("There is a tie.");
}
}
Well, it's clear that you have a handful of different branches in your code, so you'll want to implement unit tests where you can hit your if and else if statements:
player1 = 0 and player2 > 0
player1 = 0 and player1 > 0
As well as a test where you hit your else statement:
Clearly multiple options, but player1 > 0 and player2 > 0 will suffice to reach that branch.
So that's at least three different #Test methods.
I'm assuming your concern is determining how to capture the System.out calls. You can change the out stream to one which points to something that can be captured programmatically. That is achieved with System.setOut():
#Test
public void testSomething() {
ByteArrayOutputStream myOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(myOut));
// do your setup and then execute declareWinner()...
String methodOutput = myOut.toString();
// Use Assert.assertEquals(...) to check methodOutput against expected value...
}
By the way
Based on what I can see, how do you know that two scores > 0 end in a tie? Seems like the else statement hasn't really closed out all of the options. But then again, I'm not sure what you're underlying code is doing...

Java int 'through' int operator? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm trying to figure out how to do something like:
int test = 1;
int test1 = 10;
if (value = test (though) test1) {
}
I've looked at oracles java operators but could not figure out how to do it.
The construct should check if value is between test and test1.
Can anybody tell me how to do this in Java?
if (value >= test && value <= test1)
{
//doSomething
}
Java does not support chained inequalities, ie test <= value <= test1, so instead you can just use two boolean expressions, connected via the boolean and operator, to get a logically equivalent conditional.
You should try something like with logical and operator
if (value > test && value < test1) {
// do something
}
or add >= to add equals comparison too.
It looks like you are looking for range operator that is common in a lot of programming languages, Java not being one of them, but the condition that you are trying to impose on the range will always be the same. You don't need to check every value in the range, merely the endpoints since it is contiguous:
if( value > test && value < test1 ) {
// do something
}
There is no through op in Java. You can do it with a simple if :
if (value >= test && value <= test1) {
// your code
}
This post begged to be clarified.
If you are checking that value is between test1 and test2 then you need:
if(value >= test && value <= test1){
// do stuff
}
Note that you should remove the = signs if value should be strictly between test and test1.
However, if you are checking that value is one of multiple tests from test0 "through" test10 for instance, then pack those tests in a set and check if value is among them:
import java.util.*;
Set tests = new HashSet();
tests.add(test);
tests.add(test1); // similarly for as many as you need
if(tests.contains(value)){
// do stuff
}

Categories

Resources