读入挂——java


从大佬那里copy来的读入挂


若有不对的地方,还请不吝赐教:
首先是StreamTokenizer:
这个是我平常除了Scanner之外用到的最多的了,但这个有一点不好的就是,如果读入整数的话只能用nval()方法,然后再强转一下,再者就是读入数组的话也会有不方便的地方,这个的不方便之处就是,如果,in.nextToken();放的位置不正确的话是极有可能读入一个空的数组的,这时候就需要去判断应不应该插入in.nextToken();,或者是应在哪个地方插入in.nextToken();
最重要的就是每次读入之后,都不要忘记加in.nextToken();还有就是在Scanner方法中用hasnext()方法判断是否读完,而在此方法中用TT_EOF还是TT_EOL判断,我也不是太清楚,各位小朋友可以自己探索一下;
再次列举几个常用的方法:

StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int a=(int) in.nval;   //输入数字;
in.nextToken();
String str=in.sval;
in.nextToken();

其次是自定义的:(这个真的就是完完全全copy的大佬的了,就不再多言了)

package 参考资料;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.InputMismatchException;
import java.util.Scanner;

public class 读入挂 {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		InputReader in=new InputReader(System.in);
		int T=in.nextInt();
		while(T-->0) {
			int n=in.nextInt();
			int temp;
			long sum=0;
			int max=0;
			for(int i=0;i<n;i++) {
				temp=in.nextInt();
				sum+=temp;
				if(temp>max) {
					max=temp;
				}
			}
			System.out.println(sum-max);
		}	
	}

}
class InputReader{
	private final InputStream stream;
	private final byte[] buf=new byte[8192];
	private int curChar,snumChars;
	public InputReader(InputStream st) {
		this.stream=st;
	}
	public int read() {
		if(snumChars==-1)
			throw new InputMismatchException();
		if(curChar>=snumChars) {
			curChar=0;
			try {
				snumChars=stream.read(buf);
			}catch (IOException e) {
				// TODO: handle exception
				throw new InputMismatchException();
			}
			if(snumChars<=0)
				return -1;
		}
		return buf[curChar++];
	}
	public int nextInt() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		int sgn=1;
		if(c=='-') {
			sgn=-1;
			c=read();
		}
		int res=0;
		do {
			res*=10;
			res+=c-'0';
			c=read();
		}while(!isSpaceChar(c));
		return res*sgn;
	}
	public long nextLong() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		int sgn=1;
		if(c=='-') {
			sgn=-1;
			c=read();
		}
		int res=0;
		do {
			res*=10;
			res+=c-'0';
			c=read();
		}while(!isSpaceChar(c));
		return res*sgn;
	}
	public int[] nextIntArray(int n) {
		int a[]=new int[n];
		for(int i=0;i<n;i++) {
			a[i]=nextInt();
		}
		return a;
	}
	public String readString() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		StringBuilder res=new StringBuilder();
		do {
			res.appendCodePoint(c);
			c=read();
		}while(!isSpaceChar(c));
		return res.toString();
	}
	public String nextLine() {
		int c=read();
		while(isSpaceChar(c)) {
			c=read();
		}
		StringBuilder res=new StringBuilder();
		do {
			res.appendCodePoint(c);
			c=read();
		}while(!isEndOfLine(c));
		return res.toString();
	}
	private boolean isEndOfLine(int c) {
		// TODO Auto-generated method stub
		return c=='\n'||c=='\r'||c==-1;
	}
	private boolean isSpaceChar(int c) {
		// TODO Auto-generated method stub
		return c==' '||c=='\n'||c=='\r'||c=='\t'||c==-1;
	}
}

接下来是BufferedReader,这个就只有read()和readline()两种方法;但是平常用的最多的貌似是readLine();这个需要分割转化为数组,然后再依次取其中的内容
下面附上代码:

	BufferedReader buff=new BufferedReader(new InputStreamReader(System.in));
		String s=buff.readLine();
	String[] ss=s.split(" ");
	int arr[]=new int[100];
		int a=Integer.parseInt(s);

接下来提一下快速输出PrintWriter,这个就是输出了,用法和System.out差不多,就是去掉System。后面要刷新和关闭,这个不能忘了,否则你可能会得不到你想要的结果哦;

PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
out.println();
out.flush();
out.close();

到这里就结束了!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值