Java Code Examples for java.util.Scanner#nextLong()

The following examples show how to use java.util.Scanner#nextLong() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeapHistogramImpl.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
HeapHistogramImpl(String histogramText) {
    Map<String,ClassInfoImpl> classesMap = new HashMap(1024);
    Map<String,ClassInfoImpl> permGenMap = new HashMap(1024);
    time = new Date();
    Scanner sc = new Scanner(histogramText);
    sc.useRadix(10);
    while(!sc.hasNext("-+")) {
        sc.nextLine();
    }
    sc.skip("-+");
    sc.nextLine();


    while(sc.hasNext("[0-9]+:")) {  // NOI18N
        ClassInfoImpl newClInfo = new ClassInfoImpl(sc);
        storeClassInfo(newClInfo, classesMap);
        totalHeapBytes += newClInfo.getBytes();
        totalHeapInstances += newClInfo.getInstancesCount();
    }
    sc.next("Total");   // NOI18N
    totalInstances = sc.nextLong();
    totalBytes = sc.nextLong();
    classes = new HashSet(classesMap.values());
}
 
Example 2
Source File: Taxi_1607.java    From AlgoCS with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    long a, b, x, y, i = 0;
    a = in.nextLong();
    x = in.nextLong();
    b = in.nextLong();
    y = in.nextLong();
    if (a >= b) {
        System.out.println(a);
        return;
    }
    while (a <= b) {
        if (a + x >= b) {
            System.out.println(b);
            return;
        }
        a += x;
        b -= y;
    }
    System.out.println(a);
}
 
Example 3
Source File: DiffCompiler.java    From Universal-FE-Randomizer with MIT License 6 votes vote down vote up
public void addDiffsFromFile(String diffName, long addressOffset) throws IOException {
	InputStream stream = DiffApplicator.class.getClassLoader().getResourceAsStream(diffName + ".diff");
	BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
	String currentLine = bufferedReader.readLine();
	while(currentLine != null) {
		Scanner scanner = new Scanner(currentLine);
		scanner.useDelimiter("[\\s\\W]+");
		long nextAddress = scanner.nextLong(16);
		int existingValue = scanner.nextInt(16);
		int newValue = scanner.nextInt(16);
		
		addDiff(new Diff(nextAddress + addressOffset, 1, new byte[] {(byte)(newValue & 0xFF)}, new byte[] {(byte)(existingValue & 0xFF)}));
		scanner.close();
		currentLine = bufferedReader.readLine();
	}
	
	stream.close();
}
 
Example 4
Source File: TitanRuins_1910.java    From AlgoCS with MIT License 6 votes vote down vote up
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    long n = in.nextLong(), x = 0, max = 0, s = 0;
    int a[] = new int[(int) n];
    for (int i = 0; i < a.length; i++) {
        a[i] = in.nextInt();
    }
    if (n >= 3) {
        for (int i = 1; i < a.length - 1; i++) {
            s = a[i] + a[i - 1] + a[i + 1];
            if (s > max) {
                max = s;
                x = i;
            }
        }
    }
    System.out.print(max + " " + (x + 1));
}
 
Example 5
Source File: DepartmentManager.java    From ctsms with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void interactiveCreateUser() {
	Scanner in = ExecUtil.getScanner();
	try {
		printDepartments();
		System.out.print("enter department id for new user:");
		Long departmentId = in.nextLong();
		in.nextLine();
		String plainDepartmentPassword = ExecUtil.readPassword(in, "enter department password:");
		System.out.print("enter new username:");
		String username = in.nextLine();
		System.out.print("enter user language [" + getDefaultLocale() + "]:");
		UserInVO newUser = getNewUser(departmentId, username, in.nextLine());
		printPasswordPolicy();
		PasswordInVO newPassword = getNewPassword(ExecUtil.readPassword(in, "enter desired user password:"));
		printPermissionProfiles();
		System.out.print("enter separated list of permission profiles:");
		ArrayList<PermissionProfile> profiles = checkProfileList(in.nextLine());
		UserOutVO user = toolsService.addUser(newUser, newPassword, plainDepartmentPassword);
		System.out.println("user created");
		addUserPermissionProfiles(user, profiles);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		in.close();
	}
}
 
