A simple text scanner in the JDK which can parse primitive types and strings using regular expressions.
0
votes
0answers
14 views
InputMismatchException after checking with hasNext
I'm using a Scanner to control input (which comes 1 line at a time) to my program. First, I check the line to see which command it matches. Then, I use Scanner's next to go through the line and ...
0
votes
0answers
21 views
I need to use while loop return a List containing all numbers read from the Scanner
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;
public class Q3ControlFlow {
Scanner scanner = new Scanner(System.in);
...
4
votes
6answers
20k views
Java: CSV file read & write
I'm reading 2 csv files: store_inventory & new_acquisitions.
I want to be able to compare the store_inventory csv file with new_acquisitions.
1) If the item names match just update the quantity ...
0
votes
3answers
45 views
if else statement not stopping issue
I have a code which has a button that gets the input of a text field and then afterwards looks through a .txt file, to see if there is a match. Only issue is it runs through each line and checks if ...
-1
votes
0answers
25 views
Java: Arraylist continues to be populated by same value [duplicate]
I am learning java and I have run into trouble when working on my final assignment for my Java Programming I class. For our assignment we are creating a text-based, turned-based, multi-player fighting ...
0
votes
1answer
47 views
How to allow the user to access methods of a class
I am working a programming project from Java textbook that says:
The L&L bank can handle up to 30 customers who have savings accounts. Design and implement a program that manages the accounts. ...
0
votes
1answer
4 views
Java Scanner While Loop Conditionally Not Respecting Conditions
I've attempted to resolve this problem for several hours, don't understand the problem.
Code
import java.util.Scanner;
public class Scan {
public static void main(String args[]) {
System.out....
-1
votes
6answers
31 views
How can i use the variables (which are inside try/catch block) outside the block?
i am trying to use try/catch block but i can't get the variables values out of the block.
What can i do ?
import java.util.Scanner;
public class Program {
public static void main(String[] args)...
0
votes
1answer
17 views
java copying all contents of one file to another using input to read/create the file name
I'm having trouble using input to create the file that I want to write the lines from another file in. When my code reaches the file name I want to create, it doesn't let me input anything after I ...
0
votes
3answers
11k views
Scanner in java STDIN nextLine()
Our professor has us doing a lab to parse strings. He refuses to let us use files and says if he copy and paste a paragraph of strings we have to parse it. My problem is I'm not sure how to get the ...
-1
votes
2answers
16 views
Java: Searching an unsorted integer array for the first occurrence of a user inputted integer
I've noticed two problem and would really appreciate the help!
With (what I believe) to be with how the methods "arrayIndex" and "position" are interacting with each other. I say this because when I ...
0
votes
3answers
28 views
Java: Searching through unsorted integer array and returning how many numbers are greater
I've been trying to create a method to search an unsorted integer array but I have come across some issues I just can't get by. I want to allow the user of the program to enter an integer value and ...
-1
votes
0answers
35 views
Creating an Array with no repetitive element [on hold]
How do you create an array in Java which can accept names AND only (1,3,5,9,@,$,%,^) from the user. No other digits or special characters allowed.
The user should be asked to enter [5] elements and no ...
-2
votes
2answers
27 views
(JAVA) The following code cannot debug: what errors were done and how to fix these errors?
I have a problem with the following code. The scenario is as such: the user inputs a string (can be a sentence). The code will, for each words, print the complete word + "ay" at the end if it starts ...
0
votes
0answers
25 views
Using delimiter to get multiple words as one string?
I don't really know how to explain what I'm asking but I'm having trouble using a delimiter to isolate a multi-worded string.
Example: For line
3 James De Santa 2.30 friend 2010
I am not sure how ...
-3
votes
2answers
57 views
Scanner input validation Java
while(!isValid){
System.out.println("Enter house number: ");
if(input.hasNextInt()){
houseNum=input.nextInt();
isValid=true;
}
else{
System.err.print("Wrong ...
9
votes
9answers
48k views
Java reading multiple ints from a single line
I am working on a program and I want to allow a user to enter multiple integers when prompted. I have tried to use a scanner but I found that it only stores the first integer entered by the user. For ...
0
votes
3answers
1k views
“Java.Util.NoSuchElementException: No Line Found”
//I am not able to figure out what is wrong? Please help me. I was able to use the //scanner .I am not able to input the values.Java.Util.NoSuchElementException: No Line //Found.
//String ...
-1
votes
0answers
28 views
Need help importing a file with Java using Scanner and File
I'm trying to import an excel file to read data from and when I run my program and it should display the data, but it displays the memory location of one of my classes instead. Anyone know what ...
-3
votes
0answers
11 views
in javascript, im trying to make a tv rating program but im getting a run-time error that i cant fix [duplicate]
no errors are displayed untill i run the program. it all works untill i try to enter input like '1' etc. below is the code that im trying to run along with the error message after it.
{
private ...
0
votes
1answer
23 views
hasNext() hangs on False value when used with System.in [duplicate]
For some reason scanner.hasNext() function works weirdly when used with System.in.
Code You can see below works well on first part, but not on second. For some reason it hangs when scanner.hasNext() ...
1
vote
0answers
13 views
nextint Preventing a nextLine from executing [duplicate]
I have a method for adding an episode/multiple episodes, which uses sc.nextInt for validation, then sc.nextLine for the input of the name of the episode(s).
However, the sc.nextInt prevents the sc....
-3
votes
1answer
26 views
Scanner.hasNextLong Error
Hello and thanks in advance,
I'm having a problem with the java.util.Scanner Here is my code:
public static void ScanInput(String Choice) {
if(scanner.hasNextLong()){
long input = ...
1
vote
2answers
46 views
Java: Finding average and numbering lines
I've been trying to figure out how to find the average of all the ages in this set of data and also numbering each individual line. What I've done so far has only made it incomprehensible. My errors ...
0
votes
1answer
48 views
Java Scanner making a triangle with star( divided in four static void
I have to make something like this.
*
***
*****
*******
*********
But my teacher required in some weird format have to follow. And using For loop statement. And ...
0
votes
2answers
16 views
Java: Token rearrangement and character removal with text file
I am trying to take a text file which has a list of peoples first and last names with age and rearrange it so the console output would go from 46 Richman, Mary A. to Mary A. Richman 46. However, in my ...
-1
votes
2answers
28 views
Java: Removing commas from text file input
I am trying to figure out how to remove all commas from a text file so that I can output it in the console where there are no commas. My input.replace("[,]", ""); statement isn't working and I don't ...
0
votes
0answers
10 views
Battleship scanner to joptionpane
I am writing a battleship game which require me to use the joptionpane, but I really have trouble of understanding that. I have everything set up in scanner and I need helps from converting scanner to ...
0
votes
1answer
33 views
How to read from text file, split strings from integers and then pass integers into an arraylist?
so I am very new to programming and have been trying out some exercises to better learn java.
Right now, I have a program that reads exam marks from a text file(contains only integers exclusively) ...
1
vote
2answers
33 views
How to read data from a file using a class's member method in Java?
I've created a class called Stud which contains data and member methods about students.
I would like one of the member methods, getData, to read data from a file.
import java.io.*;
import java.util.*...
1
vote
2answers
53 views
Making a List in Java from a text file
In Java I'm using the Scanner to read from a text file,
for example (cat, dog, mouse).
When I use the System.out.println() the output appears like cat, dog, mouse
I want the list to look like this
...
-1
votes
1answer
19 views
If the input isn't valid loop back and repeat [duplicate]
I need to put an invalid file catch around this bit of code where the String input can still be read but I can't seem to do it.
If the file isn't found, I want it to say the input is invalid and ...
1
vote
1answer
43 views
Java abruptly terminates when hasNextInt() encountered the end of input
I'm trying to make stack. I used debug tool in Eclipse, and found the code terminates when hasNextInt() encountered the end of input. the below code terminates at 14th while loop, and 18th statement ...
-1
votes
2answers
27 views
Convert string to doubles in a .txt file?
I have a .txt file of numbers that are formatted like the following:
1.2 2.3 3.4 4.5
2.0 3.0 4.0 5.0
6.0 7.0 8.0 9.0
I'm trying to use a scanner to take each line as a string, convert each number to ...
0
votes
1answer
29 views
How can I get my program to stop accepting inputs and run the program?
I want the program to print out the adjacency list of the given input using maps and sets. The input itself is supposed to be a directed graph and each line is supposed to be an edge. I want the user ...
2
votes
4answers
11k views
Java Scanner won't “finish” reading input
I've been having trouble using java's Scanner class. I can get it to read my input just fine, but the problem is when I want to output something. Given multiple lines of input, I want to print just ...
1
vote
0answers
18 views
Input issue in Java [duplicate]
I was wondering if someone could help me fix small issue with the code I that I cant seem to figure it out or point me in the right direction. Every iteration after the first one at the begining the ...
17
votes
4answers
84k views
Read next word in java
I have a text file that has following content:
ac und
accipio annehmen
ad zu
adeo hinzugehen
...
I read the text file and iterate through the lines:
Scanner sc = new Scanner(new File("translate.txt"...
-1
votes
0answers
7 views
2D array: Exception in thread “main” java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) [duplicate]
Ok, I'm kinda new to programming in Java so bare with me. I have searched this site for an answer to my question, but all the fixes that I try to apply don't really seem to do anything. I'm getting ...
1
vote
0answers
19 views
Read a single line from a text file Java [duplicate]
So I have a large text file full of primes, and I want to be able to ask for a single prime, like the 48th prime for example, and I want Java to read that specific line and return the value. As of now,...
0
votes
0answers
18 views
System.in is closed abnormally
I want to use Scanner to read from console. I create a new object, initialize it and when the main thread's works are done, I call a function named maintenance (). Then I instantiate a new Scanner:
...
0
votes
1answer
8k views
Reading a field of type Date by a scanner
private Date startDate;
public void readData(Scanner scanner)
{
// String dates = DateUtil.convertDateToShortString(startDate);
dates = scanner.next();
}
How can I read ...
0
votes
1answer
23 views
Reading in multiple data types from a txt file Java
I know there are many questions already on simply reading data from a txt file, but the txt file I was given contains this:
1980 Aug 945 100 Allen1983 Aug 962 100 Alicia1984 Sep 949 100 Diana
...
0
votes
0answers
24 views
Java File Handling java.util.NoSuchElementException
I am writing a java program that reads a file and perform different operations and then write modified contents in the same file. All of my code is working fine but the add a new word method is giving ...
7
votes
11answers
23k views
Scanner doesn't read whole sentence
I'm writing a program which allows the user to input his data then outputs it. Its 3/4 correct but when it arrives at outputting the address it only prints a word lets say only 'Archbishop' from '...
4
votes
1answer
36 views
Java Scanner class fails in tokenization when 1024 character is the delimeter
I've found a strange behaviour of java.util.Scanner class.
I need to split a String variable into a set of tokens separated by ";".
If I consider a string of "a[*1022]" + ";[*n]" I expect a number n ...
0
votes
2answers
27 views
Java: How to AUTOMATICALLY test a console application that gets input from user
I'm writing a console application in Java that gets input from the user by
'Scanner' class. I don't want to enter each time that I test my program the input manually in the console, so I want to put ...
0
votes
2answers
26 views
How can I get the matched delimiter from Java's Scanner?
Using the following code as an example (modified from the docs):
String input = "1 fish 2 fish red sheep blue sheep";
Scanner s = new Scanner(input).useDelimiter("\\s*(fish|sheep)\\s*");
System....
0
votes
2answers
38 views
nextLine() not functioning as expected even after edits
Here is the problem that I am trying to solve:
Task
Given a string, S, of length N that is indexed from 0 to N-1 , print its even-indexed and odd-indexed characters as space-separated strings on a ...
18
votes
7answers
258k views
Reading a .txt file using Scanner class in Java
I am working on a Java program that reads a text file line-by-line, each with a number, takes each number throws it into an array, then tries and use insertion sort to sort the array. I need help with ...