Jump to content

Hey guys I have an assignment from school. Its a program that animates some shapes, I have to create a small program to read a "script" from a file.

 

I already know how to get java to scan a file and look for strings and numbers however I don't know how to make a "while" loop to make java read all the data from the file.

 

How do you make java remember and or let you know what line its are on when reading a file?

 

I've attatched a photo of what my code looks like at the moment, I hope you guys can help. Thanks in advance.

Animator.png

Link to comment
https://linustechtips.com/topic/793493-java-reading-from-files/
Share on other sites

Link to post
Share on other sites

You can create a custom class to store all of the scanned movements, then execute them one by one. In very broad terms:

  • Create a class that contains the name of the move, and its arguments
  • Create a loop that reads all the lines of the file, and instantiate your custom class into an array
  • Load the file data into your custom object array
  • When you are ready to execute all the commands, start a loop that reads every object from your custom class and do whatever you need to do

I'm not very familiar with java syntax (I'm more of a C# guy). However I you need more help I can share a program I did that fulfills a very similar objective to what you are trying to achieve (written in C# though).

Cheers!

Quote or tag if you want me to answer! PM me if you are in a real hurry!

Why do Java developers wear glasses? Because they can't C#!

 

My Machines:

The Gaming Rig:

Spoiler

-Processor: i5 6600k @4.6GHz

-Graphics: GTX1060 6GB G1 Gaming

-RAM: 2x8GB HyperX DDR4 2133MHz

-Motherboard: Asus Z170-A

-Cooler: Corsair H100i

-PSU: EVGA 650W 80+bronze

-AOC 1080p ultrawide

My good old laptop:

Spoiler

Lenovo T430

-Processor: i7 3520M

-4GB DDR3 1600MHz

-Graphics: intel iGPU :(

-Not even 1080p

 

Link to comment
https://linustechtips.com/topic/793493-java-reading-from-files/#findComment-9992364
Share on other sites

Link to post
Share on other sites

All of what @dany_boy has listed is essentially correct and indeed these concepts are not strictly specific to Java; although if you require more specific help then you may ask for it. The only thing that I'd add to it is that you may benefit from using a Command Pattern here.

 

Some general advice however:

  1. Don't screenshot your code. It's going to make people very much less inclined to lend you any assistance at all. No one really has the time, energy or inclination to manually copy out your code from your image into an editor - me probably least of all. It's just lazy and if anything it serves to be an insight into the total amount of effort that you have been putting in...
  2. Get rid of that garbage IDE and use a proper tool for the job such as IntelliJ (in your specific context). It's the difference between driving something like a 1ltr Micra vs a Maserati or Porsche... Your life will be made so much easier.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/793493-java-reading-from-files/#findComment-9992840
Share on other sites

Link to post
Share on other sites

16 hours ago, dany_boy said:

You can create a custom class to store all of the scanned movements, then execute them one by one. In very broad terms:

  • Create a class that contains the name of the move, and its arguments
  • Create a loop that reads all the lines of the file, and instantiate your custom class into an array
  • Load the file data into your custom object array
  • When you are ready to execute all the commands, start a loop that reads every object from your custom class and do whatever you need to do

I'm not very familiar with java syntax (I'm more of a C# guy). However I you need more help I can share a program I did that fulfills a very similar objective to what you are trying to achieve (written in C# though).

Cheers!

Hey dude Tnx for replying. I already have the class for the moves and arguments for it. Its just the loop part and file read commands I get stuck on .

 

I would love to see that program you mentioned, maybe i could get some ideas from it, even if its in C#.

Link to comment
https://linustechtips.com/topic/793493-java-reading-from-files/#findComment-9995958
Share on other sites

Link to post
Share on other sites

12 hours ago, Nuluvius said:

All of what @dany_boy has listed is essentially correct and indeed these concepts are not strictly specific to Java; although if you require more specific help then you may ask for it. The only thing that I'd add to it is that you may benefit from using a Command Pattern here.

 

Some general advice however:

  1. Don't screenshot your code. It's going to make people very much less inclined to lend you any assistance at all. No one really has the time, energy or inclination to manually copy out your code from your image into an editor - me probably least of all. It's just lazy and if anything it serves to be an insight into the total amount of effort that you have been putting in...
  2. Get rid of that garbage IDE and use a proper tool for the job such as IntelliJ (in your specific context). It's the difference between driving something like a 1ltr Micra vs a Maserati or Porsche... Your life will be made so much easier.

1. Duly noted.

2. I've tried IntelliJ before however it looks so complicated for me to use, I am beginner programmer (and not a smart one :( ).

 

Is there a website you can recommend where I can view all the java syntax there is and also very well organized and easy to navigate?

My java vocabulary isn't that good.

Link to comment
https://linustechtips.com/topic/793493-java-reading-from-files/#findComment-9996094
Share on other sites

Link to post
Share on other sites

10 hours ago, o0NotSoToxicPoisoN0o said:

Is there a website you can recommend where I can view all the java syntax there is and also very well organized and easy to navigate?

That would be the official Java Documentation.

6 hours ago, edward30 said:

I hope you're a smart guy, or you're just wasting your time trying to become a programmer. Unless you want to do web work; in that case you'll do fine.

xD

10 hours ago, o0NotSoToxicPoisoN0o said:

I've tried IntelliJ before however it looks so complicated for me to use, I am beginner programmer (and not a smart one :( ).

I suppose that quintessentially it comes down to whether or not you are happy with simply being mediocre at best. Are you even interested in Software Engineering/Development or is this just something that you are trying to get through.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/793493-java-reading-from-files/#findComment-9998134
Share on other sites

Link to post
Share on other sites

17 hours ago, Nuluvius said:

That would be the official Java Documentation.

xD

I suppose that quintessentially it comes down to whether or not you are happy with simply being mediocre at best. Are you even interested in Software Engineering/Development or is this just something that you are trying to get through.

"I suppose that quintessentially it comes down to whether or not you are happy with simply being mediocre at best." - These words I've been needing to hear!!

You just gave me fire>:(>:( m8, Thanks!

 

Of course I don't want to suck at programming but how can I get better?

How did you begin? What did you do?

Link to comment
https://linustechtips.com/topic/793493-java-reading-from-files/#findComment-10002256
Share on other sites

Link to post
Share on other sites

On 2017-6-17 at 3:15 AM, o0NotSoToxicPoisoN0o said:

Of course I don't want to suck at programming but how can I get better?

It depends on what you want to do, where you want to go and what you enjoy doing.

 

Learning a language i.e. understanding a given syntax is fine for starting out. However that alone is nowhere near enough in order to be considered well rounded, capable and actually being productive out there in the industry. To accomplish those then one really has to be concerned with the larger picture; I am specifically referring to theoretical concerns such as:

 

Principals, Practices and Patterns

Methodologies

Paradigms

These are some basic concepts that I consider (based on my own experience) to be particularly important to be started on as early as is possible, in parallel with your syntax play. The earlier that one begins to understand, ingrain and practice these concepts then the better really.

 

Be Aware of

Some Particularly Useful Sites

I mentioned that it is important to understand where you want to go and what you want to do because these factors will also have an effect on the theory that you will be expected to understand and practice.

 

For instance, consider the Software Developer vs the Software Engineer. In current industry it is argued that the concerns of the former lie specifically within the sphere of implementing a predefined design or a technical subset of a given project, while the latter is in control of defining and implementing that overall design and laying out the overall technical topology of a given project.

 

Another example is the Computer Scientist who is generally concerned with the R&D of new technologies and implementations; the results of which are later to be taken and refined by (at least) the previous two and integrated into a production grade implementation. They may branch out to be multidisciplinary, combining software knowledge with other scientific disciplines such as Physics, Mathematics, Chemistry, Biology and so on. This is something that can be true of the other roles to a degree as well, it greatly depends on the company to be honest.

On 2017-6-17 at 3:15 AM, o0NotSoToxicPoisoN0o said:

How did you begin? What did you do?

My story is a complicated, different to most but likely similar to some. To summarise, I started relatively late in life and I had to crawl scratching and clawing to where I am right now. Essentially I wanted more than I had and was compelled not to settle for less.

 

One of the smartest things that you can do, especially given the flavour of today's industry, is to diversify as much as possible; cover as many languages and as much theory as you can fit into your skull. This will increase your value and general ability to find work, be it contracting or permanent. This is an ethos that I personally follow and, again in my experience, have found that it works very well. There's value to be found in mostly everything one picks up along the journey and each tool has (or will have) it's place - the right tool for the right job.

 

Pragmatism is also an important doctrine; doing the simplest possible thing to get a result, refactoring and generalising later but in balance against gain/value. Also understanding when something is not working or feels wrong and having the courage and resolve to challenge your own approach and assumptions and if necessary to rethink them even if it means throwing out hours or days of work.

 

I hope that you find some of this useful. If you have further questions then we are here for you.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
https://linustechtips.com/topic/793493-java-reading-from-files/#findComment-10016417
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×