last day (15 days later) » 

10:52 PM
1
A: Array Reading problems

PshemoYou don't actually even need ReadFile class to read all lines from file. Java provides you Files utility class in java.nio.file package which has readAllLines(Path) method. Also i==6 as mentioned already in comments will let you handle only sixth line, but it will not handle 16th, 26th. To handle...

 
your method is providing the following error error: method readAllLines in class Files cannot be applied to given types; because my textfile name is not provided but, rather input from the textfield. how do i get what is the textfiled and put it in List<String> lines = Files.readAllLines(Paths.get( fileField.getText()));
 
@mkwilfreid At start of code example from your question you are using File filing = new File(file);. I assume that file argument used in new File(...) constructor is String similar to the one from code example "input.txt". If that is the case then you should be able to use it like List<String> lines = Files.readAllLines(Paths.get(file));.
 
that's how it is, i tried the method you showed, but couldn't made it work maybe i was missing something until i saw you posted it. String file = fileField.getText(); File filing = new File(file);
 
Could you paste exact error massage you are getting (it will need to be formatted so link to pastebin.com would be fine)?
 
that's the error " error: method readAllLines in class Files cannot be applied to given types; "
 
10:52 PM
@mkwilfreid It should also say what types it expected and what type it got instead. That part is very important to solve this problem.
 
that's the full error : ^ required: Path,Charset found: Path reason: actual and formal argument lists differ in length 1 error
 
@mkwilfreid OK, apparently readAllLines(Path) version of this method was added in Java 8 and you are most probably are using earlier version. In that case you will need to use Files.readAllLines(Paths.get("input.txt"),Charset.defaultCharset()); or if you want to explicitly pass encoding of file you can use something like Files.readAllLines(Paths.get("input.txt"),Charset.forName("UTF-8"));.
You can also use StandardCharsets.UTF_8 instead of Charset.forName("UTF-8") like Files.readAllLines(Paths.get("input.txt"),StandardCharsets.UTF_8)
 
1000000%%%% working
do you mind helping me with what i'm trying to achieve as project, of course if you've time and will. i'm trying hard to get better in progamming
 
I'm also studding so my time is limited, but I can try to help you if it will not be too complex matter.
 
ok thanks. from what you've showed me it's worked, but however my main point in this, it's after having reading the file, it has to upload it on the database which i have. so i can make use of the preparedstatement.
so basically, for the questions in the file, which look like the following pastebin.com/vkVWKRJ1
Collections
Which of these is not an example of a "real-life" collection?
a. The cards you hold in a card game.
b. Your favorite songs stored in your computer.
c. The players on a soccer team.
d. The number of pages in a book.
for each question it has to put for example Collections in my CategoryColumn in the database, and Which of these is not an example of a "real-life" collection?
a. The cards you hold in a card game.
b. Your favorite songs stored in your computer.
c. The players on a soccer team.
d. The number of pages in a book.         in my QuestionsColumn in the database and the skipped part which is the answer d. in my answerColumn in the database
basically what i'm saying is for those questions,

Collections //// will go to my CategoryColumn in my database
Which of these is not an example of a "real-life" collection? ////from here
a. The cards you hold in a card game.
b. Your favorite songs stored in your computer.
c. The players on a soccer team.
d. The number of pages in a book. /// till here will go to my QuestionsColumn in my database

d. /// will go to my answer in my database
 
11:04 PM
Funny you mentioned it, because right now I am in the middle of reading JDBC tutorial. I am at start docs.oracle.com/javase/tutorial/jdbc/basics/… (I just finished setting up ant which is also new to me :/). Anyway this shouldn't be so hard. Anyway what troubles you? Do you have problems with SQL query?
 
my main issue is to insert it at the right time while the loop is going through the whole file
in the database
that's the main party that i'm missing now
from what you said i can do this
 
Simplest way I see it (for now) is to use for (int i=0; i<lines.size(); i+=10) loop to iterate over each question. In that case lines.get(i) will have always value for category, lines.get(i+1) will contain question, lines.get(i+2) until lines.get(i+5) will contain answers, and lines.get(i+7) will contain correct answer.
Simple code example pastebin.com/teuGY8s7. In this code I placed each part of single question in separate variable like category, question, answers, correctAnswer. Now you just need to use these variables in each iteration and place them in PreparedStatement.
 
11:29 PM
ok let me try cuz i was trying to get it connected to the database that's why i was not respoindig
but i'm a bit lost what about the first iteration you suggested before for (int i = 0; i < lines.size(); i++)
{if (i % 10 == 6)

i = i + 3;
}
 
In that loop all you need to do was each time print one line (so each time loop did same thing so we just may wanted to skip unnecessary input), but this time each line needs to be treated differently so we can't just use this loop.
So we will need to use different approach, very similar to described in this answer stackoverflow.com/a/26919092/1393766
 
11:52 PM
thanks it works as you showed now i just have to find my way and store in the database
it's a really pleasure to get helped by you.
 
I am glad to hear it :) Anyway I will be going now so good luck with putting it in DB (I can't help you here much since I also am in the middle of learning it :).
 
yeah thanks once again.
 

last day (15 days later) »