• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Write Files

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Please if someone can help me, the program doesn't go inside for,
dataset has 0 size in the write method.  From the first method is OK
Could you understand why??

 
Master Rancher
Posts: 172
25
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the problem you describe in not in your writeFile() method. How are you initialising your list? (by the way, dataSet is maybe not a good name, as it suggests that it should be a Set, when it is a List)
 
Jj Roberts
Master Rancher
Posts: 172
25
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe knowing what you are trying to do will also help. What is the purpose of your code? What do you expect it to do, and what does it actually do?
 
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't try nesting [u][/u] tags in code tags; it doesn't work. Simply quote the line number.
Don't create multiple Scanners reading System.in. You only need one Scanner reading from System.in. Why are you calling close() when try with resources is more reliable and has been available for nearly 11 years?
Why are you using for loops when a for‑each loop would be simpler?
You have more poorly named variables, including file, fileW, and out.
As JJ said, we would need more explanation of what you are planning to do. Please explain why you have a 0‑length List, in which case none of the loops will be entered. How do you know whether the loops are entered or not?
What's more, you are dealing with a poor error‑prone design, going through multiple arrays looking for matching elements. Why are you doing that?
Why does the loop in line 2 start from index 1 rather than 0? Why is the first element in the List something special to be thrown away? Why don't you collect the elements of the array into an object, so you can deal with a List<Foo> instead?
 
Eleni Pap
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for your answers. I try to encrypt some data.

Firts, I get two Lists from files. One from file with columns and rows (tab-separated) and the other one only lines.

So, I thought to save into two ArrayLists
for file1.txt
a b c
c b x
and the second ArrayList  from file2.txt
a
b


The first row for file1.txt  has all the columns
The second file has the name of the columns that will be encrypted.
So I take the ArrayList in the for loop in order to encrypt from the second line(because is the first has no sensitive data)


I understand that my names are not easy to read, I will improve it.

I am sorry for not being clear!
 
Jj Roberts
Master Rancher
Posts: 172
25
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, now I have a better idea of what is going on. The methods you have shown are not the cause of your issues. I think you should rather check the validity of your files, and the code which reads the data in and populates your list.

I think that you could probably use a better format for your data, but I'll leave that for now.

Regarding the code that you have shown:
1. Why don't you use List's method contains() instead of the innermost loop in your method method() (I'm sure you know that "method" is not a good name for a method)? That cuts down on the complexity.
2. In your writeFile() method, you are writing everything out on one line. You should use BufferedReader's newLine() method to add a newline after each row.
I'm not sure why you are mixing arrays and Lists. You are representing a table of data in a two-dimensional array form, and accessing values by their index. To me is seems that a straight two-dimensional array would be better in that case.

Here is a slightly improved version of your code. I'm also fairly green, so I'm sure it can still be bettered in many ways.
 
Eleni Pap
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your time and your help.

I am begginer and I face difficulties all the time.
For example, i haven't understand about inheritance , polymorphism, abstract classes. Because I don't practise on them quite. That means that I find it difficult  to call a method from another class.

To call these methods it needs to make a new instance on Main with name of the class.

Main()
Writing write= new Writing();
write.writeEncryptedData():
write.writeFile();

So why need abstract class?
To have general methods, which are used to another classes, in other words to avoid reuse code.

So in this excersise,
I should had method
openFile()

That means









I have been trying for days in diffferent ways, but it confuses me and I change my code again and again. For the above I have on Main()




       BufferedReader br = new BufferedReader();

on ReadingConfig , how to get the file, which give the user .??

Thank you !!
 
Jj Roberts
Master Rancher
Posts: 172
25
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inheritance and polymorphism are just theory; once they "click" for you, you will be fine.

Lets talk about inheritance.

Inheritance is useful when two classes are similar, but not identical. Inheritance allows you to reuse the parts that are the same, but allows you to have the differences too. It is difficult to find a good example, but think of this: you have an online store, which sells clothes and electronic devices. Now we think, "Ah, I need an Item class for all the items". All of your items have a name, a price, a colour and they all have a manufacturer. Great! Let's write the Item class:
We have a problem, though. Our clothes have a size, but our electronic devices don't. Our devices have a warranty, but our clothes don't. Oh, no, we'll have to write those as two different classes instead:
Look how much of that is repeated for the two classes, though! And what if we wanted to add an id to the items? We would have to add it to both classes! Thankfully for us, we can use inheritance to solve this problem.

ElectronicDevice and ClothingItem have everything the same except for one thing each one has that the other doesn't. What we can do is use a class with name, price, colour and manufacturer. If ElectronicDevice and ClothingItem extend that class, they will get those fields and methods, and we won't have to put them in the classes directly.



Do you see how that works? We can now use objects of type ClothingItem and ElectronicDevice just the same as we could when they were just the two classes, because they inherit both the fields and methods from Item. We don't have a lot of repeated code, though, which is a good thing. Do you understand everything so far?
 
Eleni Pap
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really appreciate your willing to help me and I am grateful for you.
I will go one step before now to be practised more, in simpler programs!!

 
Jj Roberts
Master Rancher
Posts: 172
25
Firefox Browser MySQL Database Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm happy to help. Don't worry if you are struggling and feel like you can't get anywhere. Learning to program is very challenging, especially at first. Simpler programs might be the way to go for a little bit, until you are comfortable with how Java works. I also recommend looking for good materials online on the fundamentals of OOP, encapsulation, abstraction, inheritance and polymorphism. There is a lot of poor quality stuff out there, though, so be careful. The Oracle learning trails are an excellent start.

I'm still wondering, though, why you specifically wanted to use an anonymous class, and why you wanted to use inheritance in your program. Is it just for practice?
 
Eleni Pap
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes for practice! I would like to minimise my code and make it more reusable!

for this reason I use the more methods, on my program.

For example, I change my code to read a file as above:





So I simply use the method to the other classes.
If I give the wrong file, Print: Try again
But then throw Exception
 
Campbell Ritchie
Marshal
Posts: 69494
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using an input stream reader rather than a file reader?
 
Eleni Pap
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because use BufferReader to read the content of the file and with File Reader I have an endless loop
 
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eleni Pap wrote:


I have some basic issues with this method:
  • You don't prompt for the file name.
  • You don't say why it failed.
  • The name of the method doesn't reflect what the method is doing. It should be something more like: openInputStreamReader().
  • Your variable 'file' is not a File but a Scanner. Even then,  it is a particular scanner that represents the user's 'keyboard'.
  • You have a variable 'f' that is not used.
  • You are handling the case where a file doesn't exist but you should do it in such a way that throwing a FileNotFoundException is no longer required.
  • You are creating a new Scanner from System.in every time the method is called. A Scanner created from System.in should only be created once during the entire running life time of your program. The best way to do that is make it a public static final Scanner constant, and then use that single constant everywhere. A very common way to achieve this is by creating a KeyboardUtility (see my signature below) to wrap this particular Scanner. I believe Campbell  Ritchie also has a similar utility.

  • ***EDITS***
  • You may want to pass in a String parameter that is the file name prompt, that way the caller can tailor the prompt to the application.
  • On lines 4 and 9 you may want to trim() the input to get rid of any leading or trailing spaces.
  • You have a potential endless loop of prompts. What if the user wants to exit the loop without opening the file? The prompt could say something like "...press Enter to exit", and then you'd need to decide what to return: null, Optional, or throw an exception.
  •  
    Campbell Ritchie
    Marshal
    Posts: 69494
    277
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Eleni Pap wrote:. . . with File Reader I have an endless loop

    In which case you are using the file reader wrongly. You are however right to wrap it in a buffered reader. Input stream readers are designed for reading System.in and similar. Please go through this part of the Java™ Tutorials.
     
    no wonder he is so sad, he hasn't seen this tiny ad:
    Devious Experiments for a Truly Passive Greenhouse!
    https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
      Bookmark Topic Watch Topic
    • New Topic