...

View Full Version : file-handling javascript, printing sections of a file



kitty
12-06-2010, 06:53 PM
I can get this code to take two separate sections of a file which are not beside each other and write them into another file. It always comes up as a single full line of the code instead of the sections I want. The code includes the student number first name last name and three results of assignments. I want the code to write the student number and three results of all the students into a file and then work out the average of the student results. Can you help? :)


try{
while (in.hasNextLine())
{
String line = in.nextLine();
out.println( line);
int i=0;
if(!Character.isDigit(line.charAt(i))) { i++; }
studentStringNumber = line.substring(0, i);
String stringResult = line.substring(i);

studentStringNumber = studentStringNumber.trim();
stringResults = stringResults.trim();
double stringResultsValue = Double.parseDouble(stringResults.trim());
stringResults = in.nextLine();


studentStringNumber = in.nextLine();
studentNumber = Integer.parseInt(studentStringNumber);
if(in.hasNextInt())
{
int value = in.nextInt();
}

results = Double.parseDouble(stringResults);
if(in.hasNextDouble())
{
double value = in.nextDouble();
}


Scanner lineScanner = new Scanner(line);
studentStringNumber = lineScanner.next();
while(!lineScanner.hasNextDouble())
{
studentStringNumber = studentStringNumber+ " " +lineScanner.next();
}
stringResultsValue = lineScanner.nextDouble();
}
}

Philip M
12-06-2010, 07:11 PM
You seem to be confusing Javascript with some other language (Java???).

String stringResult = line.substring(i);
double stringResultsValue = Double.parseDouble(stringResults.trim());
studentNumber = Integer.parseInt(studentStringNumber);
double value = in.nextDouble();


- none of those (for example) are valid Javascript syntax.

JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server or access a database.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

kitty
12-06-2010, 07:14 PM
You seem to be confusing Javascript with some other language.

String stringResult = line.substring(i);
double stringResultsValue = Double.parseDouble(stringResults.trim());
studentNumber = Integer.parseInt(studentStringNumber);
double value = in.nextDouble();


- none of those (for example) are valid Javascript syntax.

JavaScript is purely a client-side language, and unless used with an Ajax call has no capability to read from or write to a file (except a cookie), communicate with the server or access a database.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.


I'm very sorry but i have got mixed up. This is acctually just Java codes not javascript. I'm all very new to this and i didn't know that there was a difference.

I would be delighted if you could still help me or redirect me to another part of the forum that deals with java...

kitty
12-06-2010, 08:46 PM
Please delete this as i have moved my question to the appropriate thread.

Thank you.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum