1

Contents of file patch.txt:

1
2
3
4
5

My code:

int sum = 0;
Scanner s = new Scanner (new File("patch.txt"));
while (s.hasNextLine()){
            String [] str = s.nextLine().split("/r");
            for (int i=0; i<str.length; i++){
            sum+=Integer.parseInt(str[i]);
            }
            System.out.print(sum); //the result is 15
        }
        s.close();
    }
}

When I sum the data must be 15, but why do I always get an error?

5
  • why are you using split? " String [] str = s.nextLine().split("/r");" ? Commented Nov 12, 2014 at 6:25
  • because my data just one column and five rows... Commented Nov 12, 2014 at 6:26
  • s.nextLine gives you string representation of data. you can parse it without using split. You could use s.nextInt() to read ints. if you know your input is ints Commented Nov 12, 2014 at 6:29
  • @rickyhitman10 What means "but why always error"? Wich error? Commented Nov 12, 2014 at 6:31
  • if not use split it always error Commented Nov 12, 2014 at 6:35

4 Answers 4

2

Solution using for loop and array:

String[] str = new String[10];
for(int i = 0; s.hasNextLine(); i++) {
    str[i] = s.nextLine();
    sum += Integer.parseInt(str[i]);
}
System.out.print(sum);

But for this problem given below is a better solution:

 while (s.hasNextLine()) {
        String str = s.nextLine();
        sum += Integer.parseInt(str);
 }
 System.out.print(sum);
Sign up to request clarification or add additional context in comments.

3 Comments

i want to using for not while loops... :)
@rickyhitman10 This problem doesnot require a for loop if you need a for loop you can add for(;s.hasNextLine();) instead of while(s.hasNextLine()). But while loop is better
@rickyhitman10 i have edited and made the solution using for loop and array
1

You don't have to do String [] str = s.nextLine().split("/r"), it is not needed. You were also printing the sum from within the while loop, but it should be outside the while loop. Try this:

while (s.hasNextLine()){
    String str = s.nextLine();          
    sum+=Integer.parseInt(str);         
}
System.out.print(sum); //the result is 15
s.close();

1 Comment

@rickyhitman10 Then you could replace the while loop with for ( ; s.hasNextLine(); ) { instead.
1

Try This : Use a try catchfor Scanner otherwise it shows the error unreported exception java.io.FileNotFoundException;

        import java.io.*;
        import  java.util.*;

        class Entry1 
        {
        static  Scanner s ;
        public static void main( String[] args )
        {
         int sum = 0;
          try
           {
             s = new Scanner (new File("patch.txt"));
           }
          catch(Exception e)
          {
            System.out.print(e);
          }
          while (s.hasNextLine())
          {      
            String [] str = s.nextLine().split("/r");
            for (int i=0; i<str.length; i++){
            sum+=Integer.parseInt(str[i]);
          }   
         }
        System.out.print(sum);
        s.close();
       }
      }

1 Comment

copy the code and run it. it's working fine on my system
0
int sum = 0;
Scanner s = new Scanner (new File("patch.txt"));
while (s.hasNextLine()){
    String [] str = s.nextLine().split("\r");
    for (int i = 0; i < str.length; i++){
        if(!str[i].trim().isEmpty){
            sum+=Integer.parseInt(str[i]);
        }
    }
}
s.close();
System.out.print(sum); //the result is 15

Try this.

Edited above code.

1 Comment

@rickyhitman10, does this work? Please let me as soon as possible. Because I will be leaving to home, and may not be available for helping you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.