• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Reading ints from a text file

 
Ranch Hand
Posts: 90
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my code so far:



I have a class for numbers created but im not sure i really need it

i have a text file that i have to read ints from that looks like this:

1 6 8 7
2 5 8 9 9
3 4
2 3 9 9 8 8 7
10 10 10
3

I need the output to look like this:
The following is the distribution of values:
1 - 2
2 - 2
3 - 3
4 - 1
5 - 1
6 - 1
7 - 2
8 - 4
9 - 4
10 - 3
The average value was 6.17

So after i read it from the text file and assinging it tho the array which i think i have it right how do i get it to know it has three ones four sixs two tens etc.

if i have any errors in my code please let me know



 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Stallard wrote:if i have any errors in my code please let me know



  • Does your code compile ?
  • Does it execute fine ?
  • Does it give the output you expect ?
  •  
    salvin francis
    Bartender
    Posts: 2911
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If it does not compile, do let us know the cause of failure, we could help you with that.
    If it does not execute fine, do let us know what is the error thrown during execution
     
    Saloon Keeper
    Posts: 11030
    88
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I could suggest some ways to fix your code, however I think a different approach would clean things up considerably. You are reading lines and then tokenizing (parsing) the line. Your file suggest that if you could just read each int, ignoring the  new-lines, it would be simpler. Scanner is the perfect way to do this. Scanner by default separates the input by white space, including new-lines. You can then just use hasNextInt() and nextInt() to retrieve each number.
     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    okay how would i use the scanner

    and here is an algorithm i was given

    // create an array of int size 11 (ignoring the zeroth location like we did in Dice)

    // copy and paste the code needed to ope n a text file

    // read in the first line

    // outer while - while there are more lines

        // set up your tokenizer  (here)

       // inner while -while there are more tokens in this current line

              // read in the next token and convert it to an integer

              // increment the correct location in your array

      // end of inner while

     // read in the next line

    // end of outer while

    and i have re done the code well doing it a little different the reading in the file part im struggling with



    i think i got up until i hit the here marked step any helped would be appreciated
     
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why do you expect the program to output that there are two 1s when there is only one in the input?
     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    that was just an example not meant to be taking as actual output.
     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    sorry i left a one out i know what you are talking about now junilu lacar those three 10s at the end should have a one in there sorry for the typo
     
    Carey Brown
    Saloon Keeper
    Posts: 11030
    88
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Daniel Stallard wrote:okay how would i use the scanner

    and here is an algorithm i was given....

    i think i got up until i hit the here marked step any helped would be appreciated


    Ok, so if you've got the algorithm perhaps you should stick with it. The first thing to fix is that you are only getting the token for the first int in the line. You need a loop around the code that includes t.nextToken().
     
    Sheriff
    Posts: 7126
    185
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    In the above line, the comment doesn't match the code.

    Note: do you have to use ArrayList?  Can you use a Map?
     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have actually redone alot that line of code is gone sorry i just havent updated it. Im not use to using the StringTokenizer.It throws me off alot.
     
    Carey Brown
    Saloon Keeper
    Posts: 11030
    88
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    If you wanted to go with a Scanner instead of readLine/tokenize:
     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i am meant to use the StringTokenizer

     
    Carey Brown
    Saloon Keeper
    Posts: 11030
    88
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When you try to keep track of how many times an object appears, that is called a histogram. Histograms are typically implemented using a Map. In your case you want the output in sorted order so using a TreeMap would be ideal. Maps require a "key" and a "value" data type. In your case the  number you scan in is the key so its type would be an Integer. Your value would be the count, which would also be an Integer.
     
    Carey Brown
    Saloon Keeper
    Posts: 11030
    88
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Comments about your latest code:

    while(t!=null){ // i think this is right
    Is not correct.

    You are assuming the numbers are between 1 and 10 (inclusive), what if it's 42?
    Using a Map would remove any range restriction, it could even be -42.

    Editi: Doesn't look like your code compiles.
     
    Knute Snortum
    Sheriff
    Posts: 7126
    185
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Please post code that compiles.  Can you see what's wrong here?
     
    Carey Brown
    Saloon Keeper
    Posts: 11030
    88
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Knute Snortum wrote:Please post code that compiles.  Can you see what's wrong here?


    To utilize an array value at a specific index use square brackets ([...]).
     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yes i made a mistake on the braces and i fixed that an yes i don't need a for loop cause that is like saying i know how many numbers are in the file when i don't. Thanks for helping me see that. All i need is the outer while loop and the inner while loop but if im not using
    for the second loop what then?

    what would i use I know that this:

    is bringing in the entire line from the text file then
    my first while is saying while the line is not empty i think this says read until the end of the line or is this counting each individual number
    then this gets the individual number which then need to be compared to the if else statements to determine where they need to be placed in the array

     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    okay i debuged it is reading the numbers adding it to the array its just not going to the next line
     
    Junilu Lacar
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This entire construct can be reduced to a single line of code. Look at the index you're using for the nums array. Now look at the value of num.  Can you see a consistent relationship in all the cases? The whole point of using an array and using variables is so that you don't have to write code like this.  What if you had a thousand elements in your array? Does that mean you'd write a thousand if-else cases? No, you'd still write one single line of code using a variable to get the correct index into the array. And no, you don't need a loop to do this.

     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    but im not sure what that one line of code is im new to this stuff.



     
    Junilu Lacar
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The idea is the same as when you are printing out the contents of the array using a loop and an index variable. Do you know how to write a loop like that?  This is the same idea, except you don't need a loop.

    If you have 10 cubby holes labeled like so:
    [1][2][3][4][5][6][7][8][9][10]
    ----------

    And I gave you piece of paper with a number on it and told you "Put this paper in the corresponding cubby hole," what would you do if the paper had 5 written on it? What would you do if the paper had 3 written on it? 10?  9?  1?

    One side of the paper would be written "num",  on the other side would be written the actual number of the cubby hole you should put the piece of paper in. See what I mean? That's one line of code.  

    Important Note: Java uses zero-based indices for arrays. That is, the first element of an array has an index of 0, the second an index of 1, and so on. This is what a 10-element Java array looks like:

    [0][1][2][3][4][5][6][7][8][9]
    ----------

     
    Junilu Lacar
    Sheriff
    Posts: 17734
    302
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Maybe you're thinking that an array needs to be address by a literal number, like this:

    It doesn't. The value in the square brackets can be anything that evaluates to an integer.  So, if you have an int variable i, and i was assigned the value of 3, then this code:
     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i see what you are saying now. now im having trouble with the math part java make it 6.00 when  i need it to be 6.17
     
    Daniel Stallard
    Ranch Hand
    Posts: 90
    Java ME MySQL Database PHP
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    i got it sorry ive been staring at code to long and today has not been the best days to focus
     
    Carey Brown
    Saloon Keeper
    Posts: 11030
    88
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I don't quite follow your response. The "math" should be strictly integer, not floating point, so I don't see where 6.17 comes into play.
    Can you show us the code that you are attempting?

    Edit: Sorry, I forgot you had to compute the average at the end. In that case you want to force it to do floating point. Something like:
    avg = (double)sum / qty;
     
    reply
      Bookmark Topic Watch Topic
    • New Topic