Win a copy of Production-Ready Serverless (Operational Best Practices) this week in the Cloud/Virtualization 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Reading from Text File without using loops  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Java Programmers, I have a question in my mind, that is>>>>
I have given four variables and there are five values in my text file, I have to read and assign those values into my four variables without using loops may be only just assignment operators there will work.  So is there any such methods or classes to do that ?

remember: It's not any assignment problem, I just want to know.
 
Saloon Keeper
Posts: 5753
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a Scanner you can just write 4 separate next____() calls. For only four of them this is not a big deal.
 
Bartender
Posts: 20562
120
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Using a Scanner you can just write 4 separate next____() calls. For only four of them this is not a big deal.



Yes, but that's cheating. You could just as easily write 4 readLine() method calls and eliminate the Scanner.

Plus the Scanner does loop internally, as it accumulates characters to return, so it's only externally non-looping to use it that way.
 
Saloon Keeper
Posts: 2182
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Shahriar Mim wrote:...I have to read and assign those values into my four variables without using loops may be only just assignment operators there will work.  ...


I am quite interested to know why someone would restrict you on how the code should be. Is there a complete problem statement that you can share with us ?
As I understand, you need to read the first 'n' lines of the file and assign it to 'n' variables, where n=4. I would naturally think about using a loop for such work, hence I am curious to know why you aren't supposed to use one.
 
Tim Holloway
Bartender
Posts: 20562
120
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally, compilers employ an optimization technique known as loop unrolling.

If the compiler detects that the body of a loop isn't too complicated, that the number of iterations is fixed, and that the number of iterations is small (typically less than 10), then the compiler itself will delete the loop and replace the code with multiple copies of the loop body, one for each requested iteration.

So you may not see any performance gains by manually unrolling a loop, and it would just result in more lines of code to ride herd on.
 
Rancher
Posts: 3111
110
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to cheat, then do it in an elegant way. Look at the 'Files.lines(...)' method, with .limit(n) and .toArray(String[]::new]. Just one line, and the 'variables' will be string[0]...string[N-1].
 
Piet Souris
Rancher
Posts: 3111
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:(...) So you may not see any performance gains by manually unrolling a loop, and it would just result in more lines of code to ride herd on.


On my ancient 25MHz Arm3 processor, with, IIRC 8 kb instruction cache, the advice was not to unroll loops too much, because of the possibility the code might not fit in the cache. But maybe current processors are less limited.
 
Tim Holloway
Bartender
Posts: 20562
120
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:

Tim Holloway wrote:(...) So you may not see any performance gains by manually unrolling a loop, and it would just result in more lines of code to ride herd on.


On my ancient 25MHz Arm3 processor, with, IIRC 8 kb instruction cache, the advice was not to unroll loops too much, because of the possibility the code might not fit in the cache. But maybe current processors are less limited.



Actually, I think the optimization predates CPU pipeline caches, so that wouldn't have been an issue. Now, of course, you'd have to consider the size of the cache. Plus, a decrement-and-jump is a basic machine code for many CPUs, so I don't know how much they were saving at the best of times.

But compiler designers base their optimizations on common practices, so the one thing I can advise the application programmer is not to get too "clever" with optimization, lest they defeat the built-in optimizations and end up with overall less efficient code. And, of course, better algorithms almost always greatly exceed optimizations done by tweaking instructions.
 
Bartender
Posts: 10759
68
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:You could just as easily write 4 readLine() method calls and eliminate the Scanner.
Plus the Scanner does loop internally, as it accumulates characters to return, so it's only externally non-looping to use it that way.


Hmmm. Pretty much any method that "reads" something will involve internal looping, so how much is "cheating"?

If OP is simply looking for something that allows the extraction of n-1 values from n, I suspect there are lots of ways, including extracting all into an ArrayList and deleting the last entry. But I suspect it might be a bit more than that. :-)

Winston
 
Tim Holloway
Bartender
Posts: 20562
120
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:Hmmm. Pretty much any method that "reads" something will involve internal looping, so how much is "cheating"?





So where's the loop?

If you're thinking a loop figures out where the end of the line is, that's not technically part of the read operation. And some machines, such as IBM mainframes have a basic instruction that can scan for "magic" characters and so even at the machine-language level there's no looping.

Oh, but what about at the microcode level? Maybe. But there's also this thing called Content-Addressable memory.

I think we'd better stick to the higher-level stuff here.
 
salvin francis
Saloon Keeper
Posts: 2182
92
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a more practical solution:


Bam !! no loops, no scan, nothing
Of course we need a well formatted file. But OP didn't mention anything about it.

Welcome Back Winston I think it's been a long time time (probably a year?)

 
Something must be done about this. Let's start by reading this tiny ad:
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!