Results 1 to 6 of 6
  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Read a series of strings (name) and integers (score) from a text file...

    I'm supposed to write a static method named numberStatistics that accepts a string of text as a parameter, and use a text file...
    I'm winging it, and of course I'm getting syntax errors on that bufferedWriter part, and of course it won't read it and say what the numbers are:
    Java Code:
    import java.io.*; 
    import java.util.*; 
    public class BS { 
    public static void main(String[] args) throws FileNotFoundException { 
    Scanner in = new Scanner(new File("score.txt")); 
    evenNumbers(in); 
    //TEXT FILE
            String fileName = "score.txt";
    
      try {
                FileWriter fileWriter =
                    new FileWriter(fileName);
    
                BufferedWriter bufferedWriter =
                    new BufferedWriter(fileWriter);
    
                bufferedWriter.write("Helene 5 Jordan 7 Eve 2 Ken 8 Andrew 9");
                bufferedWriter.write(David 10 Amy 12 Sean 98 Patrick 7");
                bufferedWriter.write(Joy 14 Sue 20 Jimmy 22");
                bufferedWriter.close();
            }
            catch(IOException ex) {
                System.out.println(
                    "Error writing to file '"
                    + fileName + "'");
            }
        }
    //END TEXT FILE
    public static void evenNumbers(Scanner in) { 
    String lineOfDigits = in.nextLine(); 
    Scanner lineScan = new Scanner(lineOfDigits);  
    int numNums = 0; 
    int sum = 0; 
    while (lineScan.hasNextInt()) { 
    int number = lineScan.nextInt(); 
    numNums++; 
    sum += number; 
    } 
    System.out.println("Total numbers = " + numNums); 
    System.out.println("Sum of numbers = " + sum); 
    } 
    }
    I'm supposed to find the minimum and maximum of the numbers too, but of course that's not working either. :)

    Why can't DrJava just locate the stuff or not want the code to be so verbatim. It seems words and numbers. Omg.
    Last edited by manibby93; 10-07-2013 at 02:26 AM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,598
    Rep Power
    5

    Default Re: Read a series of strings (name) and integers (score) from a text file...

    I told you how to approach this in your other thread. --> Tokenizing? Help writing a static method that accepts a string of text as a parameter

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Read a series of strings (name) and integers (score) from a text file...

    This is about reading the text though, a whole different topic, regarding that bufferedWriter stuff I threw in there. I didn't understand how you said it, and no one else was responding.
    Last edited by manibby93; 10-07-2013 at 03:44 AM.

  4. #4
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Read a series of strings (name) and integers (score) from a text file...

    IS THERE A WAY TO DELETE THIS POST SO THAT I CAN ASK A NEW QUESTION BECAUSE MY NEW QUESTION HAS NOTHING TO DO WITH THIS POST EXCEPT FOR IT'S THE SAME ASSIGNMENT. THIS IS FRUSTRATING. ONLY ONE PERSON IS RESPONDING, SO HOW ARE NEW PEOPLE GOING TO FIND THIS POST IF IT'S OLD & UNDER THE WRONG TOPIC???

    My question is how can I make an array not print? I need to show how the text file is set up w/ the array, but if I take out the first println statement, it says the local variables name and score can't be located or something. Like I said, I'm new to Java, so don't insult me. I was trying to put the bottom println statements into the BufferedReader one, but of course that doesn't work. I need to calculate the total numbers, sum, etc (everything listed in the bottom println statements). Help???

    The text file looks like this:

    Helene 5
    Jordan 7
    Eve 2
    Ken 8
    Andrew 9
    David 10
    Amy 12
    Sean 98
    Patrick 7
    Joy 14
    Sue 20
    Jimmy 22

    This is the code I'm referring to:

    Java Code:
    import java.io.*; 
    import java.util.*; 
    public class numberStatistics { 
    public static void main(String[] args) throws FileNotFoundException { 
    Scanner in = new Scanner(new File("score.txt")); 
    evenNumbers(in);
    //READING TEXT FILE
    {
        String fileName = "score.txt";
    BufferedReader br = null;
    String line = "";
    String txtSplitBy = " ";
    
        try {
    
        br = new BufferedReader(new FileReader(fileName));
        while ((line = br.readLine()) != null) {
    String[] splitLine = line.split(txtSplitBy);
    String name = splitLine[0];  //first element of array
    String score = splitLine[1]; //second element of array
    
            System.out.println("name: " + name + "\n" + 
                                "score: " + score + "\n") ;
        }
        } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    }
    }
    //END READING TEXT FILE
    public static void evenNumbers(Scanner in) { 
    String lineOfDigits = in.nextLine(); 
    Scanner lineScan = new Scanner(lineOfDigits);  
    int numNums = 0; 
    int sum = 0; 
    int min = 0;
    int max = 0;
    
    while (lineScan.hasNextInt()) { 
    int number = lineScan.nextInt(); 
    numNums++; 
    sum += number; 
    } 
    System.out.println("Total numbers = " + numNums); 
    System.out.println("Sum of numbers = " + sum); 
    System.out.println("Average = " + (double)(sum)/numNums); 
    System.out.println("Minimum = " + min);
    System.out.println("Maximum = " + max);
    } 
    }
    Last edited by manibby93; 10-07-2013 at 05:59 AM.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Read a series of strings (name) and integers (score) from a text file...

    You post is somewhat confusing (as well as your code) and your requirements are a bit vague.

    How can I make an array not print? Simple! Don't have a print statement to print it. BTW you shouldn't print an array but rather use a loop to print elements in the array.

    If you have trouble with the name and score variables then that is because you have declared them inside the while loop so that is the only scope where you can access them. Once the loop exits you can no longer access those variables. Maybe declare them before the loop.

  6. #6
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Read a series of strings (name) and integers (score) from a text file...

    It may be confusing because I am "NEW TO JAVA." I don't understand... If I knew all there is to know, I wouldn't be here. This forum IS for helping people, too, right?? & yeah, I don't know where else to put the "name...score" part though to show the pattern. I only learned about "println" & "printf." I posted the prompt in the other thread. Do I really have to put a disclaimer: IS STILL LEARNING JAVA in every post?? I thought the "New to Java" category spoke for itself... To make these programs, you HAVE to have referenced something else first. You can't pull it out the sky...

    CAN SOMEONE ANSWER MY "DELETE THIS POST/THREAD" PART THOUGH? It's a question.

Similar Threads

  1. Replies: 2
    Last Post: 04-27-2011, 04:09 PM
  2. Read text file and compare strings
    By africanhacker in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 12:53 AM
  3. Problem writing multiple strings to a text file
    By Yogesh_P in forum New To Java
    Replies: 4
    Last Post: 03-30-2011, 12:58 AM
  4. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 05:45 PM

Posting Permissions

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