-
1 Attachment(s)
Changing strings in a switch statement n stroing it in an array
Hey i need help i need to read in a file (the delimiter below) n store it in an araay whilst changing a string from the end of the file into the array by changing it into an int .
Any Help this is what i have up to now.
the problem is that is saying that the second scanner is not working.
also im not sure if it is storing the changed strings
Code java:
{
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 gem = 0;
for(int add=0; add<responces; add++)
{
Scanner scanner2 = new Scanner(scanner.nextLine());
scanner2.useDelimiter("[ ]*(,)[ ]*");
for (int columns =0; columns<10; columns ++){
for(int i=0; i<4; i++)
{
switch(scanner2.next())
{
case "excellent": gem= gem + 5; break;
case "very good": gem= gem + 4; break;
case "good": gem= gem + 3; break;
case "average": gem= gem + 2; break;
case "poor": gem= gem + 1; break;
}
testMarks[add][columns+gem] = scanner2.nextInt();
//System.out.println();
} //for the third for
} // for the second for
scanner2.close();
scanner.close();
}//for the first
} // to end
-
Re: Changing strings in a switch statement n stroing it in an array
Can you explain what problems you are having?
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
-
Re: Changing strings in a switch statement n stroing it in an array
changed the code n out the problems im having in the message
-
Re: Changing strings in a switch statement n stroing it in an array
Quote:
problem is that is saying that the second scanner is not working.
Can you explain what that means? What does "not working" mean? What is a "second scanner"? Post the names of any program variables that you are talking about.
If there are error messages, please copy the full text and paste it here.
The code has some poor coding styles that chain several method calls together in one statement instead of having one method call per statement which allows you to test results before passing it to the next method.
-
Re: Changing strings in a switch statement n stroing it in an array
the second scanner object that is in the first for loop
- replies that scanner closed in util scanner.
java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1115)
at java.util.Scanner.next(Scanner.java:1403)
at studentMarks.readMarksData(studentMarks.java:46)
.i just need to know how to avoid that and also change the switch to store the ints in the array.
-
Re: Changing strings in a switch statement n stroing it in an array
Quote:
java.lang.IllegalStateException: Scanner closed
Don't close the Scanner until you are finished using it.
The code is poorly formatted. The ending }s should be inline below the start of the the statement with the matching {.
Too many of the }s are in the first column which makes it hard to see the nesting of the code to see where the close statements are.
-
Re: Changing strings in a switch statement n stroing it in an array
the problem now iss that it cant find anything when trying to store anything so what should i change to put there
-
Re: Changing strings in a switch statement n stroing it in an array
Quote:
it cant find anything when trying to store anything
Can you be more specific? What happens when the code executes?
Try debugging the code by adding some println statements to print the line that is read and the tokens used in the switch statement so you can see what the code sees when it executes.
The formatting is still poor.
There should never be two } one below the other.
There should never be a lower } further to the right than the } above it.
The third for should be indented the same as the first two for statements are.
Where is the ending } for the third for statement? It should be beneath the f in for
-
Re: Changing strings in a switch statement n stroing it in an array
yeah sure when it excutes in the testMarks[add][columns+gem] = scanner2.nextInt(); <= it says that it is null and ovbs but im not sure what to put to make it work
-
Re: Changing strings in a switch statement n stroing it in an array
Print out the values that the code is working with as I suggested in my last post. You need to see what the computer sees to understand what the problem is.
-
Re: Changing strings in a switch statement n stroing it in an array
is there any way of storing the ints once it has been in the loop along with the other ints in the file on the lines as you can see i have posted the file above that i am working with
-
Re: Changing strings in a switch statement n stroing it in an array
Quote:
is there any way of storing the ints
There shouldn't be any problems storing int values into a variable or array if the variable's definition is in scope where you are trying to access it.
Can you explain what problem you are having?
A problem I see in the code is a missing default: clause in the switch statement.
You should ALWAYS have a default clause to catch problems when testing.
It should have a println statement with a message and show what value caused it to be executed.
-
Re: Changing strings in a switch statement n stroing it in an array
yeah its the way that the file lines are n i have to store the ints into an array plus change the strings into ints and put them into the 2d array, but since it is cant read ints by the way that it set up . This is what im trying to achieve overall.
-
Re: Changing strings in a switch statement n stroing it in an array
Quote:
it is cant read ints by the way that it set up
Sorry, I have no idea what you said.
The Scanner class methods that the code uses must be used in the same order that the data is in the file.
If there are 5 numeric values in the file, then the code should call nextInt() 5 times.
If next there is a String, then the code should call next() one time.
-
Re: Changing strings in a switch statement n stroing it in an array
there are 8 lines of code on each line there are 10 ints that have to be read in followed by 4 strings which have to be converted to strings and stored in a 2d array. If you get it now
-
Re: Changing strings in a switch statement n stroing it in an array
What problems are you having with the program?
Quote:
4 strings which have to be converted to strings
What conversion need to be done with the 4 Strings?
Quote:
converted to strings and stored in a 2d array
An array can only hold one data type. You can not store both int and String in the same array.
-
Re: Changing strings in a switch statement n stroing it in an array
there is 5 different ratings and each of them have to be changed to an int depending on the rating type .e.g excellent - 5 , very good - 4 etc
-
Re: Changing strings in a switch statement n stroing it in an array
What problems are you having with the program?
-
Re: Changing strings in a switch statement n stroing it in an array
its not reading all the lines or storing the ints and the converted strings into the 2d array all it does is read the converted strings
-
Re: Changing strings in a switch statement n stroing it in an array
Quote:
its not reading all the lines
What makes the code stop reading the lines?
You need to do some debugging as I suggested in post#8
Try debugging the code by adding some println statements to print the line that is read and the tokens used in the switch statement so you can see what the code sees when it executes.
and in post#12
A problem I see in the code is a missing default: clause in the switch statement.
You should ALWAYS have a default clause to catch problems when testing.
It should have a println statement with a message and show what value caused it to be executed.
Another useful debugging tool is the Arrays class's deepToString() method. It will format a 2 dim array for printing:
Code :
System.out.println("tM="+java.util.Arrays.deepToString(testMarks)); //<<<
-
Re: Changing strings in a switch statement n stroing it in an array
what i meant by it not storing the strings is when it has been converted into ints and i have put some but it doesnt seem to print out the first time only the last 4 after being converted
-
Re: Changing strings in a switch statement n stroing it in an array
How are you debugging the code? Have you added the println statements I suggested so you can see what the code is doing?
You should print out the contents of the testMarks array frequently to see what is being added to it.
-
Re: Changing strings in a switch statement n stroing it in an array
testMarks [add][columns+gem]= scanner2.nextInt(); <= what do i need to put there since it wont read ints
-
Re: Changing strings in a switch statement n stroing it in an array
Please explain what that means.
The nextInt() method will read numeric characters representing an int value and convert them to an int.
Try this to see what is happening. Call next() instead of nextInt() and print out what was read.
String aStr = scanner2.next();
Then use the Integer class's parseInt() method to convert the String that was read to an int that can be stored in the array.
-
Re: Changing strings in a switch statement n stroing it in an array
what should i put in the switch paramter ? that keeps coming up null