Results 1 to 4 of 4
- 05-01-2009, 09:38 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
Comparing two data types in order to store in array
I am experiencing a problem in some code i am writing in that it does not see when current "== j" and therefore does not store anything in the array.
i have tried casting current which is a char but they don't seem to be matching does anyone know what im doing wrong?
[CODE]
for (int j = 0; j < 50; j++) {
if ((int) current == j) {
num[j]++;
}
else other++;
}/CODE]
full code:
Java Code:package ArraysStorage; import java.util.Scanner; public class NumberCount { public static void main(String[] args) { Scanner scan = new Scanner(System.in); char current = '0'; int other = 0; String input; int[] num = new int[50]; //initialise array for (int i = 0; i < num.length; i++) { // fill array with 0's num[i] = 0; } System.out.println("Please enter a sequence of numbers from 0-50: "); input = scan.nextLine(); //getting user input for (int i = 0; i < (input.length()); i++) { current = input.charAt(i); for (int j = 0; j < 50; j++) { if ((int) current == j) { num[j]++; } else other++; } } for (int i = 0; i < 50; i++) { System.out.println("Number" + " " + (i+1) + " " + num[i]); } System.out.println(other); } }-Long time no c-
-:eek:-
- 05-01-2009, 09:52 AM #2
Characters are not integers. For example, the character '1' is represented by the char value 49. To get numbers from an input stream use Integer.parseInt().
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-01-2009, 10:25 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
Thanx for the help i just cant work out a way of putting it in the code.
-Long time no c-
-:eek:-
- 05-01-2009, 10:27 AM #4
or try using a Scanner
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
Similar Threads
-
Store textfile data in an array
By mokonji in forum New To JavaReplies: 4Last Post: 02-22-2009, 05:28 PM -
Display the data in Descending order
By santanu in forum New To JavaReplies: 2Last Post: 10-31-2008, 12:06 PM -
Array of different data types?
By venkatteshb in forum New To JavaReplies: 1Last Post: 08-27-2008, 05:42 PM -
Can I use vectors to store multiple types of objects
By Nathand in forum Advanced JavaReplies: 6Last Post: 04-28-2008, 07:55 AM -
Comparing types, integer with null
By Felissa in forum New To JavaReplies: 1Last Post: 07-05-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks