Results 1 to 2 of 2
  1. #1
    justme is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default What is end of input character?

    Hi there!
    What is end of input character? I wanted to read in input from standard input and find a max but i don't know it does not work. Here is my code:
    Java Code:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class maxNum {
    	public static void main(String[] args) throws IOException{
    		BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
    		ArrayList<Integer> arr= new ArrayList<Integer>();
    	//	String s;
    		//Integer i=0;
    		while(buff.read()!=-1){
    			arr.add(buff.read());
    		}
    		Integer max=0;
    		for(int j=0; j<arr.size(); j++){
    			max=Math.max(max, arr.get(j));
    		}
    		System.out.println(max);
    	}
    }
    Any help? thanks in advance!

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,838
    Rep Power
    19

    Default Re: What is end of input character?

    Java Code:
    while(buff.read()!=-1) {
       arr.add(buff.read());
    }
    You read twice. That means you are only actually reading half the bytes into your array.
    You need to assign that first read to a variable and then add that variable to your array.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2012, 02:29 PM
  2. Replies: 0
    Last Post: 11-06-2012, 02:09 PM
  3. Replies: 0
    Last Post: 11-06-2012, 02:09 PM
  4. Replies: 2
    Last Post: 08-21-2010, 10:53 AM
  5. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM

Posting Permissions

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