Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
Please could you tell me how its work in section (g[ss[i]]++;) and tell me the sequence of output in java
class A{
public static void main(String []a){
int []ss={1,2,3,4,2,3,3,1,1,1,5,6,4,5,4};
int []g=new int[15];
for(int i=0;i<15;i++){
g[ss[i]]++;
}
for(int i=1;i<15;i++){
System.out.println(ss[i-1]+"=="+g[i]);
}
}
}
Can't you run it?
g[ss[i]]++; can be rewritten as
int index = ss[i];
g[index] = g[index] + 1;
So it's counted number of each number in ss.
It's very error prone, and you should never do something like that.
Just run it?
1==4
2==2
3==3
4==3
2==2
3==1
3==0
1==0
1==0
1==0
5==0
6==0
4==0
5==0
This should be your output.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I'm trying to find the lentgh of a set from a preference.
I tired set.length, but it doesnt work. Anyone know another way to find a length of a set?
set = prefs.getStringSet("key", null);
int X = set.length; //it doesn't like length....
I believe instead of set.length, you'll want to call set.size()
Your set is a Set<String>, not a String[]. So you have to call:
int x = set.size();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
Is there an easy way(instead of hard coding) to display intermidiate calculation in java?
For example: x+y*3= 10+15*3=55
I have searching for an answer without success.
You could do something like this -
public static void main(String[] args) {
int x = 10;
int y = 15;
System.out.printf("x+y*3 = %d+%d*3 = %d", x, y, (x + (y * 3)));
}
Which outputs
x+y*3 = 10+15*3 = 55
here.
Try to learn parsing strings as shown here: http://www.javapractices.com/topic/TopicAction.do?Id=87
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I've been playing with this code for an hour now trying to make it only accept integers. Atm when you input a character the loop is infinite, i can't seem to progress from here.
do
{
System.out.println("Enter student's number: ");
}
while (!in.hasNextInt());
number = in.nextInt();
Any help would be much appreciated.
Thanks
while (!in.hasNextInt());
Here, in.hasNextInt() gives you a true when in has next element in its stream as an int.
If it is not int, it will return false.
!false = true, which projects :
while(true);
this leads to the infinite loop.
Hope you understood.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I want to convert an array of ints to 1 int.
e.g. I have an array of ints {1,2,3,4,5} and want to convert it to the int 12345
How do I do this?
Iterate through the array, and concatenate the values to String and convert that String to int
String valueSt = "";
for(int val : array) {
valueSt += val;
}
int finalValue = Integer.valueOf(valueSt);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I'm working on with a problem where I should print stars (*) with Java. Here's the pattern that program should print: There should be three lines of stars
Line 1. 5 Stars.
Line 2. 3 Stars.
Line 3. 9 Stars.
Here's what I have a drafted so far:
private static void printStars(int number) {
System.out.print("*");
}
public static void main(String[] args) {
printStars(5);
printStars(3);
printStars(9);
}
private static void printStars(int number) {
System.out.print("*");
}
This method requires a loop to print the required number of stars, rather than just one (which is what you're doing at the moment, ignoring the parameter that's passed in.) Look up "for loops" - that's in all likelihood what you're required to use here.