当前位置: 首页>>代码示例>>Java>>正文


Java Scanner类代码示例

本文整理汇总了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();
    }
}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:26,代码来源:NetworkUtils.java

示例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);
}
 
开发者ID:BaReinhard,项目名称:Hacktoberfest-Data-Structure-and-Algorithms,代码行数:21,代码来源:Closest.java

示例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;
    }
 
开发者ID:prakamya-mishra,项目名称:Shield,代码行数:20,代码来源:Functions.java

示例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");
	}
}
 
开发者ID:alexferreiradev,项目名称:3way_laboratorios,代码行数:24,代码来源:Ex4.java

示例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();
        }
    }
 
开发者ID:MrNo-Body,项目名称:ecam-app-android,代码行数:24,代码来源:NetworkUtils.java

示例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();
}
 
开发者ID:bakhodirsbox,项目名称:AlgoCS,代码行数:17,代码来源:FlatSpots_1617.java

示例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);
}
 
开发者ID:bakhodirsbox,项目名称:AlgoCS,代码行数:17,代码来源:Stripies_1161.java

示例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;
}
 
开发者ID:polarsys,项目名称:time4sys,代码行数:23,代码来源:NfpFactoryImpl.java

示例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();
    }
}
 
开发者ID:fjoglar,项目名称:android-dev-challenge,代码行数:27,代码来源:NetworkUtils.java

示例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);
}
 
开发者ID:AlbertHambardzumyan,项目名称:codeforces,代码行数:20,代码来源:Problem_427A.java

示例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();
}
 
开发者ID:MMORPG-Prototype,项目名称:MMORPG_Prototype,代码行数:18,代码来源:GameServer.java

示例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;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:FcFontConfiguration.java

示例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);
}
 
开发者ID:AlbertHambardzumyan,项目名称:codeforces,代码行数:10,代码来源:Problem_591A.java

示例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);
}
 
开发者ID:IngSW-unipv,项目名称:Progetto-A,代码行数:11,代码来源:PartitaOfflineConsoleView.java

示例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;
  }
}
 
开发者ID:unix-junkie,项目名称:teamcity-autotools-plugin,代码行数:25,代码来源:DejagnuTestsXMLParser.java


注:本文中的java.util.Scanner类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。