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.

Results 1 to 10 of 10

Thread: how to use Scanner and useDelimiter with int

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to use Scanner and useDelimiter with int

    my first post in this forum hope i do everything right …
    I have this code but i get error because i think of the " ; " separator in the file.

    so my code is
    import java.io.*;
    import java.util.*;
     
    public class metoder {
    	private int id, km, min, totalt;
     
    	Scanner sc;
     
    	public void lasFil() throws IOException{
    		sc = new Scanner (new File("resor.txt"));
    		 sc.useDelimiter(";");
     
    		while (sc.hasNext()){
     
    			id = sc.nextInt();
    			km = sc.nextInt();
    			min = sc.nextInt();
     
    			if (km >20 & min <= 60 ){
    				System.out.println("ID:"+id+" "+km+"Km  " + min +"Min: ");
    				}
    			}
    	}
     
     
     
    }

    how do let java know that the ";" is the separator for each int ? the FIle looks like this ,
    1;53;38
    2;28;27
    3;39;72
    4;12;30
    5;21;35
    6;19;32


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to use Scanner and useDelimiter with int

    i get error
    Please copy the full text of the error message and post it here so we can see what the problem is.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how to use Scanner and useDelimiter with int

    What does this program do? What error do you get?

    Read the API for the Scanner class. Notice the difference between hasNext() and hasNextLine(). Also please notice that nextInt() doesn't scan past the next line character.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use Scanner and useDelimiter with int

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and post it here so we can see what the problem is.
    This is the error code i get .
    Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:840)
    at java.util.Scanner.next(Scanner.java:1461)
    at java.util.Scanner.nextInt(Scanner.java:2091)
    at java.util.Scanner.nextInt(Scanner.java:2050)
    at metoder.lasFil(metoder.java:17)
    at huvudapp.main(huvudapp.java:7)

    --- Update ---

    the program scans a text file and gives it names
    Ex
    1;53;38 > gets ID : 1 KM:53 Min:38

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to use Scanner and useDelimiter with int

    Which source line gives the error? Which line is line 17 in the source?
    How many int values have been read before the error?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how to use Scanner and useDelimiter with int

    Did you read the API for the Scanner class? The nextInt() method doesn't scan past the next line character.

    If you don't understand what I mean by that, I recommend stepping through this with a debugger, or at least adding some print statements, until you understand exactly what the program is doing. How far does it get into the text file? What does it succeed in scanning in? Exactly where in the file does it fail?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use Scanner and useDelimiter with int

    Quote Originally Posted by KevinWorkman View Post
    Did you read the API for the Scanner class? The nextInt() method doesn't scan past the next line character.

    If you don't understand what I mean by that, I recommend stepping through this with a debugger, or at least adding some print statements, until you understand exactly what the program is doing. How far does it get into the text file? What does it succeed in scanning in? Exactly where in the file does it fail?
    ok! i read it , and like i said i'm not very good at java . so what is an debugger ? it seams that it finds the file but doesn't read it because the ; symbol .

  8. #8
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use Scanner and useDelimiter with int

    Screen Shot 2013-08-27 at 9.28.47 PM.jpg
    here is the lines , sorry did't think about the line numbers

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to use Scanner and useDelimiter with int

    If you don't have a debugger or know how to use it, another way to debug code as it executes it to use the
    System.out.println("an ID "+ theVarHere); statement to print out the values of variables as they are assigned values in statements. Some times there needs to be one call to the println() statement for each source line.
    Replace the comment and theVarHere with the variable in the code whose value you want to see.

    By printing out the variables as they are read you will know how far the program executed.

    BTW Posting images of code and error messages is useless. There is no way to copy anything from an image.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to use Scanner and useDelimiter with int

    so after couples of days and reading on internet i found a working solution i want share
    happy programming
    import java.io.*;
    import java.util.*;
     
    public class huvudapp {
     
    	private String strID, strKM , strMIN ;
    	private int i;
     
    	public void lasFil()throws IOException{
    		Scanner sc = new Scanner (new File ("resor.txt")); //opens file named resor.txt
    		while (sc.hasNextLine()){ // the while is reading the file
    			String line = sc.nextLine(); // every line sends to "line" so we can split it 
    			String [] arr = line.split(";"); //creats an array and says ";" when you see the symbol split the text
     
    			strID = arr[0]; // the diffrent values in the file get there names 
    			int id = Integer.parseInt(strID);// string turns to INT for the IF statment 
    			strKM = arr[1];
    			int km = Integer.parseInt(strKM);
    			strMIN = arr[2] ;
    			int min = Integer.parseInt(strMIN);
     
    			if (km>20 & min<60){
    				System.out.print("ID:"+id+" MIN:"+min+" KM:"+km +"\n"); // prints out the values that made the if statment 
    				i++; // just to count how many persons there was. 
    			}
     
    		}System.out.println(i+" persons that made the if statment");
    }
    	public static void main(String[] args) throws IOException { 
     
    		huvudapp mt = new huvudapp (); 
    		mt.lasFil(); // calls the class ?? or object ? don't really know the definition
     
    	}
     
    }

    Prints
    ID:1 MIN:38 KM:53
    ID:2 MIN:27 KM:28
    ID:5 MIN:35 KM:21
    3 persons that made the if statment
    Attached Images Attached Images

Similar Threads

  1. Bug in Java ?? substring( int x, int y)
    By cheringalho in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 19th, 2013, 10:51 AM
  2. How to create an Int or double of what the user types int a JTextField?
    By Speedstack79 in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2013, 11:00 PM
  3. List methods add(int k, Data data), set(int k, Data data), remove(int k)
    By Enirox in forum Object Oriented Programming
    Replies: 3
    Last Post: September 20th, 2012, 06:43 AM
  4. scanner and int issue
    By saintnicks in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 24th, 2012, 01:01 AM
  5. Scanner.useDelimiter("\n") Problem
    By Noob_Programmer in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 31st, 2011, 09:28 AM

Tags for this Thread