Questions tagged [java.util.scanner]

A simple text scanner in the JDK which can parse primitive types and strings using regular expressions.

java.util.scanner
Filter by
Sorted by
Tagged with
4 votes
1 answer
2k views

Getting multiple inputs in one line until user press enter

What I am trying to do is that: 2 (number of test cases) 4 7 8 15 16 (test case 1) here will be output, for example will print the max number 7 97 1 2 9 (test case 2) here is output again I will ...
Sedat Gokcen's user avatar
  • 3,210
0 votes
1 answer
91 views

How can I make the scanner search for a token, then return the one after it?

I want the strings inning, battingTeam, outs, balls, and strikes in an ArrayList<String>. In other words, I need the string that immediately follows the tag @attribute. @relation and @data and ...
ReiHinoX's user avatar
0 votes
1 answer
16k views

error: cannot find symbol: class FileNotFoundException

I'm receiving the following error: printfile.java:6: error: cannot find symbol throws FileNotFoundException { ^ symbol: class FileNotFoundException location: class printfile ...
ashwetzer's user avatar
0 votes
5 answers
442 views

Create an instance for the class Scanner, but not for Math class in java?

Why do I have to create an instance for the class Scanner, but not for Math class in java? I hope you guys can explain me this with good examples. I understand it in this way: We are asking the ...
hahn's user avatar
  • 41
-1 votes
1 answer
295 views

Read integer array through white space with using limit in java

My requirement is: The first line of the input consists of an integer n (where n>=3) denotes the size of the array. In next line there will be n integers separated by space correspond to the n ...
user1099280's user avatar
-1 votes
2 answers
2k views

User input values in the global variables

