Results 1 to 6 of 6
Thread: Help me please
- 12-07-2010, 09:50 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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); } }
- 12-07-2010, 09:59 AM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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); } }
- 12-07-2010, 10:52 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 2
- Rep Power
- 0
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));
- 12-07-2010, 11:01 AM #4
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")).
- 12-07-2010, 11:03 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 27
- Rep Power
- 0
Put this in a method that returns a String - stringToCheck and loop trough the String.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");}
Your code would probably work, too, can't remember what DataInputStream does.
- 12-07-2010, 07:36 PM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks