Results 1 to 6 of 6

Thread: Help me please

  1. #1
    Cassey is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default Help me please

    How to solve the problem for java?
    Help me please)

    Introduced an array of 0 and 1. Series is a sequence of consecutive 0 or 1. How many episodes in this array.
    For example:
    0100011101
    3

    I wrote this code, but I do not like it! how can I fix it? Need to value input from the console
    thanks.


    This is me code
    Java Code:
    public class Ser {
    public static void main (String [] args)
    {
    int[] a = {0,1,0,1,0,1,0,0,1};//it is should be from console
    int count=0;
    for(int i = 0; i < a.length-1; i++) {
    if(a[i] == 0 && a[i + 1] == 1) {
    count++;
    }
    
    }
    System.out.println(count);
    }
    }

  2. #2
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I really dont understand a word from your english
    especialy when you do not use questionmarks ("?") for your questions.

    What it seems like you want to do is to find how many times "01" appears in your code.

    Heres the working one then:
    PHP Code:
    public class Ser {
    	public static void main (String [] args)
    	{
    		int[] a = {0,1,0,1,0,1,0,0,1};//it is should be from console
    		int count=0;
    		for(int i = 0; i < a.length; i++) {
    			if(a[i] == 0 && a[i + 1] == 1) {
    				count++;
    			}
    		}
    		System.out.println(count);
    	}
    }

  3. #3
    Cassey is offline Member
    Join Date
    Dec 2010
    Posts
    2
    Rep Power
    0

    Default

    hello!
    thank you for answer!
    i just need to change this code. int[] a = {0,1,0,1,0,1,0,0,1};
    array should be given from file, i do not know why but i could not do it for this program
    How to read array from file ? -this is my question
    I need to use something like this ?
    PHP Code:
    FileInputStream str = new FileInputStream("array.txt");
            DataInputStream pos = new DataInputStream(str);
            BufferedReader n = new BufferedReader(new InputStreamReader(pos));

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Cassey View Post
    hello!
    thank you for answer!
    i just need to change this code. int[] a = {0,1,0,1,0,1,0,0,1};
    array should be given from file, i do not know why but i could not do it for this program
    How to read array from file ? -this is my question
    I need to use something like this ?
    PHP Code:
    FileInputStream str = new FileInputStream("array.txt");
            DataInputStream pos = new DataInputStream(str);
            BufferedReader n = new BufferedReader(new InputStreamReader(pos));

    you can't use a StreamReader in the constructor of BufferedReader. if your file has the extension txt i guess you should use a Reader like BufferedReader in = new BufferedReader(new FileReader("foo.in")).

  5. #5
    demonick is offline Member
    Join Date
    Dec 2010
    Posts
    27
    Rep Power
    0

    Default

    Java Code:
        try
        {
        Scanner scanner = new Scanner(new FileReader("zeroone.txt"));
        try {
          while ( scanner.hasNextLine() ){
               stringToCheck =+ scanner.next();
          }
        }
        finally {
          scanner.close();
        }   
      } catch (FileNotFoundException fnf){System.out.println("File not found");}
    Put this in a method that returns a String - stringToCheck and loop trough the String.
    Your code would probably work, too, can't remember what DataInputStream does.

  6. #6
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    I think he might want not to read the contents in the txt file, but read the txt file in binary!
    Maybe.. Otherwise those BufferedReader is the way to go :D

Posting Permissions

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