Example 6
Source File: SchemaBuilder.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);

    return sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 7
Source File: Exercise_06_25.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	Scanner input = new Scanner(System.in); // Create a scanner

	// Prompt the user to enter milliseconds
	System.out.print("Enter milliseconds: ");
	long millis = input.nextLong();

	// Convert milliseconds to hours, minutes, and seconds
	System.out.println("hours:minuties:seconds: " + convertMillis(millis));
}
 
Example 8
Source File: HeapHistogramImpl.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
ClassInfoImpl(Scanner sc) {
    String jvmName;

    sc.next();
    instances = sc.nextLong();
    bytes = sc.nextLong();
    jvmName = sc.next();
    sc.nextLine();  // skip module name on JDK 9
    name = convertJVMName(jvmName);
}
 
Example 9
Source File: AVeryBigSum.java    From Hackerrank-Solutions with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	int n = in.nextInt();
	long sum = 0;

	for (int arr_i = 0; arr_i < n; arr_i++) {
		sum += in.nextLong();
	}
	System.out.println(sum);
	in.close();

}
 
Example 10
Source File: StrangeCounter.java    From Hackerrank-Solutions with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	long n = sc.nextLong();
	long i = 0;
	long lValue = (long) (3 * Math.pow(2, i)) - 2;
	long hValue = 2 * lValue + 1;

	while (hValue < n) {
		lValue = (long) (3 * Math.pow(2, ++i)) - 2;
		hValue = 2 * lValue + 1;
	}
	System.out.println(hValue + 1 - n);
	sc.close();
}
 
Example 11
Source File: LargestPrimeDivisor.java    From interviews with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	Scanner input = new Scanner(System.in);
	long number = input.nextLong();
	while (number != 0) {
		number = (long) (Math.abs(number));
		long largestPrimeDivisor = -1;
		int numberOfPrimeDivisors = 0;
		int sqrtOfNumber = (int) (Math.sqrt(number));
		for (int i = 2; i <= sqrtOfNumber; i++) {
			if (number % i == 0) {
				numberOfPrimeDivisors++;
				largestPrimeDivisor = i;
				while (number % i == 0) {
					number = number / i;
				}
			}
		}
		if (largestPrimeDivisor != -1 && number != 1) {
			System.out.println(number);
		} else if (numberOfPrimeDivisors <= 1) {
			System.out.println(-1);
		} else {
			System.out.println(largestPrimeDivisor);
		}
		number = input.nextLong();
	}
}
 
Example 12
Source File: Exercise_06_02.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
/** Main Method */
public static void main(String[] args) {
	Scanner input = new Scanner(System.in); // Create a Scanner

	// Prompt the user to enter an integer
	System.out.print("Enter a integer: ");
	long number = input.nextLong();

	// Display the sum of all the digits in the integer
	System.out.println("The sum of the digits in " + number + " is " +
		sumDigits(number));
}
 
Example 13
Source File: DataCubeDimension.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param val
 * @return
 * @throws IllegalArgumentException
 */
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);
    return sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 14
Source File: DataCubeColumn.java    From db with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 *
 * @param val
 * @return
 * @throws IllegalArgumentException
 */
public Object interpret(Object val) throws IllegalArgumentException {
    String s = val.toString();
    Scanner sc = new Scanner(s);
    return sc.hasNextLong() ? sc.nextLong()
            : sc.hasNextInt() ? sc.nextInt()
            : sc.hasNextDouble() ? sc.nextDouble()
            : sc.hasNextBoolean() ? sc.nextBoolean()
            : sc.hasNextBigInteger() ? sc.nextBigInteger()
            : sc.hasNextFloat() ? sc.nextFloat()
            : sc.hasNextByte() ? sc.nextByte()
            : sc.hasNext() ? sc.next()
            : s;
}
 
