FAQ Database Discussion Community


Java Scanner Standard in

java,java.util.scanner
I am using Eclipse as my IDE. I am reading in a simple file: 1 Andrew 2 Peter 3 Andrew 3 Peter 3 Andrew My problem is that sc.next() on the last line for Andrew hangs and never finishes. Is there a way to have it take in Andrew on...

NoSuchElementException: No Line Found, while reading in text file

java,text-files,java.util.scanner
I'm currently working on building a text adventure game, and I've run into a problem while trying to read in text files that contain the descriptions of the room. Whenever I run the program, I can properly read in and assign the first text file, but the second one throws...

How do you input a line into a array separated as array elements?(Java)

java,arrays,java.util.scanner
Using a scanner to read a line of input. I thought there was a way to directly insert the line into an array and have it separate into string elements(divided by spaces). import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int...

Using a scanner loop across classes (Java)

java,debugging,while-loop,java.util.scanner
I am trying to use a scanner loop as a method in my input class, in my main method. I keep getting this error: Exception in thread "main" java.lang.NullPointerException at Blackjack.Input.getBet(Input.java:27) at Blackjack.BlackJackGame.main(BlackJackGame.java:23) Here is the loop (this loop is in a separate class) : public int getBet(int cash) {...

Java Scanner hasNextLine returns false

java,java.util.scanner
I have several files (actually they are also java source files saved in Eclipse on Ubuntu) which I need to read and process line by line. I've noticed that I cannot read one of the files. The code I am using is as below try (Scanner scanner = new Scanner(file))...

Populating 2D array from text file using scanner Java

java,arrays,multidimensional-array,java.util.scanner
I am trying to populate a 2D array in Java through reading a text file using scanner. Currently, I don't fully understand what's going wrong. Below are the text file I'm using, the 2D array when it is printed and my code. Text File 30 30 30 30 30 30...

Java scanner adding variable conditions

java,java.util.scanner,conditional-statements
I'd like to ask how do i exactly condition what my program does if my user types in a character or a string if i want him to type an integer instead? I tried to do it how i showed here in quotes and also tried with "equals". The second...

Reading the content from an inputstream using scanner multiple times is not behaving as expected

java,inputstream,java.util.scanner
I am using the following code to read content from an input stream. @Test public void testGetStreamContent(){ InputStream is = new ByteArrayInputStream("Hello World!!".getBytes()); System.out.println(getStreamContent(is)); System.out.println("Printed once"); System.out.println(getStreamContent(is)); } public static String getStreamContent(InputStream is) { Scanner s = null; try { s = new Scanner(is); s.useDelimiter("\\A"); return s.hasNext() ? s.next() :...

Why is my BufferedReader not “Ready”?

java,java.util.scanner,bufferedreader
I looked up all of the BuffedReader "not ready" questions even slightly related to what I was looking for but I could not find the solution to my problem. I have a Client and a Server sending/recieving String using the appropriate methods (i.e. BufferedReader, PrintWriter), but my BufferedReader will not...

Using a scanner to extract data with delimiters by line

java,regex,java.util.scanner
I am extracting data from a file. I am having trouble with using the delimiters while reading through the file. My file is ordered like so: 0 Name 0 1 Name1 1 The structure is an integer, a tab (\t), a string, a tab (\t), another integer, and then a...

Why can't I enter another string after the first time in a for loop?

java,arrays,string,for-loop,java.util.scanner
I want to create a program that allows me to enter name, age, and year of birth for 5 different people. However, I get a problem where I cannot enter another name after I entered the first in my for loop. Here is my code: public static void main(String[] args)...

What does Scanner input = new Scanner(System.in) actually mean?

java,input,java.util.scanner
Scanner input = new Scanner(System.in); Could you give me a detailed explanation on what the code above is doing step by step? I don't really understand how it works and how it links to me later being able to do this statement: int i = input.nextInt() ...

Reading a file from Scanner but it won't print

java,java.util.scanner
I have a class that reads a section of a txt file. The code works for some people but to me it won't print to console or attach to my JTextArea(my ultimate goal) I just want to find out if its the code or the file that is the issue....

Why is my JOptionPane not appearing when I use a Scanner?

java,java.util.scanner,joptionpane
I have narrowed down my problem: JOptionPane will not appear if I use a Scanner. For example, this works fine: public static void main(String[] args) { System.out.println("Before the dialog."); JOptionPane.showMessageDialog(null, "Please."); System.out.println("After the dialog."); } Even this works, too: public static void main(String[] args) { Scanner sc = new Scanner(System.in);...

Why is my program not waiting for input?

java,regex,java.util.scanner
I'm trying to get user input and validate it using regular expressions. However, when I run the program, it doesn't seem to be waiting to receive any additional user input. Instead, I'm getting Exception in thread "main" java.util.NoSuchElementException` Any help on what is happening would be great! Here is my...

Java scanner won't match square brackets

java,java.util.scanner
I am having trouble parsing python like lists with scanners in Java. For some reason I seem to be doing something wrong when attempting to detect the square brackets. What puzzles me most is that it seems that somehow the occurences of square brackets in the input triggers the unwanted...

Why won't the scanner prompt me for an input?

java,loops,console,java.util.scanner
I have this method: public String scanInput() { String input = ""; Scanner skanner = new Scanner(System.in); while(skanner.hasNextLine()){ input = skanner.nextLine(); } skanner.close(); return input; } The first time this method is run, the program stops, and I'm prompted for an input in the console before it can proceed. However,...

scanner next() only -n characters from a file

java,java.util.scanner
Is there a way to read next -n characters in java scanner? Let's say I want to read 16 characters. I read a line, take 16 characters out of it, hop in new line and then continue with reading the SAME line. If it is needed, read a new line...

Scanner.next… throws java.util.InputMismatchException for Float but not for Int

java,floating-point,integer,java.util.scanner,inputmismatchexception
Why does Java throw an error when using Scanner.nextFloat() but not Scanner.nextInt() ? package myshit; import java.lang.Math; import java.util.Scanner; public class speed2 { public static Scanner keyboard = new Scanner(System.in); public static void main(String[] args){ float number = keyboard.nextFloat(); System.out.print("Start"); } } Input: 2.5 Output: Exception in thread "main" java.util.InputMismatchException...

Read same line two times using Scanner

java,java.util.scanner
In a class I have a method replay where I create a Scanner object sc. That Scanner object reads the first word in a line from a file. I then pass that Scanner object to a method in another class. In that method I want to read the rest of...

Issue with Java Guessing Game

java,java.util.scanner
I'm working on this guessing game for school. I've realized that at some point I deleted my while loop for the user's guess equalling the computer's random number and it has messed up the results of my program. I thought that I could just add a nested while loop, but...

Java Poker Game

java,list,java.util.scanner,poker
I am creating a java poker game so far I've got my program selecting random cards for the player, dealer and on the board (river). I now want my player to be able to input a value as a bet, I've though of using "Scanner bet = new Scanner(System.in);" but...

2D array scanner validation

java,arrays,multidimensional-array,java.util.scanner
Anyone able to steer me in the right direction. I am building a simple mark program where I get input from Scanner and insert it into my 2D array. I want to validate my input so that it isn't below 0 or above 100 but if I have the incorrect...

Counting number of special characters in a given file

java,java.util.scanner
Hy, In the result, the number of zeros and ones isn't the same. And i don't find where is the problem. Can any One help me please? //Main { int bufferSize = 10240; //10KB int fileSize = 10 * 1024 * 1024; //10MB Random r = new Random(); //Writing 0...

Reading text file in java using scanner

java,file,text,java.util.scanner
I am a beginner. Reading from text file in Java using a scanner. This code just to read the first 3 tokens isn't working: try{ Scanner scFile = new Scanner (new File ("readhere.txt")).useDelimiter("#"); String first = scFile.next(); String second = scFile.next(); int third = scFile.nextInt(); // error here. Why cant...

Error: Integers added together in successive uses of scanner class

java,exception,java.util.scanner
I'm doing exercises related to handling exceptions. While using the Scanner class and following exercises to check for InputMismatchExceptions, I got the following results from the following code. static Scanner sc = new Scanner(System.in); public static void main(String[] args){ System.out.print("Enter an integer: "); int a = getInt(); System.out.print("Enter a second...

List of Strings - Odd Behavior

java,java.util.scanner,contains,string-concatenation
I'm working on an obscenity filter for a random word generator so that it avoids certain words or phrases. The code is fairly simple so far and I'm using some test words to try it out, yet there is already a strange error occurring that makes absolutely no sense to...

In Java, why isn't nextInt() retrieving every number from this text file?

java,matrix,int,java.util.scanner
public GameofLife(int gen) { generations = gen; life = new boolean[20][20]; try { Scanner pablo = new Scanner(new File("life100.txt")); pablo.nextInt(); //eliminate 100 (number of files in the list) while (pablo.hasNextInt()) { life[pablo.nextInt()][pablo.nextInt()] = true; } } catch(Exception e){} out.print(Arrays.deepToString(life)); } I'm trying to read in 100 pairs int coordinates...

java scanner's same-exact-inputs are not equal (for me?)

java,java.util.scanner
I want to compare three couples of input from Scanner (just a quick test) by using the Compare method. When I run this program, it returns false for all three inputs even though there should be a true in between. When I remove Scanner and put in a String myself,...

Why get i InputMismatchException when i use scanner sc.nextDouble()

java,java.util.scanner,inputmismatchexception
I'd like to read data from a txt file, but I get InputMismatchException when I call nextDouble() method. Even though I am using the useLocale method, but it doesn't work. The txt file first line is: 1;forname;1.9 public class SimpleFileReader { public static void main(String[] args){ readFromFile(); } public static...

For Loop Logic Iteration Why not behaving Same ?

java,loops,for-loop,java.util.scanner
I have tried to Increasing Number By one Like this for (int i = 0; i < 10; i++) { System.out.println("Count " + i); } this logic Works fine. but when i try to make it As user input for the series its not giving same out put from 0...

restaurant bill, initalizing error

java,java.util.scanner
import java.util.Scanner; import javax.swing.JOptionPane; public class RestaurantBill3 { public static void main(String [] args) { //Constant final double TAX_RATE = 0.0675; final double TIP_PERCENT = 0.15; //Variables double cost; double taxAmount = TAX_RATE * cost; //Tax amount double totalWTax = taxAmount + cost; //Total with tax double tipAmount = TIP_PERCENT...

the logic behind the diffrerence between fileInputStream and Scanner classes

java,io,java.util.scanner,fileinputstream
I'm trying to understand the difference between Scanner.nextByte() and FileInputStream.read(). I read similar topics, but I didn't find the answer of my question. A similar question is asked in the topic : Scanner vs FileInputStream Let me say what I understand : Say that a .txt file includes 1 Then,...

Java scanner with 2 hasNext()

java,csv,java.util.scanner,reader
I would like to restore an object from a CSV file. I need to know if scanner has 2 next values: scanner.hasNext() the problem is my visit constructor takes 2 parameters and I need to ensure there are at least 2 left in my csv file. here is the relevant...

Data Validation and Scanners in Java

java,java.util.scanner,data-validation
I have a question regarding data validation and scanners.The following piece of code checks userinput.Anything other than an integer is not allowed and the user is asked to re-enter a value.My question is that the code works only if the scanner is declared within the while loop.The program executes infinitely...

Scan file with 3 possible regex in one method

java,regex,java.util.scanner
I am trying to retrive data from a folder which contains 3 .txt file (the same information) but in a different order. The data that I need to get is : Date Text number (one ligne) But the structure of my file is unique for each of them file1 :...

How to read propositional logic symbols as input using Java Scanner?

java,input,java.util.scanner
Scanner in = new Scanner(System.in,"UTF-8"); System.out.println(in.next()); If I paste ∧, I receive ? as output to the console. Can someone explain what I can do to properly read logic symbols like this? I'm using NetBeans 8.0.1. Thanks....

Java Scanner in Loop only gets first input

java,loops,java.util.scanner
I'm trying to get multiple inputs from the user using a for loop. Within that loop I'm calling a function that includes a scanner. The program works fine when I call the function just once. But within a loop only the first time the function is called it works correctly....

best solution for java txt project [closed]

java,file,java.util.scanner
Like topic says I'm looking for the best way to: I got .txt file. In this file there are for example: Matthew Sawicki 25\n Wladimir Putingo 28\n Barracko Obamaso 27 Whats the best way to write a program that opens this file, checks out the biggest number and then prints...

Java - Scanner causes loop to run twice [duplicate]

java,while-loop,java.util.scanner
This question already has an answer here: Skipping nextLine() after using next(), nextInt() or other nextFoo() methods 7 answers I'm fairly new to Java and programming in general. I'm trying to ask for names of celebrities and put them in an array. When I try to use scanner to...

Trying to get Scanner to scan entire file

java,java.util.scanner
String userInput = stdin.nextLine(); file = new File(userInput); Scanner fileScanner = new Scanner(file); while(fileScanner.hasNext()) { fileContents = fileScanner.nextLine(); } So I'm trying to figure out how I can get my variable fileContents to hold all of the file from the scanner. with the current way I have it setup the...

Java Scanner not reading newLine after wrong input in datatype verification while loop

java,while-loop,java.util.scanner
I've looked at similar questions and tried to follow the answers that solved the issues that others have had but putting the sc.next() or sc.nextLine() after the while loop causes it to go into an infinite loop. The problem is if a user enters incorrect input (nothing or a number)...

Issue with Scanner and While loop to check an arraylist

java,arraylist,java.util.scanner,bluej
Normally, I have no issues scanning an array list for certain elements. I am aware of how to structure the while loop etc. However, in this case, I need to use a scanner, but it is giving my issues as seen here: The following code is intended to use a...

How to skip a character when using Scanner

java,text,java.util.scanner
I want to read words from a text file which looks like: "A","ABILITY","ABLE","ABOUT","ABOVE","ABSENCE","ABSOLUTELY","ACADEMIC","ACCEPT","ACCESS","ACCIDENT","ACCOMPANY", ... I read the words using split("\",\"") so I have them in a matrix. Unfortunately I cannot skip reading the first quotation mark, which starts my .txt file, so as a result in my console I have:...

Scanner - Whitespace (Java)

java,java.util.scanner
I would like to take several inputs from a user. Input 1: 1; Input 2: 2; Input 3: 3; Input n: n However, if the user inputs 2 numbers, the scanner reads it too! Input 1: 1_2 2 is read. How do I prevent the user from storing the nth...

Why is an input line ignored?

java,string,java.util.scanner
I call a method in my main app to give some String variables to the constructor of a class but here is a problem: When I call the method (while running the program), my first input line is being ignored. Here is my code : Here i call the method...

Java use Scanner to count words and lines from user input

java,while-loop,count,java.util.scanner
I'm trying to use the Scanner class to calculate the number of words and lines from a user input, and this is my attempt: import java.util.Scanner; public class newCounter{ public static void main(String [ ] args){ Scanner input = new Scanner(System.in); printWords(input); printLines(input); } public static void printWords(Scanner x){ int...

java.util.NoSuchElementException when using Scanner.next()

java,exception,java.util.scanner
Java noob working on a project where I'm supposed to display data obtained from a text file onto grids. Project is essentially written, but output displays this exception: run: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1371) at inputOutput.ReadDataFile.populateData(ReadDataFile.java:50) at boggle.Boggle.main(Boggle.java:27) Java Result: 1 BUILD SUCCESSFUL (total time: 0...

Scanner and Multithreading issues?

java,multithreading,java.util.scanner
I have following code to read entire file data: calling method(String zipFile){ ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); //get the zipped file list entry ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(Constants.OUTPUT_FOLDER + File.separator + fileName); if (ze.isDirectory()) { new...

How to read from std.in if the length of the input is not define

java,input,java.util.scanner
I'm having problems with reading input from standard input in java using Scanner. I need to read the user input and put it in a dynamic array. Here's an example of the input: 4 6 2 3 4 8 9 5 And my code: Scanner scan = new Scanner(System.in); List<int[]>...

Using a delimiter in a method (Java)

java,java.util.scanner,delimiter
I have to read in input from a line of a text file : 1165,24 1305,27 1345,12 1360,10 1388,15 1388,20 1495,32 1680,36 and send it to a method that will build a Sales object with the first number on the line being the Product Code and the second being the...

Issues with if statement

java,if-statement,java.util.scanner
I am trying to find a keyword in a text file and return the sentence if found. If the keyword is not found I want to call a Writer method. But for some reason the Writer method always runs. What needs to change to ensure the writer method is only...

Trouble constructing an object with data in a text file using commas as delimiters

java,text-files,java.util.scanner
I'm having trouble creating an Inventory object through a constructor that is reading from a txt file. This is the file: 1165,4.25,15 1305,1.80,42 1345,12.56,16 1388,7.42,30 1480,6.54,80 1495,8.36,48 1560,15.27,65 So I'm attempting to grab the "1165" into the first variable then "4.25" into the next, etc. So the comma is the...

Receiving float from Scanner input Java

java,floating-point,java.util.scanner
I need a method that should check whether user's input is a float, and if it is string or int it should throw an exception. I declare the Scanner outside of the method: Scanner sc = new Scanner(System.in); And the method definition is: private boolean CheckFloat(Scanner sc) throws MyException {...

Scanning numbers from a formatted text in java

java,java.util.scanner
I have a formatted text like this: x.i9j11k2d1" index="603" value="0"/> x.i9j11k2d2" index="604" value="0"/> x.i9j11k2d3" index="605" value="0"/> x.i10j1k1d1" index="606" value="-0"/> And, I'm interested in Scanning only the digits. For example: int i,j,k,d,index,value; For the first line I want: i=9, j=11, k=2, d=1, index=603, value=0 For this purpose, I used the following...

Reading N lines from a file in java?

java,arrays,file,java.util.scanner,bufferedreader
I am not quite sure how to explain my question but I will try my best. Say for example, I have a file containing 100 numbers, is it possible to read lines 25-50 from this 100 numbers file. To read N amount from begining, I would do something like this;...

java scanner does not accept two words with space in between

java,runtime-error,java.util.scanner,space
I'm trying to create a simple project that will ask the user true or false and if true then the scanner is gonna keep storing the category name in an arraylist. And it works fine until you enter two words with a space in the category name "first second" for...

Java Method to Writer a new line to file each time called

java,file,java.util.scanner
I'm trying to create a java method that will write to a CSV file that has already been created. Each time the method is called from the class, it should append all the fields from that class, separated by commas, and then skip to the next line. My class overall...

Java read txt file to hashmap, split by “:”

java,file,text,java.util.scanner
I have a txt file with the form: Key:value Key:value Key:value ... I want to put all the keys with their value in a hashMap that I've created. How do I get a FileReader(file) or Scanner(file) to know when to split up the keys and values at the colon (:)...

Virus from using Scanner class on a URL?

java,java.util.scanner,virus
This may be a silly question but is it possible to get a virus while using the Java class Scanner on a potentially dangerous URL. URL url = new URL("http://www.VirusInfectedWebsite.com"); try{ PrintWriter fw = new PrintWriter("Test.txt", "UTF-8"); Scanner s = new Scanner(url.openStream()); while(s.hasNextLine()){ String line = s.nextLine(); fw.println(line); System.out.println(line); }...

Run Java Console Input as Statement

java,java.util.scanner,console-input
I am writing a command-line matrix manipulation tool in Java, and was wondering if it is possible to run Java statements through console input. I was thinking of using the java.util.Scanner object since that's what I'm using for the rest of my application, but I am open to any solution....

Reading CSV File Using Scanner Class Error

java,csv,java.util.scanner,delimiter
I have an original code that came from a Java book example. The program simply reads a text file ("clients.txt"). However, I would like to modify the "clients.txt" into a "clients.csv" and have the program read ".csv" file instead. I have parsed the data with "," as the delimiter according...

Searching for a particular line of text in a text file

java,regex,java.util.scanner
I am having issues with my synonym map. I want to be able to search a text file for a keyword or a related word in the textfile then outputting the found sentence. so my program searches for the answers to questions based on the keyword or sunonym. the way...

Why is my program throwing java.util.InputMismatchException?

java,java.util.scanner
I am getting an error in my code: Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at Hello.main(Hello.java:22) The full code for my example is as follows: import java.util.Scanner; import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc = new...

File not found when using multiple scanners

java,file,input,java.util.scanner,scan
I'm writing a simple program in Eclipse to input and compare two text files. However, I can't seem to import the two text files at the same time. If I delete one of the new scanner objects, it clears my other file; otherwise, it gives me an error that the...

use scanner in method with reloadable menu

java,methods,menu,java.util.scanner
I can´t get my reloadable method "menu" to work and it looks like it has something to do with scanner. I´m sure it´s an easy solution but I just can´t find it public class menuControl { public static void main(String[] args) { switch (menu()) { case 1: System.out.println(1); menu();break; case...

How can I create an Array of Objects from a text file?

java,arrays,arraylist,data-structures,java.util.scanner
I'm trying to complete an assignment for a Java class that I am currently taking but I'm having some issues with creating an array of objects (Giraffes in this case) from a text file. Can anyone provide some advice for me? The issue that I am having is that when...

Java Scanner - Read line breaks in to a string?

java,string,java.util.scanner,system.in
I have a scanner that takes user input until ctrl+d and then a while loop which adds each word to a string and then prints it, but i'd like to know how to also include the new line indicators like \n in the string wherever there is a new line....

Updating an old line inside a text file

java,java.util.scanner
Okay, so i have an issue trying to update a line or sentence in a text file. The way my program works is this: If a user enters a question the program searches the text file for that exact question(lets say is n). The answer to the question would be...

Scanner() not working with if - else statement

string,if-statement,java.util.scanner
My intention is to let the user decide which method to use by cheking its input. I have the following code: try { String test = scan.next(); if(test == "y") { //do stuff } else if (test == "n") { //do stuff } } catch (Exception e) { System.out.println("false"); }...

How to prompt user until the program gets a valid answer, how to do it N times?

java,arrays,java.util.scanner
I'm trying to make a simple program where if the user puts in a number in the array the digit that array number equals will appear. It works perfectly unless the user puts in a number not in the array, and then puts in a number in the array. What...

Java: if/else statement in infinite loop not working [duplicate]

java,java.util.scanner
This question already has an answer here: How do I compare strings in Java? 23 answers I'm trying to figure out why my function isn't working as it should. The code is fairly basic and self explanatory. Here is the function: public static void Greet(){ System.out.println("Hello, what is your...

Read words using scanner in Java

java,java.util.scanner
i wish to read multiple inputs on a single line in java. Ex: System.out.print("Input name, age, address, city: "); user will input these details separated with space what is expected in console: Input name, age, address, city: Tom, 10, USA, NY Any idea how to do this, using the Scanner...

Scanner object does not return intended result

java,java.util.scanner
I am trying to use the scanner object to validate some user input. According to my requirement if user input is 100>inputs<0 I need to provide some console output. However, the following code does not work when I enter 100/0 and provides me some empty console output. I tried to...

Is it necessary to close scanner object?

java,java.util.scanner,close
I'm a beginner in java, I wanted to know that is it necessary every time to close a scanner object ? If I do not close the Scanner object, it shows "Resource leak : 'input' is never closed". Can anyone help me ? import java.util.Scanner; public class average { public...

Read an instruction line by line java

java,file-io,java.util.scanner
I have a set of instructions in a text file: LoadA 0 LoadB 1 Add Store 0 LoadA 2 etc... I know I can use Scanner and hasNextLine but not sure how to implement this and have the instructions read and understood....

How to check nextInt from Scanner without consuming the int itself

java,conditional,java.util.scanner
What I am attempting to do is verify user input of an integer between 0-136, while also making sure that the input IS in fact an integer. I can't figure out a good way to do so, as using nextInt in the conditional consumes the int, and you can't compare...

How to properly use methods in Java for Magic 8 Ball program

java,methods,while-loop,switch-statement,java.util.scanner
I have no code to paste since all I have is a template of my methods to be used. Hopefully this isn't too broad because I've looked all over and haven't received the answer I'm needing. Many have seen or heard of a "Magic 8 Ball" program. A user asks...

My do/while loop messes my program up the second time around

java,java.util.scanner,do-while,palindrome
The issue I am having is once I input a word like "non" I get back it IS a palindrome. I then type "Yes" as I ask "Keep Going?". It starts back up at "Type Word:" and I type "non" again. This time however I get back it is NOT...

How do I reassign a scanner-based value that was already entered? - Java

java,methods,input,console-application,java.util.scanner
Not seeing any posts that I was able to answer my question with, I will ask it myself. I am currently trying to figure out some programming and had the intent of making a simple program that would ask a user to input an integer. After entering whatever the user...

breaking out of a loop at end of file

java,java.util.scanner
I'm trying to break out of a loop when all lines are finished being read. I've tried using String temp = scan.nextLine, if temp.equals("") break but that's not breaking out of the loop at the right time. I want it to read until the end of the file and then...

Java Scanner to find a tag, then delimiters to write what's in that tag to a file

java,html,java.util.scanner
I'm writing a program that's intended to search the HTML of a website, find a specific tag, then write the contents of that tag to a file. For example, the HTML could look like this: <div class="something" specific-tag:"print this 1">some content</div> <div class="something" not-the-right-tag:"don't print this">some content</div> <div class="something" specific-tag:"print...

Java Scanner reading something that is not there when reading CSV file

java,java.util.scanner
I have a csv file that goes like this Mike,Smith Scuba,Steve John,Doe And java code that goes like this: Scanner file=new Scanner(new File("input.txt")); file.useDelimiter(","); while (file.hasNext()){ String s1=file.next(); String s2=file.next(); System.out.println(s1+" "+s2); } file.close(); I get as output: Mike Smith Scuba Steve John Doe I don't understand what could possibly...

How to search for a sentence +1 in a file?

java,search,text-files,java.util.scanner
Okay so i am trying to store all user input into a text file. Eg if a user enters a question i search the text file for that question. if the question is found the next line in the text file would be the answer to the question. So my...

Scanner ignoring the String “name”

java,multidimensional-array,java.util.scanner
I am trying to take user input for name, last name, phone number and age. For some odd reason the scanner is skipping name but none of the other variables. Can someone point out my mistake please? I can't figure it out. import java.util.Scanner; public class Lab2{ String [][] info...

How to count occurrences of 15 numbers that are in an array?

java,arrays,java.util.scanner
I'm new to writing code. I need to make a program that reads in 15 numbers into an array that are between 0-50 and then print the occurrences. This is what I have so far. import java.util.Scanner; public class Occurrence { public static void main(String[] args) { Scanner scan =...

Java - if/else fails immediately when supposed to pause

java,if-statement,java.util.scanner
I'm having troubles with a function in java. Here's my code: do{ System.out.print("Proceed to payment? (y/n) "); input = scan.nextLine(); if((input.trim()).equals("y")){ break; } else if((input.trim()).equals("n")){ System.out.print("Come back next time, " + name + "."); System.exit(0); } else{ System.out.println("Invalid response. Try again."); } } while(true); Basically, the first time the function...

Java - Scanner execute only the Int and “skip” the Strings data types when i input String data before the Int ones [duplicate]

java,string,int,java.util.scanner
This question already has an answer here: Skipping nextLine() after using next(), nextInt() or other nextFoo() methods 7 answers I've been working in Java for a good couple of months as a Computer Science - Software Engineering. So I'm still beginner. Anyway, today while i'm working with "Scanner" from...

Java : Scanner odd behaviour [duplicate]

java,java.util.scanner
This question already has an answer here: Scanner issue when using nextLine after nextXXX [duplicate] Skipping nextLine() after using next(), nextInt() or other nextFoo() methods 7 answers I tried to get inputs via scanner and in the past, I use enter to get to the next set of inputs....

Can I use the same scanner to scan a double and string? Is this “||” an or-statement?

java,string,if-statement,while-loop,java.util.scanner
System.out.println("\nEnter item's price"); Scanner newItemPriceSC = new Scanner(System.in); Double newItemPrice = newItemPriceSC.nextDouble();//stores item price String goBack = newItemPriceSC.nextLine(); System.out.println("type \"no more\" if there are no more items\ntype any other word to continue"); String answ = continueEnd.nextLine(); if(!(answ.equals("no more"))){ continue;//if user does not answer "no more!", loop continues } if(answ.equals("no...

Counting integers with hasNextInt

java,integer,java.util.scanner
I don't know what is wrong here import java.util.Scanner; public class CountingInts { public static void main(String args[]) { Scanner input = new Scanner(System.in); int count = 0; System.out.print("Numbers: "); while (input.hasNextInt()) { int x = input.nextInt(); count = count + 1; } System.out.print(count); input.close(); } } I want to...

INPUT FILE VALIDAT DATA

java,java.util.scanner
I cannot seem to figure out a problem. here is what I'm trying to do open and read the content of a .txt file. If there is a problem with one of the lines of data in the file skip it and continue reading the file I cannot find a...

Java Scanner useDelimiter() syntax error

java,java.util.scanner
I've looked around SO but I can't find anything that is helping. Basically I am writing a piece of code to grab a list of numbers as input. However I want the numbers input with a comma as Delimiter. Here is my code-snippet. import java.util.Scanner; public class TreeUtils { Scanner...

Java Scanner input in separate thread

java,multithreading,java.util.scanner,nio
I have a multi-threaded command line app. It is a web service client with a pool of 10 threads that churns away, sending requests, batch-style, to a server. But it runs for a few days, and sometimes further down the pipeline, the queues start getting backed up. So I want...

In Java Program Scanner is Workin only once whereas i/ve used it twice

java,java.util.scanner
import java.util.Scanner; class Student1{ int a; String b; void get(int c, String d){ a = c; b = d; } void show(){ System.out.print("Id is " + a +"\n"); System.out.print("Name is " + b); } public static void main(String args[]){ Scanner one = new Scanner(System.in); System.out.print("Enter Id"); int e = one.nextInt();...

If statement that checks if a user entered string contains user entered letters excluding any extra letters

java,string,if-statement,java.util.scanner
I need to create a program in which a string and 5 letters are entered by the user and the program checks if the string can be created again from those 5 letters, but ignores any letters that the string cannot be created from. For example, if I enter the...

Knight's tour in java (recursive)

java,recursion,java.util.scanner
I am writing the code for a classic Knight's tour, my code seems to be mostly doing the right thing but it still gives me "not possible" for possible boards and i can't figure out why. (ex: it fails for for a table with 3 rows, 4 columns, starting at...

How to split a string with space being the delimiter using Scanner

java,java.util.scanner
I am trying to split the input sentence based on space between the words. It is not working as expected. public static void main(String[] args) { Scanner scaninput=new Scanner(System.in); String inputSentence = scaninput.next(); String[] result=inputSentence.split("-"); // for(String iter:result) { // System.out.println("iter:"+iter); // } System.out.println("result.length: "+result.length); for (int count=0;count<result.length;count++) { System.out.println("==");...

Option pane is not opening

java,swing,java.util.scanner,joptionpane
I am trying to display an option pane from a Java application, but it is not working. Below is the code package sampleapplication; import java.util.Scanner; import javax.swing.JOptionPane; public class SampleApplication { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Brin"); int sample = input.nextInt(); System.out.println("The output number...

ADT not accessing other class, constructors not working

java,java.util.scanner
I'm trying to create a Node class and I want the default to be all 0's and null and then when I call the node class with constructors i want to set them. and with the code I have set now I just keep getting symbol: method Node(int,int,java.lang.String) location: variable...