# Thread: Chopping integers into seperate digits.

1. Member
Join Date
Sep 2012
Posts
6
Rep Power
0

## 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;

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?)

2. ## 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.

3. Member
Join Date
Sep 2012
Posts
6
Rep Power
0

## Re: Chopping integers into seperate digits.

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;

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

5. Member
Join Date
Sep 2012
Posts
6
Rep Power
0

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

#### Posting Permissions

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