Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Changing strings in a switch statement n stroing it in an array

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

     {      
            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
    Attached Files Attached Files


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    java.lang.IllegalStateException: Scanner closed
    how to avoid that
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    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
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    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.
    Last edited by Norm; February 20th, 2013 at 03:32 PM. Reason: Need default:
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    What problems are you having with the program?

    4 strings which have to be converted to strings
    What conversion need to be done with the 4 Strings?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    What problems are you having with the program?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    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:
          System.out.println("tM="+java.util.Arrays.deepToString(testMarks)); //<<<
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,101
    Thanks
    52
    Thanked 2,285 Times in 2,256 Posts

    Default Re: Changing strings in a switch statement n stroing it in an array

    it wont read ints
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Feb 2013
    Posts
    67
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

Page 1 of 2 12 LastLast

Similar Threads

  1. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 3rd, 2013, 11:21 PM
  2. Strings in switch are not supported in source 1.5...
    By glennder in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 2nd, 2012, 03:33 PM
  3. Do you add objects to an array when using switch statement with a For loop?
    By TheWhopper858 in forum Collections and Generics
    Replies: 2
    Last Post: November 13th, 2011, 12:28 PM
  4. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  5. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM