Parsing an array of integers into a single int with Java [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 8 years ago.
Improve this question
I am trying to make this work as a Python user forced to use Java:
private int parseInt(char[]){
private int parsed = 0
for (i=0 ; i < char.length(); i++;)
{parsed += (10**i) * char[i];}
}
I need to know why this method refuses to work and also how to fix it without using a built in function

First of all, change 10**i to Math.pow(10,i).
Then name your char[] array. Let's call it c.
Then, change c[i] to c[i]-'0', assuming c[i] is a digit.
private int parseInt(char[] c)
{
int parsed = 0;
for (i=0 ; i < c.length; i++)
parsed += Math.pow(10,i) * (c[i]-'0');
return parsed;
}

Related

Problem of printing ABC... and a number in Java [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 2 years ago.
Improve this question
This is the function I set:
public int printABCNum(p) {
char textinfo = 65;
String textValue = "";
while (textinfo < 91) {
textValue+=textinfo;
textinfo++;
}
System.out.println(textValue + Integer.toString(p));
Eclipse (a text editor) says that there is a problem with public int printABCNum(p) bit. Basically I'm trying to do is run a function printing "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and an integer. I'm new to Java and is not good 'enough' at programming with it and finding bugs, so please help!
A p parameter of your printABCNum function is missing a type. Based on context it should be an int. Another problem is your function return type is defined to int, but you haven't returned any value.
Your code should be:
public void printABCNum(int p) {
char textinfo = 65;
String textValue = "";
while (textinfo < 91) {
textValue += textinfo;
textinfo++;
}
System.out.println(textValue + p);
}
It looks like there's a few things wrong here.
You shouldn't have a return type int if you are not returning anything.
The parameter needs to have a type, as such: int p
Other than that looks fine. Take a look below at full answer that worked for me.
public void printABCNum(int p) {
char textinfo = 65;
String textValue = "";
while (textinfo < 91) {
textValue+=textinfo;
textinfo++;
}
System.out.println(textValue + Integer.toString(p));
}

Method to work out win/loss/tie and return points (football) [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 2 years ago.
Improve this question
static final int WIN = 3;
static final int TIE = 1;
static final int LOSS = 0;
public static int berekenWedstrijdPunten(int[] mpUserTeamPunten, int[] mpTegenstanderTeamPunten){
if (mpUserTeamPunten > mpTegenstanderTeamPunten){
return WIN;
}
if (mpUserTeamPunten == mpTegenstanderTeamPunten){
return TIE;
}
if (mpUserTeamPunten < mpTegenstanderTeamPunten) {
return LOSS;
}
}
getting error lines under the if statements with > & <, trying to get a return of the win/tie/loss point into a print statement.
You can't compare two arrays with logical operators.

Java When I run this method in my code, sometimes it shows full result, other times it doesn't [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 7 years ago.
Improve this question
If this function is running:
public static void maximizeweight(int[] weight, int[] A){
Random r = new Random();
int random = r.nextInt(A.length);
for(int i=0;i<A.length;i++){
while(totalweight(weight,A) < 630){
if(A[random]==0)
A[random] += 1;
}
}
}
The output some times breaks a bit or freezes eclipse, totally at random, and sometimes it is able to give the whole result, other times it doesn't show the last part of the desired result.
If your A[random] != 0 and still your totalweight() returns < 630, the while loop would be infinite. One possible fix (and the one I think you need) is to move your int random = r.nextInt(A.length); inside your while loop.
public static void maximizeweight(int[] weight, int[] A){
Random r = new Random();
for(int i=0;i<A.length;i++){
while(totalweight(weight,A) < 630){
int random = r.nextInt(A.length);
if(A[random]==0)
A[random] += 1;
}
}
}
Note: This would still loop infinitely if the sum of weight array is still < 630. So you will need additional checks.

Repeat String Recursively [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 8 years ago.
Improve this question
This is homework. I get the logic but i got stuck on the code. I've done it with normally way and it takes 1 week to get the code. I need to get repeat string with recursive way in Java.
This is my code :
static String repeatString (final int n, final String[] syllables, final String currentWord) {
if (n == 0) {
System.out.println(currentWord);
} else {
for (int i = 0; i < syllables.length; i++) {
repeatString(n - 1, syllables, currentWord + syllables[i]);
}
}
return "";
}
if i call in main method like
String[] str = {"a", "b"};
repeatString(1, str, " ");
then i get output (a,b) if i change to
repeatString(2,str," ");
then i get output ( aa,ab,ba,bb) if i change to
repeatString(3,str," ");
then i get output (aaa,aab,aba,abb,baa,bab,bba,bbb) and so on.
So basically it is like 2 to the power to n. If n=1, i got 2, if n=3, i got 8, and so on.
I would be grateful if someone can help me to get this code in recursive way.
Any help is much appreciated.
The method you have there is recursive already. Being recursive does NOT mean it should have no for loops. A recursive method in cheap words means the method calls itself, which yours does.

need code or logic behind hist command in matlab [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i need a help.i am using matlab code that uses hist command,i need the java equivalent code or the logic behind hist function in matlab so that i can code it in java or c.Thanks in advance.
Simple logic for histogramming (assuming you have known bins of constant width, and you don't need the most efficient code possible):
float x[50]; // assumed to be array of data values
float binWidth, firstBin; // bins of width binWidth; first one centered on firstBin
int numBins; // number of bins
int *bins, tooSmall = 0, tooLarge = 0, ii, indx;
bins = (int*)calloc(numBins * sizeof(int)); // allocate, set to zero
for(ii = 0; ii < 50; ii++) {
indx = floor((x[ii]-firstBin)/binWidth + 0.5);
if (index < 0 ) {
tooSmall++;
}
elseif (index >= numBins) {
tooLarge++;
}
else {
bins[indx]++;
}
}
}
At the end you have a histogram of the data in x, with two counters corresponding to the data that didn't fit in the range (either under, or over range).
Disclaimer: written without compiler to test. Looks "about right" - test it on a known case before relying on it.

Categories

Resources