Example 15
Source File: Exercise_06_22.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
/** Main Method */
public static void main(String[] args) {
	Scanner input = new Scanner(System.in); // Create a Scanner

	// Prompt the user to enter an integer
	System.out.print("Enter a number: ");
	long number = input.nextLong();

	// Display the square root
	System.out.println(
		"The approximated square root of " + number + " is: " + sqrt(number));
}
 
Example 16
Source File: NumberUtils.java    From sherlock with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param str string to check
 * @return whether the string is a long value
 */
public static boolean isLong(String str) {
    if (str == null) {
        return false;
    }
    Scanner s = new Scanner(str.trim());
    if (!s.hasNextLong()) {
        return false;
    }
    s.nextLong();
    return !s.hasNext();
}
 
Example 17
Source File: Exercise_18_11.java    From Intro-to-Java-Programming with MIT License 5 votes vote down vote up
/** Main method */
public static void main(String[] args) {
	// Create a Scanner
	Scanner input = new Scanner(System.in);

	// Prompt the user to enter an integer
	System.out.print("Enter an integer: ");
	long n = input.nextLong();

	// Display the sum
	System.out.println("The sum of " + n + " = " + sumDigits(n));
}
 
Example 18
Source File: 10755 Garbage Heap.java    From UVA with GNU General Public License v3.0 4 votes vote down vote up
public static void main (String [] args) throws Exception {
	Scanner sc=new Scanner(System.in);
	int testCaseCount=sc.nextInt();
	for (int testCase=0;testCase<testCaseCount;testCase++) {
		int a=sc.nextInt();
		int b=sc.nextInt();
		int c=sc.nextInt();
		
		long [][][] v=new long [a][b][c];
		for (int i=0;i<a;i++) for (int i2=0;i2<b;i2++) for (int i3=0;i3<c;i3++) v[i][i2][i3]=sc.nextLong();
		
		long [][][] dp=new long [a][b][c];
		for (int i=0;i<a;i++) for (int i2=0;i2<b;i2++) for (int i3=0;i3<c;i3++) {
			dp[i][i2][i3]=v[i][i2][i3];
			if (i>0) dp[i][i2][i3]+=dp[i-1][i2][i3];
			if (i2>0) dp[i][i2][i3]+=dp[i][i2-1][i3];
			if (i3>0) dp[i][i2][i3]+=dp[i][i2][i3-1];
			
			if (i>0 && i2>0) dp[i][i2][i3]-=dp[i-1][i2-1][i3];
			if (i2>0 && i3>0) dp[i][i2][i3]-=dp[i][i2-1][i3-1];
			if (i>0 && i3>0) dp[i][i2][i3]-=dp[i-1][i2][i3-1];
			
			if (i>0 && i2>0 && i3>0) dp[i][i2][i3]+=dp[i-1][i2-1][i3-1];
		}
		
		long max=Long.MIN_VALUE;
		for (int i=0;i<a;i++) for (int i2=0;i2<b;i2++) for (int i3=0;i3<c;i3++) {
			for (int j=i;j<a;j++) for (int j2=i2;j2<b;j2++) for (int j3=i3;j3<c;j3++) {
				long curr=dp[j][j2][j3];
				
				if (i>0) curr-=dp[i-1][j2][j3];
				if (i2>0) curr-=dp[j][i2-1][j3];
				if (i3>0) curr-=dp[j][j2][i3-1];
				
				if (i>0 && i2>0) curr+=dp[i-1][i2-1][j3];
				if (i2>0 && i3>0) curr+=dp[j][i2-1][i3-1];
				if (i>0 && i3>0) curr+=dp[i-1][j2][i3-1];
				
				if (i>0 && i2>0 && i3>0) curr-=dp[i-1][i2-1][i3-1];

				max=Math.max(curr, max);
			}
		}
		
		if (testCase>0) System.out.println();
		System.out.println(max);
	}
}
 
