本文整理汇总了Java中java.util.Scanner类的典型用法代码示例。如果您正苦于以下问题:Java Scanner类的具体用法?Java Scanner怎么用?Java Scanner使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Scanner类属于java.util包,在下文中一共展示了Scanner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getResponseFromHttpUrl
import java.util.Scanner; //导入依赖的package包/类
/**
* This method returns the entire result from the HTTP response.
*
* @param url The URL to fetch the HTTP response from.
* @return The contents of the HTTP response.
* @throws IOException Related to network and stream reading
*/
public static String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
} finally {
urlConnection.disconnect();
}
}
示例2: main
import java.util.Scanner; //导入依赖的package包/类
public static void main(String[] args) {
//get input
Scanner sc = new Scanner(System.in).useDelimiter(" ");
ArrayList<Integer> input = new ArrayList<>();
//add input to ArrayList
while (sc.hasNextInt()) {
input.add(sc.nextInt());
}
//sort the list
Collections.sort(input);
//initialize closest values, with the first two elements in the list
int closest = input.get(1)-input.get(0);
//check all elements in the list
for(int i = 2; i < input.size(); i++) {
//change closest if we find two elements with a shorter distance.
closest = Math.min(closest, input.get(i)-input.get(i-1));
}
//print the shortest distance
System.out.println(closest);
}
示例3: secondList
import java.util.Scanner; //导入依赖的package包/类
public static ArrayList<String[]> secondList(){
try{
Scanner scannerSongs = new Scanner(new File("Recommended_db.csv"));
ArrayList<String[]> allSongs = new ArrayList<>();
scannerSongs.nextLine();
while (scannerSongs.hasNextLine()) {
String line = scannerSongs.nextLine();
String[] columns = line.split(",");
String[] songInfo = {columns[3],columns[4],null};
allSongs.add(songInfo);
}
return allSongs;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
示例4: main
import java.util.Scanner; //导入依赖的package包/类
public static void main(String[] args) {
// entrada = 3 notas
// Seria o prompt do java script
Scanner sc = new Scanner(System.in);
System.out.println("Digite o valor da nota 1:");
double nota1 = sc.nextDouble();
System.out.println("Digite o valor da nota 2:");
double nota2 = sc.nextDouble();
System.out.println("Digite o valor da nota 3:");
double nota3 = sc.nextDouble();
// processamento = calcular media
double media = (nota1 + nota2 + nota3)/3;
// saida = condicao do aluno (Reprovado, aprovado, recuperacao)
if (media <= 5) {
System.out.println("Reprovado");
} else if (media > 5 && media < 7) {
System.out.println("Recuperacao");
} else {
System.out.println("Aprovado");
}
}
示例5: getResponseFromHttpUrl
import java.util.Scanner; //导入依赖的package包/类
public static String getResponseFromHttpUrl(String url) throws IOException {
URL urlObject = new URL(url);
HttpURLConnection urlConnection =
(HttpURLConnection) urlObject.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
if (hasInput) {
return scanner.next();
} else {
return null;
}
} finally {
urlConnection.disconnect();
}
}
示例6: main
import java.util.Scanner; //导入依赖的package包/类
public static void main(String[] args) {
Scanner x = new Scanner(System.in);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
int n = x.nextInt(), c;
int a[] = new int[701];
for (int i = 0; i < n; i++) {
c = x.nextInt();
a[c]++;
}
int s = 0;
for (int i = 600; i < a.length; i++) {
s += a[i] / 4;
}
writer.print(s);
writer.close();
}
示例7: main
import java.util.Scanner; //导入依赖的package包/类
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
Byte n = in.nextByte();
double ans = 0;
double a[] = new double[n];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
ans = a[a.length - 1];
for (int i = a.length - 1; i > 0; i--) {
ans = Max(ans, a[i - 1]);
}
System.out.printf("%.2f", ans);
}
示例8: createTimeIntervalFromString
import java.util.Scanner; //导入依赖的package包/类
/**
* @generated NOT
*/
@Override
public TimeInterval createTimeIntervalFromString(final String value) {
final TimeIntervalImpl anInterval = new TimeIntervalImpl();
final Scanner scan = new Scanner(value);
final String leftPar = scan.findInLine("\\]|\\[");
anInterval.setMinOpen("]".equals(leftPar));
final String leftStr = scan.findInLine("[^,]*");
anInterval.setMin(createDurationFromString(leftStr));
scan.findInLine(",");
String rightStr = scan.findInLine("[^\\]\\[]*");
if (rightStr == null) {
rightStr = leftStr;
}
anInterval.setMax(createDurationFromString(rightStr));
final String rightPar = scan.findInLine("\\]|\\[");
anInterval.setMaxOpen("[".equals(rightPar));
scan.close();
return anInterval;
}
示例9: getResponseFromHttpUrl
import java.util.Scanner; //导入依赖的package包/类
/**
* This method returns the entire result from the HTTP response.
*
* @param url The URL to fetch the HTTP response from.
* @return The contents of the HTTP response, null if no response
* @throws IOException Related to network and stream reading
*/
public static String getResponseFromHttpUrl(URL url) throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = urlConnection.getInputStream();
Scanner scanner = new Scanner(in);
scanner.useDelimiter("\\A");
boolean hasInput = scanner.hasNext();
String response = null;
if (hasInput) {
response = scanner.next();
}
scanner.close();
return response;
} finally {
urlConnection.disconnect();
}
}
示例10: main
import java.util.Scanner; //导入依赖的package包/类
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = in.nextInt();
int reserve = 0;
int result = 0;
int temp;
for (int i = 0; i < number; i++) {
temp = in.nextInt();
if (temp == -1 && reserve > 0)
reserve--;
else if (temp == -1 && reserve == 0)
result++;
else if (temp > 0)
reserve += temp;
}
System.out.print(result);
}
示例11: startCommandHandlerOnNewThread
import java.util.Scanner; //导入依赖的package包/类
private void startCommandHandlerOnNewThread()
{
Log.info("NOTE: you can now type commands here.");
CommandHandler commandHandler = new CommandHandler();
Runnable commandHandlingTask = () ->
{
try (Scanner scanner = new Scanner(System.in))
{
while (true)
{
String command = scanner.nextLine();
commandHandler.handle(command);
}
}
};
new Thread(commandHandlingTask).start();
}
示例12: getVersionString
import java.util.Scanner; //导入依赖的package包/类
/**
* Gets the OS version string from a Linux release-specific file.
*/
private String getVersionString(File f){
try {
Scanner sc = new Scanner(f);
return sc.findInLine("(\\d)+((\\.)(\\d)+)*");
}
catch (Exception e){
}
return null;
}
示例13: main
import java.util.Scanner; //导入依赖的package包/类
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int length = in.nextInt();
int speed1 = in.nextInt();
int speed2 = in.nextInt();
double time = (double) length / (speed1 + speed2);
System.out.print(time * speed1);
}
示例14: PartitaOfflineConsoleView
import java.util.Scanner; //导入依赖的package包/类
/**
*
* @param model modello partita offline
*/
public PartitaOfflineConsoleView(PartitaOfflineModel model) {
this.listeners = new CopyOnWriteArrayList<>();
this.model = model;
this.model.addObserver(this);
scanner = new Scanner(System.in);
}
示例15: handleXmlResults
import java.util.Scanner; //导入依赖的package包/类
/**
* Handles Xml file and public dejagnu test results from this.
*
* @param xmlFile file with test results
* @return count of public xmlFile has been handled
*/
int handleXmlResults(@NotNull final File xmlFile){
myTestsCount = 0;
try {
String xmlEntry = new Scanner(xmlFile, "UTF-8").useDelimiter("\\A").next();
if (myNeedReplaceApm){
xmlEntry = replaceAmp(replaceXmlHeaderVersion(xmlEntry));
}
if (myNeedToReplaceControls){
xmlEntry = replaceControlChars(xmlEntry);
}
parseXmlTestResults(xmlEntry, xmlFile.getAbsolutePath());
return myTestsCount;
}
catch (final FileNotFoundException ignored){
//e.getMessage();
return 0;
}
}