Math calculations in java [closed] - java

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

Related

Percentage from difference in 2 ints [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want the percentage from the difference of 2 ints, the way packetloss is measured as an example, if int i= 10 and int t=4 the percentage is 60%.
This is simple mathematics .
Int i=10;
Int t=4;
Float prctng=((i-t)/i )*100;
Like this.
((packetSend - packetReceive) * 100) / packetSend
So if i have well understood, you want the normalized distance between the two ints?
int a = 10;
int b = 4;
int distance = (int) 100 * (Math.abs(a-b)/Math.max(a,b)); // = 60

java array printing the repeated numbers , array in the array [closed]

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.

NullPointerException when I have clearly referenced the file correctly [closed]

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 writing a basic GUI for practice, and I want to include an image, and after double-checking all of my code, there remains a NullPointerException when I run it.
Image i;
ImageIcon ii;
...
public Pnl() {
BorderFactory.createLineBorder(Color.BLACK, 5);
setBackground(Color.GREEN);
x = 10;
y = 10;
ii = new ImageIcon(this.getClass().getResource("shrek.jpg"));
i = ii.getImage();
setDoubleBuffered(true);
timer.start();
} // end of constructor
EDIT: The NPE occurs in the line where I initialize the ImageIcon
Check the documentation for "getRecource(..)":
http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getResource(java.lang.String)
A URL object for reading the resource, or null if the resource could not be found or the invoker doesn't have adequate privileges to get the resource.

Regular expression to split geo-coordinates from a String [closed]

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 have geo-coordinates in a String like the one given below.
[79.9016492,6.8632761]
I need to get the two numbers separated as double values. Can someone please help me with writing a regular expression?
For [79.9016492,6.8632761] string, it is
String[] oxoy = "[79.9016492,6.8632761]".split("[\\[\\],]");
String x = oxoy[1]; // 79.9016492
String y = oxoy[2]; // 6.8632761
Ideone DEMO
Convert to double
Double x1 = Double.valueOf(x);
Double y1 = Double.valueOf(y);
Not exactly regex, but you can get it very easily as follows:
String[] a = "[79.9016492,6.8632761]".split(",");
double x = Double.valueOf(a[0].substring(1));
double y = Double.valueOf(a[1].substring(0,a[1].length()-1));

How do I use multidimensional arrays? [closed]

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
How would I use multidimensional arrays to store an x and y coordinate and the value that should be displayed at that coordinate. The swing tutorial really confused me and im not sure if you can read them the same way as a normal array. Here are my previous attempts:
private int[][][] idXY = new int[1000][1000][2];
int id;
public int[][] idPos(int x, int y){
return idXY[x][y][id];
}
public void setID(int x, int y, int ID){
idXY[x][y][ID] = {x}, {y}, {ID};
}
Im very sure that code doesn't make sence XD.
Thanks for your help,
AidoP
This is the syntax to store a value to a multidimensional array:
array[x][y] = value;
To access it:
array[x][y];
System.out.println(array[x][y]); // as an example

Categories

Resources