Example 19
Source File: AvroRecordConverter.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Converts the data from one schema to another. If the types are the same,
 * no change will be made, but simple conversions will be attempted for
 * other types.
 *
 * @param content
 *            The data to convert, generally taken from a field in an input
 *            Record.
 * @param inputSchema
 *            The schema of the content object
 * @param outputSchema
 *            The schema to convert to.
 * @return The content object, converted to the output schema.
 * @throws AvroConversionException
 *             When conversion is impossible, either because the output type
 *             is not supported or because numeric data failed to parse.
 */
private Object convertData(Object content, Schema inputSchema,
        Schema outputSchema) throws AvroConversionException {
    if (content == null) {
        // No conversion can happen here.
        if (supportsNull(outputSchema)) {
            return null;
        }
        throw new AvroConversionException("Output schema " + outputSchema
                + " does not support null");
    }

    Schema nonNillInput = getNonNullSchema(inputSchema);
    Schema nonNillOutput = getNonNullSchema(outputSchema);
    if (nonNillInput.getType().equals(nonNillOutput.getType())) {
        return content;
    } else {
        if (nonNillOutput.getType() == Schema.Type.STRING) {
            return content.toString();
        }

        // For the non-string cases of these, we will try to convert through
        // string using Scanner to validate types. This means we could
        // return questionable results when a String starts with a number
        // but then contains other content
        Scanner scanner = new Scanner(content.toString());
        scanner.useLocale(locale);
        switch (nonNillOutput.getType()) {
        case LONG:
            if (scanner.hasNextLong()) {
                return scanner.nextLong();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to long");
            }
        case INT:
            if (scanner.hasNextInt()) {
                return scanner.nextInt();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to int");
            }
        case DOUBLE:
            if (scanner.hasNextDouble()) {
                return scanner.nextDouble();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to double");
            }
        case FLOAT:
            if (scanner.hasNextFloat()) {
                return scanner.nextFloat();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to float");
            }
        default:
            throw new AvroConversionException("Cannot convert to type "
                    + nonNillOutput.getType());
        }
    }
}
 
Example 20
Source File: AvroRecordConverter.java    From nifi with Apache License 2.0 4 votes vote down vote up
/**
 * Converts the data from one schema to another. If the types are the same,
 * no change will be made, but simple conversions will be attempted for
 * other types.
 *
 * @param content
 *            The data to convert, generally taken from a field in an input
 *            Record.
 * @param inputSchema
 *            The schema of the content object
 * @param outputSchema
 *            The schema to convert to.
 * @return The content object, converted to the output schema.
 * @throws AvroConversionException
 *             When conversion is impossible, either because the output type
 *             is not supported or because numeric data failed to parse.
 */
private Object convertData(Object content, Schema inputSchema,
        Schema outputSchema) throws AvroConversionException {
    if (content == null) {
        // No conversion can happen here.
        if (supportsNull(outputSchema)) {
            return null;
        }
        throw new AvroConversionException("Output schema " + outputSchema
                + " does not support null");
    }

    Schema nonNillInput = getNonNullSchema(inputSchema);
    Schema nonNillOutput = getNonNullSchema(outputSchema);
    if (nonNillInput.getType().equals(nonNillOutput.getType())) {
        return content;
    } else {
        if (nonNillOutput.getType() == Schema.Type.STRING) {
            return content.toString();
        }

        // For the non-string cases of these, we will try to convert through
        // string using Scanner to validate types. This means we could
        // return questionable results when a String starts with a number
        // but then contains other content
        Scanner scanner = new Scanner(content.toString());
        scanner.useLocale(locale);
        switch (nonNillOutput.getType()) {
        case LONG:
            if (scanner.hasNextLong()) {
                return scanner.nextLong();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to long");
            }
        case INT:
            if (scanner.hasNextInt()) {
                return scanner.nextInt();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to int");
            }
        case DOUBLE:
            if (scanner.hasNextDouble()) {
                return scanner.nextDouble();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to double");
            }
        case FLOAT:
            if (scanner.hasNextFloat()) {
                return scanner.nextFloat();
            } else {
                throw new AvroConversionException("Cannot convert "
                        + content + " to float");
            }
        default:
            throw new AvroConversionException("Cannot convert to type "
                    + nonNillOutput.getType());
        }
    }
}