Home > OS >  How to read intergers from file?
How to read intergers from file?

Time:07-27

I want to write a program that reads numbers from both.txt file and write to even.txt if its even and to odd.txt if its odd numbers in java. i need help

i have managed to create these files

I have tried the following but its not working:

public static void main(String[] args) throws FileNotFoundException, IOException{
  File both = new File("/home/eddie/Desktop/both.txt"); 
  File odd = new File("/home/eddie/Desktop/odd.txt");      
  File even = new File("/home/eddie/Desktop/even.txt");
  
  Scanner myObj = new Scanner(System.in);
   int number,remainder;
    System.out.println("Please enter a number!");
        number = myObj.nextInt();
    
     
    try (PrintWriter pwboth = new PrintWriter(both)) {
        pwboth.println(number);
        pwboth.close(); 
    }
    remainder = number/2;
    
    if (remainder != 0)
    {
        
                
        
            try (PrintWriter pwodd = new PrintWriter(odd)) {
                pwodd.println(number);
                pwodd.close();
            }
    } 
    else if(remainder > 0)
{
      try (PrintWriter pweven = new PrintWriter(even)) {
           pweven.println(number);
           pweven.close();
        }
}
}

This code is only printing to odd.txt and both.txt even if its even number and i want the program to read numbers from the both file i just don't know how to go about it.

CodePudding user response:

Note : The operator modulo (%) is used to get remainder value after division for Example: X % Y = Z means if X is divided by Y then it gave Z as remainder

So the definition of ODD and EVEN

  • number % 2 != 0 for ODD
  • number % 2 == 0 for EVEN

Here is correct code as you have posted having expected result:

public static void main(String[] args) {

        //Read & Write from and To the file

        File file = new File("source.txt");
        File even = new File("even.txt");
        File odd = new File("odd.txt");

        int number = 0;

        try (BufferedReader reader= new BufferedReader(new FileReader(file));
             PrintWriter evenWriter = new PrintWriter(even);
             PrintWriter oddWriter = new PrintWriter(odd);){

            String text = null;

            while ((text = reader.readLine()) != null) {
                number = Integer.parseInt(text) ;

                if(number % 2 == 0) {
                    evenWriter.println(number);
                }else{
                    oddWriter.println(number);
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

source.txt

12
23
34
45
56
67
78
89
90

even.txt

12
34
56
78
90

odd.txt

23
45
67
89

CodePudding user response:

If you want remainder you need to use modulo, not division.

remainder = number%2;

Also:

else if(remainder > 0)

remainder can only be 0 or 1, so this condition makes little sense.

Your try blocks are not correct - here is a nice article about how to do it correctly. https://www.baeldung.com/java-try-with-resources

CodePudding user response:

To know if a number is odd or even, you have to know if it is divisible by 2 or not.

if (number % 2 == 0) {
  // It's an even number, write it to even.txt
} else {
  // It's an odd number, write it to odd.txt
}

number % 2 == 0 means that the number is divisible by 2 and leaves no remainder behind when divided by 2.

number %2 != 0 means that the number is NOT divisible by 2 and leaves a remainder behind when divided by 2.

CodePudding user response:

Here is another solution with some more generic readability. Assuming that the input file contains numbers separated with space or new line

public static void filterOddEvenNumberFromFile() {

        File input = new File("input.txt");
        File evenOutput = new File("evenOutput.txt");
        File oddOutput = new File("oddOutput.txt");
       
        List<Integer> evenList = new ArrayList<>();
        List<Integer> oddList = new ArrayList<>();

       try (BufferedReader reader = new BufferedReader(new FileReader(input));
            PrintWriter evenOutputWriter = new PrintWriter(evenOutput);
            PrintWriter oddOutputWriter = new PrintWriter(oddOutput);
           ) {

           String text = null;
           String [] numbers;
           while ((text = reader.readLine()) != null) {
               numbers = text.split(" ");
               for(String n:numbers){
                   if(Integer.parseInt(n) % 2 == 0){
                       evenList.add(Integer.parseInt(n));
                   }else{
                       oddList.add(Integer.parseInt(n));
                   }
               }
               for(Integer even : evenList) {
                   evenOutputWriter.print(even   " ");
               }
               for(Integer odd:oddList) {
                   oddOutputWriter.print(odd   " ");
               }
               evenList.clear(); oddList.clear();
           }
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
    }

input.txt

12 23 34 45 56
67 78 89 90
98 76 54
32 10
90

evenOutput.txt

12 34 56 78 90 98 76 54 32 10 90 

oddOutput.txt

23 45 67 89 
  • Related