java.util.scanner

Regex calendar validation

偶尔善良 提交于 2019-12-13 21:47:24
问题 I am trying to make a program where the user enters a date(any date), for example 29-Jul-1995 . The program should match the date format and validate if this date is correct or not. Here is my code: import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Review1_Main { public static Pattern pattern; public static Matcher matcher; // DATE_PATTERN is a format for user input private static final Pattern DATE_PATTERN = Pattern.compile("(0?[1-9]|[12][0-9]|3

Program for Odd/Even output only giving 1 answer

北战南征 提交于 2019-12-13 21:38:00
问题 I'm stuck on an assignment needs the program to accept multiple numbers and then output "Yes" if odd and "No" if even, and can't figure out how to make the program accept more than 1 int and then output the correct println. This is my code so far. import java.util.Scanner; class odd{ public static void main(String args[]){ Scanner in = new Scanner(System.in); int[] numbers = new int[10]; for(int i = 0; i < 10; ++i) { numbers[i] = in.nextInt(); if(i % 2 == 0 ) System.out.println("Yes"); if( i

Arrays are null. Not getting information from while loop

我的梦境 提交于 2019-12-13 21:32:38
问题 The following snippet of code is supposed to read from a text file, check how many words are in the file, and how many lines there are, and with that information it's supports to sort the nth word into it's appropriate array. protected static void loadAccountInformationFromFile() throws Exception { // These variables control the logic needed to put words in the right array int accountNumberCount = 1; int firstNameCount = 2; int lastNameCount = 3; int balanceCount = 4; int lastVariableCount =

NoSuchElementException in Scanner

眉间皱痕 提交于 2019-12-13 21:22:05
问题 I am working on a kind-of parser (hobby project) which takes a Cpp file, reads through the comments in the file, and then tries to create a header file based on that. The problem I am facing is when the java.util.Scanner is about to read the very first line. The program stops and gives me the NoSuchElementException . I can't really figure out what should be wrong. I checked that both path and pathname are made correctly. The file must be there, and I can read fields on the Scanner object as

How to recognize carriage return using java scanner

南楼画角 提交于 2019-12-13 21:03:55
问题 I am trying to read a CSV file in to my application to read a bunch of data into a table. Every row is separated by commas and each end of the row has a carriage return. I try using the scanner in java to read the first line with delimiter set to (","), but I can't figure out how to stop the scanner at the end of the first row when it reached the carriage return. This is the code I have so far that scans in everything in the file since it doesn't stop at carriage returns. Scanner scn = new

Keep getting NoSuchElementException using Scanner

十年热恋 提交于 2019-12-13 18:39:18
问题 I'm trying to read in string values separated by whitespaces. Once I try to set them to a variable I get a NoSuchElementException Error. I've done similar things before where I had to it with integers instead and never got this error. Doing some research: java.util.NoSuchElementException: Read words from a file: It says that hasNext is implemented to work with next() while hasNextLine is implemented to work with nextLine(), so I tried replacing hasNextLine() with hasNext(), but still nothing.

NoSuchElementException Issue

こ雲淡風輕ζ 提交于 2019-12-13 15:16:12
问题 What I am trying to do is to create a while loop where a program menu is run from until the user inputs a valid response. However the scanner instance I have created does not want to play ball and keeps throwing NoSuchElementExceptions no matter what I have tried. import java.util.Scanner; public class Menu { Menu() { isValid = false; uInput = 0; } public int mMain() { Scanner in2; do { in2 = new Scanner(System.in); //For user input System.out.println("Please choose from the following options

Read integers and strings from a single line of a console

一个人想着一个人 提交于 2019-12-13 15:12:21
问题 The problem is like this: I have two programs which takes input from a console but in different manner: 1) Scanner input = new Scanner(System.in); int temp1 = input.nextInt(); input.nextLine(); String str = input.nextLine(); int temp2 = Integer.parseInt(str); int total = temp1+temp2; System.out.println(total); 2) Scanner input = new Scanner(System.in); int temp1 = input.nextInt(); // input.nextLine(); String str = input.nextLine(); int temp2 = Integer.parseInt(str); int total = temp1+temp2;

Conditional Statement on Java7

橙三吉。 提交于 2019-12-13 11:30:00
问题 Hello i just started a coding exercise on hackerrank and i am having a little challenge using scanner class with the skip function. here is what i have tried. Objective In this challenge, we're getting started with conditional statements. Check out the Tutorial tab for learning materials and an instructional video! Task Given an integer, perform the following conditional actions: If n is odd, print Weird If n is even and in the inclusive range of 2 to 5, print Not Weird If n is even and in

growing the size of an array based on issue input

半腔热情 提交于 2019-12-13 10:32:01
问题 I wrote a class that has a 2d array. The class will print out the numbers of an array based on user input and size. For example the user input will be like 2 3 145464. This means that the array size is 2 rows and 3 columns and it should print the numbers in the array as 145 464 I cant figure out how to get the size of the array to change without declaring the size originally . This is what i wrote import java.util.Scanner; public class Assignment7 { public static void main(String[] args) {