Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Write method given a character file [JAVA]

  1. #1
    Junior Member
    Join Date
    Sep 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Write method given a character file [JAVA]

    Hello everyone, I have to write a method that given the name of a character file containing integers, one for each line, checks if the number of positive values in the file exceeds that of negative values.
    This is what I managed to do. The problem I encounter is that it always tells me that positive values are greater than negative ones even when negative values are greater in the.txt file ... Could you help me? thank you
    public static String valori(String nomeFile){
     
       int accNeg=0;
       int accPos=0;
        try (FileInputStream bin = new FileInputStream(nomeFile);
                BufferedInputStream bis = new BufferedInputStream(bin);
                DataInputStream lettore = new DataInputStream(bis)) {
            while (lettore.available() != 0) {
                int parziale = lettore.readInt();
                if (parziale > 0) {
                    accPos++; 
                }
                    else if(parziale<0){                            
                            accNeg++;
              }
            }  
        } catch (FileNotFoundException e) {
            System.out.println("File " + nomeFile + " non trovato");
        } catch (IOException e) {
            System.out.println("Errore: " + e.getMessage());
        }
        if(accPos>accNeg){
        return "Valori positivi maggiori di quelli negativi";
        }
        else return "Valori positivi minori o ugali di quelli negativi";
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,163
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Write method given a character file [JAVA]

    Have you printed out the values in accPos and accNeg to see what they are? What message is printed if they have the same value?

    How was the input file created? The DataInputStream class's readInt() method does not read character data and convert it to an int like the Scanner class's nextInt method does.
    Read the API doc for the readInt() method to see what it does.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading text file character by character into a 2d char[][] array
    By filipasa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 18th, 2019, 07:09 PM
  2. Replies: 15
    Last Post: May 2nd, 2013, 05:29 AM
  3. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  4. [SOLVED] Why can't I write to file inside a doGet() method?
    By FailMouse in forum Java Servlet
    Replies: 1
    Last Post: July 7th, 2010, 01:15 AM