how to write method for sequential search through array? [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am writing a java method which runs through an array and if a value is present, then it returns the index of the value. It is not compiling, but I don't know what part of my code isn't comprehensive.
import java.util.*;
import static java.lang.System.out;
public class Lab26 {
public static void main(String[] args) {
}
public static int simpleSearch(int[] nums, int value) {
int nul = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == value) {
return i;
}
}
}
}

What if if (nums[i] == value) is never satisfied? You will not return anything but the method signature expects you to return an int.
in simpleSearch method, return an integer at the end.

Related

Sorting Arrays method not working [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I guess my main method has an issue in it, but i'm not really sure. Hope anyone could help.
package testing;
import java.util.*;
public class mid08 {
public static int[] Sort(int[]x) {
int max=max(x);
int[] y=new int[max+2];
for(int i=0;i<x.length;i++)
y[x[i]+1]++;
return y;
}
public static int max(int[] x){
int max=x[0];
for(int i=0;i<x.length;i++){
if(x[i]>max)
max=x[i];
}
return max;
}
public static void main(String[] args) {
int[] z = new int[] {2,3,5,1};
Sort(z);
System.out.print(Arrays.toString(z));
}
}
I'm sure of both my methods but not of the main method itself.
You ignore the result of your sort:
z = Sort(z);
System.out.print(Arrays.toString(z));
Should fix your issue.

JAVA Linked List [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
public LinkedList<MazeCoord> getPath() {
return getPath();
}
public class MazeCoord {
final private int row; // final (non-static) means it can't be updated once
final private int col; // it's initialized
// create a MazeCoord with the given row and column
public MazeCoord(int row, int col) {
this.row = row;
this.col = col;
}
// get the row of this MazeCoord
public int getRow() { return row; }
// get the col of this MazeCoord;
public int getCol() { return col; }
}
Why when I try to run this in my Eclipse, the console prompts me stack over flow?
Can any one tell me the reason?
getPath() is calling itself, which causes an infinite chain of calls, that ends when the stack overflows. A correct recursive method must have a stopping condition.

unreachable statement calling for recursive method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Writing this code, I'm getting 'unreachable statement' error when I try to compile, any time I try to reach a[x] in the recursive method.
public class recursion
{
public static boolean match (int [] a, int [] pattern)
{
if(pattern.length==0)
return true;
boolean x;
x=match(a,pattern,0,0);
if(x==true)
return true;
return false;
}
public static boolean match (int [] a, int [] pattern,int aCounter,int ptCounter)
{
int count=0;
int x=aCounter;
if(x==a.length);
{
if(count==pattern.length)
return true;
else return false;
}
if(a[x]>100)
{
count=0;
return match(a,pattern,aCounter+1,0);
}
else if(((pattern[ptCounter]==1)||(pattern[ptCounter]==0))&&((a[x]>-10)&&(a[x]<10)))
{
count++;
return match(a,pattern,aCounter+1,ptCounter+1);
}
else if(((pattern[ptCounter]==2)||(pattern[ptCounter]==0))&&(((a[x]<-10)&&(a[x]>-100))||((a[x]>9)&&(a[x]<100))))
{
count++;
return match(a,pattern,aCounter+1,ptCounter+1);
}
}
}
Would appreciate input regarding this issue and also about the calling of recursive method. Thank you!
Your problem is an unnecessary ; :
if(x==a.length); // here
{
if(count==pattern.length)
return true;
else return false;
}
This ; closes the if statement, so the following bock is always executed (and returns true or false), and the code after that block becomes unreachable.

Why doesnt string.equals("astring") work? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Why don't I get 20 after delivering e.g. "Obsidian" to Test(String pStr) if I call getMatInt() ?
Also tried .toString() after all String-declariations, also declarated e.g. "Obsidian" as new String a. Nothing works.
getBonus is aways returning a 0 instead of a 20/30/... .
I already tried "Obsidian" and "obsidian", both doesnt work for me ...
public class test
{
private String str;
private int matInt;
private int bonus;
private int magic;
public test(int pMagic, String pStr)
{
int magic = pMagic;
str = pStr;
}
private void materialEquals()
{
if(str.equals("Obsidian"))
{
matInt = 20;
}
.....
}
private void calcBonus()
{
materialEquals();
bonus = magic * matInt;
}
public int getBonus()
{
calcBonus();
return bonus;
}
}
try:
public int getMatInt()
{
materialEquals();
return matInt;
}
There is no reason for this in your constructor: str = new String(pStr);, just use str = pStr.
In fact, you might be better off setting matInt in your constructor:
public test(String pStr)
{
str = pStr;
materialEquals();
}
And depending on how many materials you have, you might want to look into using enumeration.
Ok after your edits:
public int getBonus()
{
calcBonus(); //bonus won't be calculated otherwise
return bonus;
}
After further edits:
Your constructor is wrong, you're not initializing int magic. Try this constructor instead.
public test(int pMagic, String pStr)
{
this.magic = pMagic; //int magic = pMagic was a new variable only in the constructor scope
this.str = pStr;
calcBonus();
}
Also you might as well calculate the bonus on construction.
You will need to call materialEquals() so that 20 can be assigned to matInt upon equals comparison, as integers are always initialized to default value 0 upon declaration.

How can I instantiate the class DefaultHashMap [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to access the DefaultHashMap class but getting error in the main method. Could anyone please tell me what is the problem?
import java.util.Random;
import java.util.*;
public class PythonToJava {
public static void main(String[] args) {
Random rm = new Random();
int i = rm.nextInt(1000);
HashMap<Integer,Integer> stats = new HashMap<Integer,Integer>();
DefaultHashMap<K,V> default = new DefaultHashMap<K,V>();
System.out.println("Random Number Generated is: " + i);
for (int j = 0; j<i; j++){
int value = rm.nextInt(500);
System.out.println("The value of VALUE is " + value);
}
}
}
class DefaultHashMap<K,V> extends HashMap<K,V> {
protected V defaultValue;
public DefaultHashMap(V defaultValue) {
this.defaultValue = defaultValue;
}
#Override
public V get(Object k) {
V v = super.get(k);
return ((v == null) && !this.containsKey(k)) ? this.defaultValue : v;
}
}
Please help me in rectifying the errors I'm encountering at the line with the code:
DefaultHashMap<K,V> default = new DefaultHashMap<K,V>();
The K and V are type parameters, and here, you need to use concrete types to substitute them, the same as when you are using HashMap.
K,V has to be objects
You cannot use variable default. Its a java reserve word.
You should read about java generics
http://docs.oracle.com/javase/tutorial/java/generics/why.html

Categories

Resources