I declared two variables in the main class of Java which I made public. Please see the code below. java.util.Scanner; public class NestedExample { public int a,b,c; public static void main ...
Saurabh Bhatia's user avatar
5 votes
1 answer
8k views

Scanner nextLine() occasionally skips input

Here is my code Scanner keyboard = new Scanner(System.in); System.out.print("Last name: "); lastName = keyboard.nextLine(); System.out.print("First name: "); firstName = keyboard.nextLine(); ...
jsan's user avatar
  • 1,047
1 vote
2 answers
1k views

Reading ints from file and storing value to an array

I have a file, lets say data.txt which contains data in the following format 1 12 2 84 3 82 9 82 3 1 3 2 The first number indicates what index of the array the second number should be added to. I am ...
g123's user avatar
  • 11
0 votes
1 answer
5k views

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException UNKNOWN COLUMN

I am currently trying to scan and parse the file that is not in sql format. I am trying to input all the data into the SQL table but for some reason every time i run the program, i get the error ...
Kito's user avatar
  • 15
1 vote
1 answer
57 views

Listing and counting files by their extensions

So far I have a code like this: import java.io.File; import java.util.Scanner; public class Test { static Scanner input = new Scanner(System.in); public static void fileListing(File[] files, int ...
JohnDoe's user avatar
  • 25
-2 votes
1 answer
71 views

Not getting an output for my code

My code for converting Roman Numerals to their Decimal Form seems to be correct, but for some reason I cannot figure out how to get any sort of output. I want to say at the end "The Roman Number" + ...
blue1artic's user avatar
-1 votes
2 answers
101 views

issue counter and arrays

I'm trying to find a way to make this letter counter operate, and it has to be using arrays in the letterCounter class. I'm confused on how to do this where it's concise. Here is the instruction I was ...
Thomas Norman's user avatar
-1 votes
1 answer
57 views

how many letter occur in a text?

I got it working but it is also counting the spaces between the words that are being typed. All i care is for the letters to be count and not the spaces. How or what can i do to change this? where am ...
Noobie's user avatar
  • 1
0 votes
1 answer
333 views

JAVA How do you scan a .txt file and allocate individual characters it to a 2d boolean array using charAt(int)?

First of all, thank you for coming and taking the time to help with the problem. I am a complete noob (3rd day programming) which explains why I have spent countless head-aching hours trying to solve ...
Valentina's user avatar
  • 115
3 votes
2 answers
2k views

Scan all integers on a line into an ArrayList

I am trying to scan a line of Integers from a txt file and store them within an ArrayList. My question is simple, how can I do this? This is what I have public static void main(String[] args) throws ...
Junikin's user avatar
  • 301
0 votes
1 answer
46 views

Is there a way to update an object from the main class to all the other classes within the program?

Is there a way to user the Scanner class in ex. Test_Main.java and make lets say int number1 = scan.nextInt() and then have number1 be one be visible to all the classes and have it change in all of ...
LJP1203's user avatar
  • 115
1 vote
1 answer
95 views

Scanner not picking up contents of the file?

I have a small problem/bug in my program that won't scan in the contents correctly. I believe I am using the correct command? ex: java homework5 hwk5sample1.txt Why don't any contents get scanned and ...
Junikin's user avatar
  • 301
2 votes
2 answers
3k views

Remember the position in a Scanner

Here's a simple example explaining the problem I'm having: public static String nextTwoTokens(Scanner scan) { String partOne = scan.next(); String partTwo = scan.next(); return partOne+...
starwarswii's user avatar
  • 2,277
0 votes
0 answers
75 views

Using scanner to get integers from a file

For my java class in college, I need to read a text file that holds the scores of a golf game and add them together to reach a total. I can scan the first column of the file fine, but I cannot figure ...
gangwerz's user avatar
  • 435
0 votes
1 answer
548 views

null value added when using Scanner

I am trying to put all the data in a text file into a single String variable with the knowledge that I have and it runs fine but the output shows the following: nullRead File Test I used to ...
Juan's user avatar
  • 198
0 votes
1 answer
1k views

How do you scan individual characters from each line of string in a .txt file into a 2D array?

First of all, thank you for coming and taking the time to help with the problem. I have spent countless hours searching all over and still have not found a working solution to my problem: How do you ...
Valentina's user avatar
  • 115
4 votes
1 answer
13k views

Instantiate Java Scanner in class or method

I have created a class to read user input. I am using scanner to get user input and sanitize it. When using scanner object, it struck me with a question which of these 2 approaches will be best ...
muradbax's user avatar
1 vote
2 answers
120 views

How to scan the next line of a file of integers?

In my program, I am trying to use a Scanner to scan a file full of integers. This is a homework assignment asking me to write a program that shows all ways of making up a predetermined amount of money ...
GEARZ's user avatar
  • 77
0 votes
2 answers
638 views

Java Scanner if/else and while statements [duplicate]

I am trying to make a program that receives input from user, which is then used later on in the program. The problem I have is to make an exception. For example, if I only want the user to put in a ...
tomSurge's user avatar
  • 256
-3 votes
1 answer
120 views

NullPointerException thrown by code [closed]

I have a task to read data from a file, format it and then output it to another .csv file. I'm testing my reading and I'm getting a null pointer and I can't seem to figure out why. Three classes and ...
Joesph Olliff's user avatar
0 votes
3 answers
2k views

Java returning entered text

My first post here on stackoverflow. The task is: Write a method that shall return a String, the method has no parameters. The method is going to read some word from the keyboard. The inputs ends with ...
NERO's user avatar
  • 1
0 votes
1 answer
3k views

Continue reading console input after Ctrl+Z in Java

For a university assignment, from the console I need to be able to read in multiple lines until the user enters Ctrl+Z. That I have no issue with, my problem lies with me being unable to read anything ...
FreakyDan's user avatar
  • 569
0 votes
3 answers
1k views

Write over 2D array values with Scanner input

I am having difficulty with the getCategoryWeights method (at the bottom of my code) I am writing. It is supposed to change the values of the weight column based on the user's input, but I can't ...
Evan's user avatar
  • 344
0 votes
1 answer
83 views

Exception when passing value from scanner

I ma getting Exception when trying type from keyboard 2nd scanner in "talis probability". Mainly i am trying to change from keyboard probabilty of tail.. Number of threads: 2 Exception in thread "...
JaKoZo's user avatar
  • 314
1 vote
1 answer
38 views

method calling and sending is off

The code I have pasted below is 3 methods. The first method (maxLetterRecurrence) finds which letter occurs most, secondly I want to send the letter that occurs the most to the second function (...
jackson's user avatar
  • 11
0 votes
2 answers
64 views

Accepting a known number of integers on a single line in java

I know there is a similar question already asked, so let me apologize in advance. I am new to Java and before this, my only programming experience is QBasic. So here is my dilemma: I need to accept ...
Nate's user avatar
  • 19
0 votes
1 answer
805 views

Need help using java to read a file, insert trim command, edit the strings, and then save the file as an output

I have two different ways to read the file but I am not sure how to proceed to converting the text to a string and then an if then statement like... if string contains ":" ...
lcoats's user avatar
  • 3
0 votes
2 answers
53 views

NoSuchElementException in Scanner when using next()

I am basically trying to fill a String[][]. Whenever I use the next() function of my scanner it throws me an exception. Can you help, please? public static void main(String[] args) throws ...
Quatsch's user avatar
  • 391
1 vote
2 answers
2k views

Reading from scanner and skipping whitespace

This program is working okay when my input file does not include initial white space. I need it to work ok when the input file lines have space in the beginning. Here an example of my input: This ...
carl lewis's user avatar
0 votes
1 answer
67 views

Scanning a text file with Java

The purpose is to print-out a list of names from a .txt file. The code I've used is below. import java.util.ArrayList; import java.util.Scanner; import java.io.File; import java.io....
Jack's user avatar
  • 287
0 votes
0 answers
33 views

Code skips user input, executes code, and repromts after error check in java

Promting for a user to enter true or false for powersteering option to be added to a object later. for( i = 0; i < 1; i++){ System.out.println("Please enter true for power steering ...
jscincy1's user avatar
2 votes
4 answers
812 views

How do I reverse all Strings in my array?

How do I reverse my array output? Like "peter" to "retep" or "max" to "xam" I tried to use collections but it's not working This is my code: import java.util.Scanner; import java.util.Collections; ...
com9's user avatar
  • 43
-2 votes
1 answer
445 views

Number of threads defined by input

I must create a few threads and do some operations inside them. How to create them using Scanner? E.g. my input is '5' and program creates 5 threads to work with. Here's my code: public class ...
JohnDoe's user avatar
  • 25
0 votes
1 answer
87 views

Compiler presenting errors although Scanner is imported?

import java.util.Scanner; public class System { public static void main(String[]args){ Scanner scan = new Scanner(System.in); String cName, cContact, cAddress, cIC, cDob, cGender;...
Damian A.'s user avatar
0 votes
1 answer
140 views

My infinite while loop is causing Java to throw NoSuchElementException

The system exit happens when it gets to the end of the program but it should work just like the inner while loop. Any suggestions are welcome. Here is the error message that i get: Exception in ...
user avatar
1 vote
2 answers
70 views

How to read all the chars into a String from a textfile (using scanner)

I have tried: String input = ""; FileReader reader = new FileReader(inputFileName); Scanner in= new Scanner(reader); input = in.nextLine(); This doesnt work as it only reads in the first line, so i ...
10e5x's user avatar
  • 909
0 votes
1 answer
66 views

creating, writing to and reading from files java

I have made a program that is supposed to create a file, write to it, and then read from it. The problem comes with readFile(), where suddenly hasNext() is undefined for Formatter? I thought that ...
user avatar
0 votes
2 answers
8k views

Take a single line input of numbers, and storing it into an array

import java.util.Scanner; class HistogramChart { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Please enter the population of ...
Kyle Goodwin's user avatar
-1 votes
3 answers
814 views

Scanner not stopping from reading input

I have a program which reads strings from the console and stores the strings in random indexes of a String array. But the problem is that the program continuously asks to input and its not stopping. ...
Johny's user avatar
  • 2,148
1 vote
4 answers
7k views

System.out.println error New programmer

I'm trying to do a simple input program and I get an error on the System.out.println command, I don't know why it doesn't accept the command and can't continue working until I fix it. The error says: ...
rodrigo villarreal's user avatar
0 votes
2 answers
2k views

How to make the program wait for user input when it's inside a for loop?

my program needs to get information about a user inputted amount of cars. I used a for loop to make it run for that amount, but when it comes to asking for the information, the program will run the ...
Aden Peerrson's user avatar
0 votes
1 answer
54 views

Odd error related to Stream being closed

I am learning Java and I wrote a method to update a record in a file. The problem I am having is when I ask the user if they would like to look for another file my reader is closed or is unable to ...
Akerr Stephen's user avatar
-2 votes
1 answer
537 views

Command Line Returning Blank

After entering file to be processed the command line jumps to next line but remains blank, as opposed to printing the desired array. I want to call the getInputScanner() method to produce the scanner ...
LeSexyNinja's user avatar
1 vote
3 answers
4k views

After getting user input "Scanner name = new Scanner(System.in);" how to use is in a if statement? [duplicate]

So im learning java,bit by bit im getting a bit more knowledge, now im following tutroials and other webistes ect on learning but im stuck on one problem where i can't figure out what's the problem. ...
Charles Florianowicz's user avatar
0 votes
1 answer
25 views

Using java Scanner class

I am trying to get user inputs(integers) till she enters 'end'(String). I am trying the following code, but its storing only alternate numbers entered in the array. Please suggest. Scanner sc = new ...
Anushree Acharjee's user avatar

1
90 91
92
93 94
130