Results 1 to 6 of 6
  1. #1
    sjoelsteen156 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Chopping integers into seperate digits.

    Hello,

    I am trying to learn programming java. In order to understand
    things better, I have made a program and I try to do anything
    that pops into my mind. What I have done now is I let a user
    choose a number from 1-9. The next thing I do is create 1-9
    (depending on the user's choice) random numbers and write
    them into a file. Then I want to scan those integers and figure
    out how many times the user's choice occurs as a digit in those
    numbers (I know it sounds vague, and it is useless, but I just
    want to learn java).
    Java Code:
    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.io.File;
    import static java.lang.System.out;
    
    public class ReadingFile {
    	private Scanner diskScanner;
    	public char symbol;
    	public int counter = 0;
    	
    	public void checkingForNumber(int numberOfNumbers) throws FileNotFoundException {
    		diskScanner = new Scanner(new File("FileOne"));
    		symbol = diskScanner.findWithinHorizon(".",0).charAt(0);
    		while (diskScanner.hasNext()) {
    			symbol = diskScanner.findWithinHorizon(".",0).charAt(0);
    			if (symbol == numberOfNumbers) {
    				counter++;
    			}
    		}
    		out.println("Your number as a digit occurs " + counter + " times in the file.");
    	}
    
    }
    This the part in which I try to find the user's choice (which I called numberOfNumbers)
    as a digit in the integers (which are in FileOne). My problem is (I think) that first the
    integers have to be 'chopped' into seperate digits before I can read them this way
    (or am I completely wrong?)

    Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Chopping integers into seperate digits.

    the integers have to be 'chopped' into seperate digits
    If you read the user's input as a String instead of an int then you can easily look at each character in the input.

    Try debugging the code by printing out the value of symbol every time a new value is put in it.
    Last edited by Norm; 09-29-2012 at 03:10 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sjoelsteen156 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Chopping integers into seperate digits.

    Thanks for your reply.
    I transformed the integers from the file into Strings in an other file and I changed the user's input to a String.
    I guess I'm still doing something completely wrong because the current code still does not work..
    Java Code:
    import java.util.Scanner;
    import java.io.FileNotFoundException;
    import java.io.File;
    import static java.lang.System.out;
    import java.io.PrintStream;
    
    public class ReadingFile {
    	private Scanner diskScanner;
    	public char symbol;
    	public int counter = 0;
    	public String integerAsString;
    	public String numberAsString;
    	public int a;
    	private PrintStream diskWriter;
    	
    	public void integersToString () throws FileNotFoundException {
    		diskWriter = new PrintStream("FileTwo");
    		diskScanner = new Scanner(new File("FileOne"));
    		while (diskScanner.hasNext()) {
    			a = diskScanner.nextInt();
    			integerAsString = String.valueOf(a);
    			diskWriter.println(integerAsString);
    		}
    		
    	}
    
    	public void checkingForNumber(int numberOfNumbers) throws FileNotFoundException {
    		diskScanner = new Scanner(new File("FileTwo"));
    		numberAsString = String.valueOf(numberOfNumbers);
    		symbol = diskScanner.findWithinHorizon(".",0).charAt(0);
    		while (diskScanner.hasNext()) {
    			symbol = diskScanner.findWithinHorizon(".",0).charAt(0);
    			if (symbol == numberAsString) {
    				counter++;
    			}
    		}
    		out.println("Your number as a digit occurs " + counter + " times in the file.");
    	}
    
    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Chopping integers into seperate digits.

    You may be confusing the char '1' with an int 1 and with the String "1". They are all different.
    You need to make sure you are comparing two variables of the same type: char, int or String.
    This if compares a char to a String:
    Java Code:
    if (symbol == numberAsString)
    That test will never be true. A char is a primitive and a String is an object.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    sjoelsteen156 is offline Member
    Join Date
    Sep 2012
    Posts
    6
    Rep Power
    0

    Default Re: Chopping integers into seperate digits.

    But how can I solve the problem here? Because since
    Java Code:
    symbol = diskScanner.findWithinHorizon(".",0).charAt(0);
    symbol will always be a char and the integers (or integers as
    Strings) consist of multiple digits, so they cant be chars...
    (I really have no idea of how to solve this...)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default Re: Chopping integers into seperate digits.

    You need to convert one of them to be the same type as the other.
    The Integer class has a method to convert a String to an int.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. need to seperate date from a string
    By vivek chhabra in forum New To Java
    Replies: 4
    Last Post: 07-09-2012, 03:53 AM
  2. Replies: 3
    Last Post: 10-27-2011, 05:23 PM
  3. Replies: 1
    Last Post: 01-19-2011, 09:23 PM
  4. Conditions through seperate classes
    By LennyKosmos in forum New To Java
    Replies: 9
    Last Post: 09-22-2010, 04:10 PM
  5. Manipulating components of a seperate GUI Class
    By ribbs2521 in forum New To Java
    Replies: 1
    Last Post: 10-20-2009, 10: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
  •