Page 1 of 2 12 LastLast
Results 1 to 20 of 38
  1. #1
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Reading Integers from text file (file contain letters)

    This code dont reads anything. I need a full code who reads text file, and printts to console only int/double. No String.
    Text file:
    uug 14n 1785
    4588 dd or
    pe
    123




    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    
    import java.util.Scanner;
    
    public class Z {
    
    	public static void main(String[] args) throws FileNotFoundException  {
    	
    	
    		
    		Scanner s = new Scanner(new File("tes"));
    		int[] array = new int[s.nextInt()];
    		for (int i = 0; i < array.length; i++)
    		    array[i] = s.nextInt();
    		    
    	
    	
    	System.out.println(array);
    	
    	
    	
    	
    	}
    }

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

    Default Re: Reading Integers from text file (file contain letters)

    To start: you are trying to create an array with n elements, where n is the first integer read from your file; but 'uug' isn't an int in my book ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading Integers from text file (file contain letters)

    Its obvious that I dont find right code. Can you write me one?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Reading Integers from text file (file contain letters)

    This isn't a code factory.

    To take your first example line:
    uug 14n 1785

    What output do you want?
    14 and 1785, or just 1785?

    That will determine what you need to do.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading Integers from text file (file contain letters)

    Quote Originally Posted by Tolls View Post
    This isn't a code factory.

    To take your first example line:
    uug 14n 1785

    What output do you want?
    14 and 1785, or just 1785?

    That will determine what you need to do.
    If text file is .................................................. ..............................................." 123 4w6 ave
    ( dots dont exist in code, just addded because forum ignores [space]........................qwe 4r7 887 "

    The console should show .........." 123
    .............................................887 ".

    Because other "words" have letter, so they should be skipped.
    Last edited by asdfg; 11-25-2016 at 01:51 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Reading Integers from text file (file contain letters)

    Right.
    Then read in lines.
    You've already got a Scanner, so use the nextLine() method only.
    Then use the split() method on String, splitting on a space.
    This will give you a String[].
    For each element in that array check if it's a number...that part might be harder as you seem to want both integers and doubles.

    Do this a step at a time and check the step you just wrote works before moving to the next one.
    Do not try and write this all at once.

    ETA: Oh, and use a List<Integer> to store the results, not an int[]. That way you can forget how many elements to make it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading Integers from text file (file contain letters)

    I went to IT school 3 months ago. I have not learned any programming language, and java is first I attempt.

    Everything what I have learned, is to class structure, basic loop and switch-case method. Some very basic printto file and scan from file.

    The reason why I ask help on forum, is because I get trouble to find right code. As usually, code dont work or it do not fit my needs.

    Getting the full code, who does one-action is a big jump for me. Because literature is somehow to complex to understand. And I ask code, not because to complete my homework, but because it is part of homework algoritm. Yet I need to figure this alot.

  8. #8
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading Integers from text file (file contain letters)

    Java Code:
    static void addIfAlphabetic(
       List<String> words, String word)
    {
       if (word.chars().allMatch(Character::isLetter)) words.add(word);
    }
     
    public static void main(String[] args) throws FileNotFoundException {
        ArrayList<String> target = new ArrayList<>();
     
        new Scanner( new File("turg") ) .forEachRemaining(word -> addIfAlphbetic(target, word));
                   
        System.out.println ( target );
    }
    This code picks all String values from file, and convert thems to collection elements.

    So now I need code who would pick up all numbers, but no Strings.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading Integers from text file (file contain letters)

    need code who would pick up all numbers
    Change the addIfAlphbetic method to one that adds numbers instead.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading Integers from text file (file contain letters)

    Quote Originally Posted by Norm View Post
    Change the addIfAlphbetic method to one that adds numbers instead.
    Java Code:
    if (word.chars().allMatch(Character::isLetter)) words.add(word);
    I dont know what word shuld be instead of 'chars' and '(Character::isLetter)' needed to be changed?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading Integers from text file (file contain letters)

    Start by reading the API doc for the Character class to see if any of the methods will do what you want. Also read the API doc for the String class.
    Java Platform SE 8
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading Integers from text file (file contain letters)

    But I dont understad them, thats why I ask help. Actually the problem relies, that I dont know there to add those code outcrups.
    Also I quess there is problem that I didint figured out how paremeters/arguments works properly.
    Yet I know how Random parameters work.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading Integers from text file (file contain letters)

    But I dont understad them
    Copy and paste here the parts you don't understand and someone can explain what it means.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading Integers from text file (file contain letters)

    For example this: java.awt.color
    How to use, i Mean to which code part it should be binded?
    Description also " Provides classes for color spaces. "
    I dont get it?? The console [space] is fullfilled with color?

    The another this I recognize: java.math
    It is used like this. double b = Math.sqrt (9); But the thing is, the doc library did not provided any example, as syntax is diffrend thing.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Reading Integers from text file (file contain letters)

    Quote Originally Posted by asdfg View Post
    For example this: java.awt.color
    How to use, i Mean to which code part it should be binded?
    Description also " Provides classes for color spaces. "
    I dont get it?? The console [space] is fullfilled with color?

    The another this I recognize: java.math
    It is used like this. double b = Math.sqrt (9); But the thing is, the doc library did not provided any example, as syntax is diffrend thing.
    I'm afraid you're rambling now; I don't understand one single thing from from what you wrote ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading Integers from text file (file contain letters)

    doc library did not provided any example
    There are examples in the tutorial: The Really Big Index
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Reading Integers from text file (file contain letters)

    This is not OP's code. All the stream code snippets and addAlphabetic stuff is copied from here: https://coderanch.com/t/673078/java/...-maker#3149370
    I think it's totally unrelated to his original problem of reading Integers.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading Integers from text file (file contain letters)

    Yes, obviously the OP didn't write that code. But I think it could be modified for his purposes, if he understood what they are.

    BTW. Thanks for the link.
    Last edited by Norm; 11-26-2016 at 04:22 PM.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    asdfg is offline Senior Member
    Join Date
    Nov 2016
    Posts
    163
    Rep Power
    4

    Default Re: Reading Integers from text file (file contain letters)

    Quote Originally Posted by Norm View Post
    Yes, obviously the OP didn't write that code. But I think it could be modified for his purposes, if he understood what they are.

    BTW. Thanks for the link.
    Of course it is not my code. It is too complex for me to create. Thats what I have stated in this topic. Thats how I learn- I get some some short code- analyze it.
    Thats why I asked new code in this topic.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Reading Integers from text file (file contain letters)

    Have you tried to convert that code so it returns all the integer values?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 12-09-2014, 01:27 AM
  2. Replies: 5
    Last Post: 10-08-2013, 06:40 PM
  3. Error in reading from a file containing integers
    By stud91 in forum Advanced Java
    Replies: 1
    Last Post: 07-07-2012, 06:24 PM
  4. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  5. Reading Integers from a text file
    By tress in forum New To Java
    Replies: 6
    Last Post: 02-26-2011, 06:45 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
  •