-
2 Attachment(s)
Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Can Anyone Help me ??
the problems is i dont know how to store it
its keeping coming up null in the storing
also in the switch.
the strings have to be converted into ints depending on the rate of excellence.
the code is below
public void readMarksData(String fileName)throws FileNotFoundException
{
File dataFile = new File (fileName);
Scanner scanner = new Scanner(dataFile);
//System.out.println(scanner.nextLine());
cohortName = scanner.nextLine();
int responces = scanner.nextInt();
scanner.nextLine();
//int numberOfStudentResponses=scanner.nextInt();
//System.out.println("Number of Student Responces " + responces);
testMarks = new int [responces][10];
int num;
for(int add=0; add<8; add++)
{
Scanner scanner2 = new Scanner(scanner.nextLine()).useDelimiter("[ ]*(,)[ ]*");
for (int columns =0; columns<10; columns ++){
for(int i =0; i<4; i++){
switch(scanner2.next())
{
case "excellent": num=5; break;
case "very good": num=4; break;
case "good": num=3; break;
case "average": num=2; break;
case "poor": num=1; break;
default : num=0; break;
}
testMarks[add][columns+num] = scanner.nextInt();
}
}
scanner2.close();
scanner.close();
}
}
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Quote:
its keeping coming up null
Please copy and paste here the full text of the error messages.
Is this the same problem?
http://www.javaprogrammingforums.com...ing-array.html
Have you tried debugging the code as suggested in post#20 in that thread?
-
1 Attachment(s)
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
its in the for area that is more or less the problem
you can see in the posted image Attachment 1801
Code java:
public void readMarksData(String fileName)throws FileNotFoundException
{
File dataFile = new File (fileName);
Scanner scanner = new Scanner(dataFile);
//System.out.println(scanner.nextLine());
cohortName = scanner.nextLine();
int responces = scanner.nextInt();
scanner.nextLine();
//int numberOfStudentResponses=scanner.nextInt();
//System.out.println("Number of Student Responces " + responces);
testMarks = new int [responces][10];
int num;
for(int add=0; add<8; add++)
{
Scanner scanner2 = new Scanner(scanner.nextLine()).useDelimiter("[ ]*(,)[ ]*");
for (int columns =0; columns<10; columns ++)
{
System.out.println();
for(int i =0; i<4; i++)
{
System.out.println();
switch(scanner2.next())
{
case "excellent": num=5; break;
case "very good": num=4; break;
case "good": num=3; break;
case "average": num=2; break;
case "poor": num=1; break;
default : num=0; break;
}
}
}
System.out.println();
scanner2.close();
scanner.close();
}
}
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Have you tried debugging the code as suggested in post#20 in that thread?
You need to add the println statements so you can see what the computer sees when it executes the code.
If you get errors, Please copy and paste here the full text of the error messages.
The posted code needs to be formatted so the { and } aline properly.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
ive tried everything i could just post all the code from the class n ive put the print statements in nothing is happening its still the switch statement
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Post the code with the println statements that I've suggested be added for debugging.
Also post the output from when you have executed the code with those printlns.
You need to learn how to debug your code. I use printlns for debugging and find them very useful.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
its printing out the changed ints that works fine so its changing , but i dont know what to do now ive posted all my code in the file
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Where did you post the version of the code with the println() statements for debugging?
Where did you post the output for when the code with the debug printlns was executed?
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
it keeps coming out blank because of the switch statement parameter so it isnt printing anything
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Post the code with the debug println statements.
Post the output from the program.
-
1 Attachment(s)
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
If you want help with your problem, this is what you have to do:
Post the code with the debug println statements.
Post the output from the program. Not an image.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
I guess asking for two things at the same time was confusing. Let me ask for them one at a time:
Post the full code with the debug println statements.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Code java:
public void readMarksData(String fileName)throws FileNotFoundException
{
File dataFile = new File (fileName);
Scanner scanner = new Scanner(dataFile);
//System.out.println(scanner.nextLine());
cohortName = scanner.nextLine();
int responces = scanner.nextInt();
scanner.nextLine();
//int numberOfStudentResponses=scanner.nextInt();
//System.out.println("Number of Student Responces " + responces);
testMarks = new int [responces][10];
int num;
for(int add=0; add<8; add++)
{
Scanner scanner2 = new Scanner(scanner.nextLine()).useDelimiter("[ ]*(,)[ ]*");
for (int columns =0; columns<10; columns ++)
{
for(int i =0; i<4; i++)
{
switch(scanner2.next())
{
case "excellent": num=5; break;
case "very good": num=4; break;
case "good": num=3; break;
case "average": num=2; break;
case "poor": num=1; break;
default : num=0; break;
}
System.out.println(num);
}
}
scanner2.close();
scanner.close();
}
}
--- Update ---
this is printing out 10 0's but with the ints that were changed from strings
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Ok, now you need to change the code some so that ALL data that is read in is printed before it is used. These statements need to be changed:
Code :
Scanner scanner2 = new Scanner(scanner.nextLine()).
switch(scanner2.next())
Read from the Scanner into variables, print the value of the variable and then use that variable where the call to the Scanner class methods are now.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
when does the delimiter come into use because i need to use it
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
That part of the code is not changed. The change is removing the calls to Scanner methods to a separate line,
printing what was read and then the value read is used where the calls to the Scanner methods were.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
could u show me what u mean kinda making it confusing ive only just started in sept thats why im having trouble
Scanner scanner2 = new Scanner(scanner.nextLine());
scanner2.useDelimiter("[ ]*(,)[ ]*");
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Code :
String aStr = scanner.nextLine(); // read next line into a variable
System.out.println("aStr="+aStr); // show what was read
Scanner scanner2 = new Scanner(aStr); // set the Scanner to use the line
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
testMarks[add][columns + num] = scanner.nextInt(); - still null , but prints out the first line of the file
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
what does "still null" mean? If there are error messages, copy the full text and print them.
Change
testMarks[add][columns + num] = scanner.nextInt();
to
Code :
String aStr2 = scanner.next(); //read next tken
System.out.println("aStr2="+aStr2); // show what was read
testMarks[add][columns+num] = Integer.parseInt(aStr2); // convert to int
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
i do need to use the delimiter tho to suit the file above tho
1. need to use the delimiter
2. i need to read in all the ints on the lines in the file and store them in the 2d array
3. i need to change the strings at the end of the line and store them in the 2d array
these are what i need to do
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
Quote:
need to use the delimiter
Use the scanner variable to call the useDelimiter() method:
Code :
scanner2.useDelimiter("[ ]*(,)[ ]*");
This was a problem I talked about in the other thread: The code chained together too many method calls in one statement instead of having one method call per statement.
-
Re: Im having trouble storing ints that have been converted at the end of a line in a file into a 2d array.
so how do i the other two , cz of the commas n etc they are the problem