Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    file-handling javascript, printing sections of a file

    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?

    Code:
     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();     
                   }
                }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    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...

  • #4
    New Coder
    Join Date
    Dec 2010
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Please delete this as i have moved my question to the appropriate thread.

    Thank you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •