Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Only getting a 7  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I'm writing this code for my class (just a project for practice, isn't for a grade). Essentially what I have to do is take a file supplied by the teacher, read it in, store the strings in an ArrayList, and store the integers into an array. I have mostly everything working, except for some reason, I'm only getting the number 7. to show you the number code:


its giving me the output of: 7, 7, 7, 7, 7, 7 but i should be getting: 12, 14, 16, 31, 42, 7
so i'm not sure whats wrong with it, but i apologize in advanced for any mistakes i made formatting wise as it's my first time posting.
 
Saloon Keeper
Posts: 2329
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Larry Gobson wrote: I have to do is take a file supplied by the teacher, read it in, store the strings in an ArrayList


I don't see an ArrayList in the code you posted.


In this block of code, each time you read a string value from the file, you are setting every element in the array based on the string value read, so it would make sense that the last value you read gets populated in each array element.

What does this block of code do?

Also, rather than this:it is simpler to do this:
 
Ron McLeod
Saloon Keeper
Posts: 2329
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch!  
 
Master Rancher
Posts: 1043
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do something like:


I'm not sure if it's fully correct, but it's close , damn ide's make you ignorant

By the way... is the same as
 
Ron McLeod
Saloon Keeper
Posts: 2329
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see the point is concatenating all the String values together, just to split them apart right after.

Why not just add the String values directly to a List each time they are read from the file?
 
Daniel Demesmaecker
Master Rancher
Posts: 1043
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:
Why not just add the String values directly to a List each time they are read from the file?



The whole point was to split the text and numbers in an arraylist  and an array. Then you wil still have to check your list for nr's remove them an put them in a array
 
Daniel Demesmaecker
Master Rancher
Posts: 1043
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also try to parse your string in your loop to an int, if it fails you know it's a string...

 
Ron McLeod
Saloon Keeper
Posts: 2329
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:The whole point was to split the text and numbers in an arraylist  and an array. Then you wil still have to check your list for nr's remove them an put them in a array


I guess it depends on what you understand the requirements are and how closely you want to follow them.  My understanding is:
    1. Read the values from the file and store in an ArrayList
    2. Using the ArrayList, store the integer values in an array

Since we haven't seen the contents of the file, we don't know if there is any splitting required or if it contains any non-digit characters (other than NL and/or CR).  Assuming that there is one number per line (I could be wrong), I would do something like this (ignoring exceptions):
 
Ron McLeod
Saloon Keeper
Posts: 2329
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:


I'm not sure if you intended the code to be a working example, but there are issues - please take my comments as constructive feedback:
  - The array is never initialized.
  - The array is type String, but you are trying to assign int values to it.
  - The List interface does not have an append method.
 
Daniel Demesmaecker
Master Rancher
Posts: 1043
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:
I'm not sure if you intended the code to be a working example, but there are issues - please take my comments as constructive feedback:



It wasn't just illustrating what I ment. The append and the array were typo's.
The biggest issue would actually be that just like your example the code assume when there's a nr it's the only value on the line.
And yes, you could take liberties with the interpretation of the question, or you could write something that takes care of every scenario.
 
Ron McLeod
Saloon Keeper
Posts: 2329
293
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:The biggest issue would actually be that just like your example the code assume when there's a nr it's the only value on the line.


True - I based my assumptions on the OP's code - I didn't see anything that made me think that there might be multiple numbers on a line, or the need to remove any non-digit characters.

We won't really know unless the OP shares the contents of the file.


Daniel Demesmaecker wrote:And yes, you could take liberties with the interpretation of the question


I don't think I took liberties.  I saw the requirements as vague, and stated my assumptions along with the solution.


Daniel Demesmaecker wrote:... or you could write something that takes care of every scenario.


My thinking is that the more functionality that I include, the more effort I have to expend in the design, implementation, and testing for each scenario.  I don't want to spend my time dealing with cases that may never happen.

And what does every scenario include?  Is 0x6a a valid number?  What about twenty-one or XXXIV?

 
Daniel Demesmaecker
Master Rancher
Posts: 1043
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:
And what does every scenario include?  Is 0x6a a valid number?  What about twenty-one or XXXIV?



Lol... I give you that one...
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!