2016-04-11 16 views
0

Bir txt dosyasından geçecek ve hasNextInto yöntemini kullanarak her bir sayı satırını toplayacak bir program yazmam gerekiyor. Dosyayı okumak için bir while döngüsü kullanmalı ve her satırın toplamı ve her bir toplamı elde etmek için bir for döngüsü kullanmalıyım. Örn: Satır 1: 10 Satır 2: 15 Satır 3: 30. İşte sahip olduğum şey, ama buradan nereye başlanacağını bilmiyorum.Dosya G/Ç'si, her bir satır sayısını hasNextInt yöntemiyle toplam

import java.util.*; 
import java.io.*; 
public class Lab9_Problem1 
{ 
    public static void main(String [] args) 
    { 
    int iVal; 
    int iRow; 
    int iRowSum; 
    int iToken; 

    iRowSum = 0; 
    iRow = 0; 
    try 
    { 
     Scanner ifsInput = new Scanner(new File("C:/Users/rconno7/Documents/COSC 236/9_1_Input.txt")); 
     while(ifsInput.hasNextInt()) 
     { 
     iVal = ifsInput.nextInt(); 

     } 


    } 

    catch (FileNotFoundException sMsg) 
    { 
     System.out.println("The file cannot be found or opened."); 
    } 


    } 
} 

cevap

0

nihayet off gitmek için bir şey gerekebilir olanlar ilgilenen veya işlemi için gerekli gücü üreten bir kod var.

import java.util.*; 
    import java.io.*; 
    public class Lab9_Problem1 
    { 
     public static void main(String [] args) 
     { 
     int iVal; 
     int iRow; 
     int iRowSum; 
     int iToken; 


     iRowSum = 0; 
     iRow = 1; 
     try 
     { 
      Scanner ifsInput = new Scanner(new File("C:/Users/rconno7/Documents/COSC 236/9_1_Input.txt")); 
      while(ifsInput.hasNextLine()) 
      { 

     while(ifsInput.hasNextInt()) 
     { 
      for(iToken = 0; iToken <= 3; iToken++) 
      { 
      iVal = ifsInput.nextInt(); 
      iRowSum = iRowSum + iVal; 
      } 
      System.out.println("Row " + iRow + " sum: " + iRowSum); 
      iRowSum = 0; 
      iRow++; 
     } 

      } 


     } 

     catch (FileNotFoundException sMsg) 
     { 
      System.out.println("The file cannot be found or opened."); 
     } 


     } 
    } 
İlgili konular