Results 1 to 4 of 4
  1. #1
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Question 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:-

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  3. #3
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Thanx for the help i just cant work out a way of putting it in the code.
    -Long time no c-
    -:eek:-

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  1. Store textfile data in an array
    By mokonji in forum New To Java
    Replies: 4
    Last Post: 02-22-2009, 05:28 PM
  2. Display the data in Descending order
    By santanu in forum New To Java
    Replies: 2
    Last Post: 10-31-2008, 12:06 PM
  3. Array of different data types?
    By venkatteshb in forum New To Java
    Replies: 1
    Last Post: 08-27-2008, 05:42 PM
  4. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 07:55 AM
  5. Comparing types, integer with null
    By Felissa in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:32 AM

Posting Permissions

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