I'm trying to create another array but I want it to be the array double gainT[], which is the temperature in Fahrenheit, converted to Celcius to make another array called double gainTC[]. I am also trying to do the same thing but with the precipitation. I just can't seem to find a way to do it though.
import java.io.IOException;
import java.util.Scanner;
public class AnnualClimate1 {
public static void main(String [] args) throws IOException
{
Scanner in = new Scanner(System.in);
System.out.print("Choose the temperature Scale (F = Fahrenheit, C = Celsius): ");
String tFC = in.nextLine();
System.out.print("Choose the precipitation Scale (I = Inches, C = Centimeters): ");
String pIC = in.nextLine();
System.out.println("");
System.out.println("");
System.out.println(" Climate Data");
System.out.println(" Location: Gainesville, Florida");
System.out.println(" Temperature " + tFC + " Precipitation " + pIC);
System.out.println("=================================================");
double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double gainTC[] = {(gainT[] - 32) / 1.8};
double gainP[]={3.5, 3.4, 4.3, 2.9, 3.2, 6.8, 6.1, 6.6, 4.4, 2.5, 2.2, 2.6};
double gainPC[] = {gainP[] / .3937};
if(tFC.equalsIgnoreCase("F") && pIC.equalsIgnoreCase("I")){
System.out.println("Jan. " + gainT[1] + " " + gainP[1]);
System.out.println("Feb. " + gainT[2] + " " + gainP[2]);
System.out.println("Mar. " + gainT[3] + " " + gainP[3]);
System.out.println("Apr. " + gainT[4] + " " + gainP[4]);
System.out.println("May " + gainT[5] + " " + gainP[5]);
System.out.println("Jun. " + gainT[6] + " " + gainP[6]);
System.out.println("Jul. " + gainT[7] + " " + gainP[7]);
System.out.println("Aug. " + gainT[8] + " " + gainP[8]);
System.out.println("Sep. " + gainT[9] + " " + gainP[9]);
System.out.println("Oct. " + gainT[10] + " " + gainP[10]);
System.out.println("Nov. " + gainT[11] + " " + gainP[11]);
System.out.println("Dec. " + gainT[12] + " " + gainP[12]);
}
else if(tFC.equalsIgnoreCase("C") && pIC.equalsIgnoreCase("C")){
System.out.println("Jan. " + gainTC[1] + " " + gainPC[1]);
System.out.println("Feb. " + gainTC[2] + " " + gainPC[2]);
System.out.println("Mar. " + gainTC[3] + " " + gainPC[3]);
System.out.println("Apr. " + gainTC[4] + " " + gainPC[4]);
System.out.println("May " + gainTC[5] + " " + gainPC[5]);
System.out.println("Jun. " + gainTC[6] + " " + gainPC[6]);
System.out.println("Jul. " + gainTC[7] + " " + gainPC[7]);
System.out.println("Aug. " + gainTC[8] + " " + gainPC[8]);
System.out.println("Sep. " + gainTC[9] + " " + gainPC[9]);
System.out.println("Oct. " + gainTC[10] + " " + gainPC[10]);
System.out.println("Nov. " + gainTC[11] + " " + gainPC[11]);
System.out.println("Dec. " + gainTC[12] + " " + gainPC[12]);
}
}
}
Here is an incomplete example so you can fill in the blanks yourself:
double gainT[]={54.3, 57.0, 62.5, 67.6, 74.3, 79.2, 80.9, 80.4, 77.8, 70.1, 62.8, 56.3};
double gainTC[] = new double[gainT.length]; //create array which matches the size of gainT
//array.length is a property value returning the 'size' or length or the array
//Now just iterate through all the gainT[] values you populated above and read each one
for(int i = 0; i < gainT.length; i++){
double math = //use your conversion math here, and store the value
gainTC[i] = math; //assign the results of your math to the same spot in the new array
}
If you'd like more information on Loops and Arrays, check here:
For Loops: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
Arrays: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
Related
"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
point + " WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;
this query updates my value, but I want it to add to existing value. How can I fix this problem ?
I guess you want to add the value of point to the existing value of DatabaseFıelds.SUBJECT_PROGRESS, right? You can do it like this:
"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
DatabaseFıelds.SUBJECT_PROGRESS + " + " + point +
" WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;
it's works Thank you forpas"UPDATE " + DatabaseFıelds.TABLE_NAME +
" SET " + DatabaseFıelds.SUBJECT_PROGRESS + " = " +
DatabaseFıelds.SUBJECT_PROGRESS + " + " + point +
" WHERE " + DatabaseFıelds.SUBJECT_ID + " = " + id;
i need your help!
So basically i made a Search Student Tab who read a .txt file and
output on textArea.
Image
But after search i take this:
Output result
But i want to take this:
as Output on TextArea
So here is my code:
StringBuilder rsl = new StringBuilder();
#FXML
public void PressedSearch(ActionEvent event) throws IOException { //Ektelei Search sto arxeio ton mathiton
if (searchStudent() == false) {
result.setStyle("-fx-font:13 sherif;-fx-text-fill: red");
result.setText("Failed to find the student");
} else {
result.setStyle("-fx-text-fill: black");
rsl.setLength(0);
}
}
public boolean searchStudent() throws IOException { //Sto search o admin mporei na dei perissotera pragmata
Path p = Paths.get("src", "inware", "users.txt");
Scanner in = null;
boolean i = false;
in = new Scanner(p);
while (in.hasNext()) {
String line = in.nextLine();
String[] fields = line.split("[,]");
if (line.contains(search_field.getText())) {
rsl.append(fields[4] + " " + fields[5]+ " " + fields[6] + " " + fields[7] + " " + fields[8]
+ " " + fields[9] + " " + fields[10]+ " " + fields[11]+ " " + fields[12]+ " " + fields[13]
+ " " + fields[14]+ " " + fields[15] + " " + fields[16] + " " + fields[17] + " " + fields[18]
+ " " + fields[19] + " " + fields[20] + " " + fields[21]);
rsl.append("\n"); //Gia na kanei print polla atoma
i = true;
}
}
result.setText(rsl.toString());
in.close();
return i;
Try to use String.format(String format,Object... args).
Instead of using:
rsl.append(fields[4] + " " + fields[5]+ " " + fields[6] + " " + fields[7] + " " + fields[8]
+ " " + fields[9] + " " + fields[10]+ " " + fields[11]+ " " + fields[12]+ " " + fields[13]
+ " " + fields[14]+ " " + fields[15] + " " + fields[16] + " " + fields[17] + " " + fields[18]
+ " " + fields[19] + " " + fields[20] + " " + fields[21]);
rsl.append("\n"); //Gia na kanei print polla atoma
You can use:
String format = "%10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s %10s\n";
rsl.append(String.format(format,fields[4],fields[5],fields[6],fields[7],fields[8],fields[9],fields[10],fields[11],fields[12],fields[13],fields[14],fields[15],fields[16],fields[17],fields[18],fields[19],fields[20],fields[21]));
In format String, the number between '%' and 's'( Width field) represents the minimum number of characters to output. You can change it according to your needs.
I converted a raytrace algortihm for C++ from scratchapixel.com site into an opencl java(compiled for gpu) form and it doesnt work for depth values equal to or greater than 7. When I set depth to 7(or higher), compiler tells:
Error:E013:Insufficient Private Resources!
because of the semi-recursive functions that each one gets another copy of 32bit private registers(60+). I need those registers to spill to main memory so I can set higher depth values without making the kernel into an iterative version. Is enabling spilling a bad idea? If not, how can I enable it?
and if I set number of spheres to value bigger than 100ish values, compiler tells:
Frontend phase failed compilation.
Error: Creating kernel rayTraceSphereRender failed!
but total buffer number and buffer length are constant until sphereNumber=4096 so its only a change of "for loop bound" from 100 to 110 and compiler gives that error. I suspect it automatically unrolls loops and that makes bigger register pressure. This leads to higher private register needs again(and needs to be spilled again)
Here is how the kernel string is built by host:
kernelx+=
"float4 trace"+(depth+1)+"(float4 *rayorig, float4 *raydir,__global sphereObject *spheres, int sphereNum, int depth, int threadNo ){return (float4)(0,0,0,0);}";
for(int iteration=depth;iteration>=0;iteration--)
{
//here is the bloating action to make traceX(...) function semi-recursive.
kernelx+=
"float4 trace"+iteration+"(float4 *rayorig, float4 *raydir,__global sphereObject *spheres, int sphereNum, int depth, int threadNo )"
+ "{"
+ " int MAX_RAY_DEPTH="+depth+";"
+ "float facingratio =0; float fresneleffect=0;"
+ "float4 refldir=(float4)(0,0,0,0);"
+ "float4 arg00=(float4)(0,0,0,0); float4 reflection=(float4)(0,0,0,0);float4 refraction=(float4)(0,0,0,0);"
+ "float4 refrdir=(float4)(0,0,0,0); float4 arg01=(float4)(0,0,0,0); "
+ "float4 surfaceColor=(float4)(0,0,0,0);"
+ "float4 phit=(float4)(0,0,0,0); "
+ "float4 nhit=(float4)(0,0,0,0);"
+ "float bias=0.000f; "
+ "float4 traceTmpReturn=(float4)(0,0,0,0);"
+ "float tnear=100000000.0f; "
+ "sphereObject so;"
+ " initElem(&so); "
+ " for(int i=0;i<sphereNum;i++)"
+ " {"
+ " float t0=100000000.0f;float t1=100000000.0f;"
+ " if(intersect(rayorig[0],raydir[0],&t0,&t1,spheres[i].center,spheres[i].radius)==1)"
+ " {"
+ " if(t0<0){t0=t1;}"
+ " if(t0<tnear)"
+ " {"
+ " tnear=t0;"
//+ " so=&spheres[i];"
+ " copyElem(&so,spheres[i]);"
+ " }"
+ " }"
+ " }"
+ " "
+ " if(so.radius<-0.5f){ return (float4)(0,0,0,0);} "
+ ""
+ " surfaceColor=(float4)(0,0,0,0);"
+ " phit=rayorig[0]+raydir[0]*tnear;"
+ " nhit=phit-so.center;"
+ " nhit=normalize(nhit);"
+ " bias=0.001f;"
+ " bool inside=false;"
+ " if(dot3X(raydir[0],nhit)>0){nhit=-nhit; inside=true;}"
+ " if(((so.transparency > 0) || (so.reflection > 0))&& (depth<MAX_RAY_DEPTH) )"
+ " {"
+ " facingratio = -dot3X(raydir[0],nhit);"
+ " fresneleffect= mixx(pow(1.0f-facingratio,3),1.0f,0.1f);"
+ " refldir = raydir[0] - nhit*2.0f*dot3X(raydir[0],nhit);"
+ " refldir=normalize(refldir);"
+ " arg00=phit+nhit*bias;"
+ " reflection=trace"+(iteration+1)+"(&arg00,&refldir,spheres,sphereNum,depth+1,threadNo);"
+ " refraction=(float4)(0,0,0,0);"
+ " if(so.transparency>0)"
+ " {"
+ " float ior=1.1f; float eta=(inside)?ior:1/ior; "
+ " float cosi=-dot3X(nhit,raydir[0]);"
+ " float k=1.0f-eta*eta*(1.0f-cosi*cosi);"
+ " refrdir = raydir[0]*eta+nhit*(eta*cosi-sqrt(k));"
+ " refrdir=normalize(refrdir);"
+ " arg01=phit-nhit*bias;"
+ " refraction=trace"+(iteration+1)+"(&arg01,&refrdir, spheres,sphereNum, depth+1, threadNo);"
+ " "
+ " }"
+ " surfaceColor=(reflection*fresneleffect+refraction*(1.0f-fresneleffect)*so.transparency)*so.surfaceColor;"
+ ""
+ " }"
+ " else"
+ " {"
+ " for(int i=0;i<sphereNum;i++)"
+ " {"
+ " if(spheres[i].emissionColor.x>0)"
+ " {"
+ " float4 transmission=(float4)(1,1,1,1);"
+ " float4 lightDirection=spheres[i].center-phit;"
+ " lightDirection=normalize(lightDirection);"
+ " for(int j=0;j<sphereNum;j++)"
+ " {"
+ " if(i!=j)"
+ " {"
+ " float t0,t1;"
+ " float4 arg02=phit+nhit*bias;"
+ " if(intersect(arg02,lightDirection,&t0,&t1,spheres[j].center,spheres[j].radius)==1)"
+ " {"
+ " transmission= (float4)(0,0,0,0);break;"
+ " } "
+ " } "
+ " }"
+ " surfaceColor += so.surfaceColor*transmission*max(0.0f, dot3X(nhit,lightDirection))*spheres[i].emissionColor;"
+ " }"
+ " }"
+ " }"
+ ""
+ " return surfaceColor+so.emissionColor; "
+ "}";
}
kernelx+= "__kernel void rayTraceSphereRender(__global float4 *center, __global float *radius,"
+ " __global float4 *surfaceColor, __global float4 *emissionColor,"
+ " __global float *transparency,"
+ " __global float *reflection, __global float4 *image,"
+ " __global sphereObject *spheres)"+
"{"+
" int gid=get_global_id(0);" +
" int lid=get_local_id(0);"
+ " "
+ " {"
+ " int numSphr="+raytraceSphereNumber0+";"
+ " int width="+n+", height="+n+";"
+ " float invWidth = 1.0f/((float)width);"
+ " float invHeight= 1.0f/((float)height);"
+ " float fov = 30.0f; float aspectratio= ((float)width)/((float)height);"
+ " float angle=tan(3.141592653589793f*0.5f*fov/(180.0f));"
+ " int y=gid/"+n+";"
+ " "
+ " int x=gid%"+n+";"
+ " "
+ " float xx=(2.0f*((x-0.5f)*invWidth)-1.0f)*angle*aspectratio;"
+ " float yy=(1.0f-2.0f*((y-0.5f)*invHeight))*angle;"
+ " float4 raydir=(float4)(xx,yy,-1.0f,0.0f);"
+ " raydir=normalize(raydir);"
+ " float4 ref=(float4)(0,0,0,0);"
+ " float4 upp=(float4)(0,0,0,0);"
+ " upp=trace0(&ref,&raydir,spheres,numSphr,0,gid);"
+ " upp.w=1.0;"
+ " if(x>(width-width/"+sayi2+")) image[y+x*"+n+"]=upp;"
+ " "
+ " "
+ " "
+ " }"
+ " "
//
+"}";
Host: FX8150 windows7-64bit home premium, Java-64bit(Eclipse)
Device: HD7870 Catalyst 13.12
Code works fine for lower depth and sphere numbers for GPU; and no problem for CPU for whatever number of depth and sphere I set:
I am learning java, and from what I can tell, what I am looking to do is a rare situation.
I am trying to use an API (kindof) to randomly generate musical notes. I want it to generate 20 times so i have it in a for loop. I realize that i could have used a list for this I just dont know how I could have implemented it. The question I have is, when I try to compile this code, the first part runs. It lets me make the seed. However after that it gives me
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at co.kbcomp.music.Main.main(Main.java:64)
What could I do to prevent this? I know that what I am doing is wrong. That much I dont need to be told. What I want to know is where am I going wrong.
package co.kbcomp.music;
import java.util.*;
import org.jfugue.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Please enter a number for the seed of the song");
long seed = in.nextLong();
Calendar cal = Calendar.getInstance();
Random rand;
//rand = new Random(cal.getTime());
rand = new Random(seed);
int NoteNumber = 0;
int NoteLength = 0;
int OctiveNumber = 0;
int ChordNumber = 0;
int InversionNumber = 0;
//int Duration = rand.nextInt(100 - 5) + 5;
//This keeps track of the iteration of the for loop.
String[] NN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] NL = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] IN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] CN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
String[] ON = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "};
//This is what is being played
String[] note = { "A", "B", "C", "D", "E", "F", "G" };
String[] noteLength = {"", "w", "", "h", "", "q","", "i"};
String[] octive = { "","1","", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9"};
String[] chord = { "", "maj", "", "min"};
String[] inversion = {"", "^", "", "^^", "", "^^^", "", "^^^^", "", "^^^^^"};
String[] key = {"",""};
String keys= " ";
String randstr = " ";
//this is the loop that defines the music legnth
for (int i = 0; i < 21; i++) {
NoteNumber = rand.nextInt(7);
NoteLength = rand.nextInt(8);
OctiveNumber = rand.nextInt(18);
ChordNumber = rand.nextInt(4);
InversionNumber = rand.nextInt(10);
NN[i] = note[NoteNumber]; // This randomly generates the note to be played.
NL[i] = noteLength[NoteLength]; // This randomly generates the length of the note.
ON[i] = octive[OctiveNumber]; // This defines the octive to be played in.
CN[i] = chord[ChordNumber]; // This is defines the major or the minor
IN[i] = inversion[InversionNumber]; // IN[i] = inversion[InversionNumber];
key[i] = NN[i] + NL[i] + ON[i] + CN[i] + IN[i];
//randstr = c[0] + " " + c[1] + " " + c[2] + " " + c[3] + " " + c[4] + " " + c[5] + " " + c[6] + " " + c[7] + " " + c[8] + " " + c[9] + " " + c[10] + " " + c[11] + " " + c[12] + " " + c[13] + " " + c[14] + " " + c[15] + " " + c[16];
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);
}
System.out.println(key);
Player player = new Player();
Pattern pattern = new Pattern(key[0]);
player.play(pattern);
}
}
You declare your key array with a length of two:
String[] key = {"",""};
But then later in your for loop, you try to access elements beyond the length of your array:
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] +
" " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] +
" " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " +
key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] +
" " + key[19] + " " + key[20]);
Since your array has only a length of two, when you try to access the third element (at array index 2), you get an ArrayIndexOutOfBoundsException.
String[] key = {"",""};
...
for (int i = 0; i < 21; i++) {
...
key[i] = ....
Do you see the problem?
As you array in only 20 in length
then this
for (int i = 0; i < 21; i++) {
is going to cause an overflow
It should be < 20
Plus this code is meaningless as your key is only an array of 2
keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]);
I have a method getstaffinfo, which has 3 parameter (var_1, connection, filewriter fw), the var_1 value is read from a text file. So the method will be called as many times based on all the var_1 value passed from text file . approx ( 15000)
public static String getstaffid(String var_1, Connection connection,
FileWriter fw) throws SQLException, Exception
// Create a statement
{
String record = null;
ResultSet rs = null;
Statement stmt = connection.createStatement();
boolean empty = true;
try {
rs = stmt
.executeQuery("select username, firstname, lastname, middlename, street, city, stateorprovince, ziporpostalcode, countryorregion, fax, phone, extension, mobile, pager, title, primaryemail, secondaryemail, officename, description, comments, suspendeddate, userdata, employeeid, createuser, updateuser, createdate, updatedate, employeetype, servicedeskticketnumber, startdate, enddate, manager, businessapprover, technicalapprover, delegate, location, jobcodes, customproperty1, customproperty2, customproperty3, customproperty4, customproperty5, customproperty6, customproperty7, customproperty8, customproperty9, customproperty10 from globalusers where username = '"+ var_1 + "'");
ResultSetMetaData metaData = rs.getMetaData();
int columns = metaData.getColumnCount();
ArrayList<String> records = new ArrayList<String>();
while (rs.next()) {
empty = false;
//record = rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " " + rs.getString(5) + " " + rs.getString(6) + " " + rs.getString(7) + " " + rs.getString(8) + " " + rs.getString(9) + " " + rs.getString(10) + " " + rs.getString(11) + " " + rs.getString(12) + " " + rs.getString(13) + " " + rs.getString(14) + " " + rs.getString(15) + " " + rs.getString(16) + " " + rs.getString(17) + " " + rs.getString(18) + " " + rs.getString(19) + " " + rs.getString(20) + " " + rs.getString(21) + " " + rs.getString(22) + " " + rs.getString(23) + " " + rs.getString(24) + " " + rs.getString(25) + " " + rs.getString(26) + " " + rs.getString(27) + " " + rs.getString(28) + " " + rs.getString(29) + " " + rs.getString(30) + " " + rs.getString(31) + " " + rs.getString(32) + " " + rs.getString(33) + " " + rs.getString(34) + " " + rs.getString(35) + " " + rs.getString(36) + " " + rs.getString(37) + " " + rs.getString(38) + " " + rs.getString(39) + " " + rs.getString(40) + " " + rs.getString(41) + " " + rs.getString(42) + " " + rs.getString(43) + " " + rs.getString(44) + " " + rs.getString(45) + " " + rs.getString(46) + " " + rs.getString(47);
for (int i = 1; i <= columns; i++) {
String value = rs.getString(i);
records.add(value);
}
for (int j = 0; j < records.size(); j++) {
record = records.get(j) + ",";
}
fw.append(record);
}
/*fw.append(rs.getString(1));
fw.append(',');
fw.append(rs.getString(2));
fw.append(',');
fw.append(rs.getString(3));
fw.append('\n'); */
} finally {
fw.flush();
rs.close();
stmt.close();
}
return record;
}
As you can see, am executing a query for 47 values, which could be null or it can have some value.
Then i iterate through this 47 column, take the value and store it to an array list. Then i iterate the array list and write all the values to the string record with comma seperated value. Which is written to a csv file.
But it does not work fine. Any inputs would be appreciated...
You may have already solved the problem. Just let you know that I tried to use your code just now and found the issue was here:
record = records.get(j) + ",";
You should use something like this:
record = record + records.get(j) + ",";
Also change String to StringBuffer will improve the performance.
You didn't write the exact problem you face, but there is one for sure: you never write a line break into the file, so all data gets in one line.
while (rs.next()) {
... // your code, with the for loops
fw.append(record); //writing out the line, from your code
fw.append("\r\n"); //line break -- add this line
} //this is the end of the "while(rs.next())" loop
...