1 Attachment(s)
Changing an string from a switch statement and storing it an 2d array
Hey
Who ever my help me thank you in advanced
I have some that using a file and on this file i have 10 ints with commas in between . at the end of the line i have 4 strings which have to be changed into ints and stored into an 2d array along with the other ints.
can you help me ??
I have this code >
Code:
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;
String now = scanner.nextLine();
Scanner scanner2 = new Scanner(now);
scanner2.useDelimiter("[ ]*(,)[ ]*");
for(int add=0; add<responces; add++)
{
for (int columns =0; columns<10; columns ++)
{
testMarks[add][columns] = scanner2.nextInt();
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 + i] = scanner2.nextInt();
}
}
System.out.println("scanner null" + scanner2 == null);
scanner2.close();
scanner.close();
}
}
Re: Changing an string from a switch statement and storing it an 2d array
You didn't bother to reply to the response on your earlier thread: http://www.java-forums.org/new-java/...ent-class.html
Why should we expect any better behavior this time round?
Also, please go through the following links and edit your post accordingly:
http://www.java-forums.org/forum-gui...w-members.html
BB Code List - Java Programming Forum - Learn Java Programming
db
Re: Changing an string from a switch statement and storing it an 2d array
because i didnt know i had a reply then sorry
Re: Changing an string from a switch statement and storing it an 2d array
To help us help you:
- Consider editing your original post and surrounding your posted code with [code] [/code] tags so that it retains its formatting and we can then read it.
- Consider posting the text file that you are trying to analyze.
- Consider telling us what is wrong with your current code.
- Consider telling us exactly where you're stuck.
Much luck!
Re: Changing an string from a switch statement and storing it an 2d array
the problems im having is that it keeps saying null when trying to add to the array and im also not sure if it is adding to the array.
plus im stuck on changing the strings at the end and adding the ints at the start and storing them both.
Re: Changing an string from a switch statement and storing it an 2d array
Quote:
Originally Posted by
ajw1993
the problems im having is that it keeps saying null when trying to add to the array and im also not sure if it is adding to the array.
The exception message should tell you exactly which line is throwing the NullPointerException. Please find that line and let us know which one it is. A variable that you're trying to use on that line is null.
Quote:
plus im stuck on changing the strings at the end and adding the ints at the start and storing them both.
I'm sorry, I'm not clear on what you mean by this; please clarify.
Re: Changing an string from a switch statement and storing it an 2d array
null parts 1.testMarks[add][columns] = scanner2.nextInt();
2. switch(scanner2.next())
3. testMarks[add][columns] =scanner2.next ; (in the switch statement)
both of these have to be stored in the same 2d array:
1. the ints at the start of the line
2 . the strings at the end of the line which have to be changed into ints ( by a switch statement)
Re: Changing an string from a switch statement and storing it an 2d array
Quote:
Originally Posted by
ajw1993
null parts 1.testMarks[add][columns] = scanner2.nextInt();
2. switch(scanner2.next())
3. testMarks[add][columns] =scanner2.next ; (in the switch statement)
OK, are you sure that you've created a Scanner object? You can easily test this by posting
Code:
System.out.println("scanner2 null? " + (scanner2 == null));
And thanks for the edit.
Re: Changing an string from a switch statement and storing it an 2d array
String now = scanner.nextLine();
Scanner scanner2 = new Scanner(now);
scanner2.useDelimiter("[ ]*(,)[ ]*");
am i missing something from there because i have put that system out statement and nothing prints out to the terminal and says null on > testMarks[add][columns + i] = scanner2.nextInt();
i have posted the entire code for the method
Re: Changing an string from a switch statement and storing it an 2d array
If still null on that line and you're sure you've created the Scanner object, then test if the array is null on the line above where the NPE is thrown.
Re: Changing an string from a switch statement and storing it an 2d array
its coming out false when trying to test it and it keeps saying it when im trying to store it for the second time
would i have to change anything when trying to store it e.g. scanner2.nextInt() to ?