Results 1 to 4 of 4

Thread: Scanner issue

  1. #1
    Imstuck is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Scanner issue

    Hey guys, i got an issue here.
    I have a file called 'resultaten.txt' and its like this: the file contains n+1 lines. on the first line is the number n. Each next line contains a string (the studentnumber) and (separated by a space) an integer (his score).
    For example:

    3
    000123 15
    000124 18
    000125 9

    We have to give a studentnumber and the method must check if his score is lower than 10 and if it it we have to change his score to 10. My code however isn't working, hope you can help.

    Java Code:
    import java.util.Scanner;
    import java.io.*;
    
    public class Punten
    {
        public static void hackPunten(String mijnnummer) {
            try{
                Scanner hack1 = new Scanner(new File("resultaten.txt"));
                PrintWriter hack2 = new PrintWriter(new BufferedWriter(new FileWriter("resultaten.txt")));
                int aantal = hack1.nextInt();
                String[] nummer = new String[aantal];
                int[] punten = new int[aantal];
                for(int i = 0; i < aantal; i++){
                    String n = hack1.next();
                    nummer[i] = n;
                    int p = hack1.nextInt();
                    if(p < 10 && nummer[i].equals(mijnnummer)){
                        p = 10;
                    }
                    punten[i] = p;
                }
                for(int i = 0; i < aantal ; i++){
                    hack2.println(nummer[i]);
                    hack2.print(" " + punten[i]);
                }
                hack1.close();
                hack2.close();
    
            }catch(Exception e){
                System.out.println("Schrijven mislukt");
            }
    
        } 
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Scanner issue

    One thing is that you're using a println( ... ) method where you should've used the print( ... ) method (line #23) and vice versa (line #24).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Imstuck is offline Member
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Re: Scanner issue

    Yes i have change this

    Java Code:
    for(int i = 0; i < aantal ; i++){
                    hack2.println(nummer[i]);
                    hack2.print(" " + punten[i]);
    into this:

    Java Code:
    or(int i = 0; i < aantal ; i++){
                    hack2.print(nummer[i]);
                    hack2.println(" " + punten[i]);
    Still doesn't work though. I get an Nosuchelementexception. (and from the test program my teacher wrote it says that i'm trying to adjust a score, but the score is higher than 10)

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Scanner issue

    I suppose you have a stack trace somewhere (and if you haven't, shame on you ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Simple Scanner Issue
    By jazzermonty in forum New To Java
    Replies: 7
    Last Post: 07-17-2012, 10:47 PM
  2. Issue with Scanner object
    By smvolt in forum New To Java
    Replies: 1
    Last Post: 06-27-2012, 08:54 AM
  3. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  4. Scanner delimiter issue
    By PrinceSendai in forum New To Java
    Replies: 3
    Last Post: 10-05-2010, 09:41 AM
  5. Class Scanner looping issue
    By Stev0 in forum New To Java
    Replies: 1
    Last Post: 05-25-2008, 06:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •