Parsing Input From A File Using Scanner
Mar 26, 2014
I'm having some kind of weird problem reading input from a file. It says that my scanner object I'm using to hold an item of information isn't initialized, when I do try to initialize it, it says error variable already initialized. I'm using the scanner to read input from a file whose contents are this
10
150.4
88.4
-3.14
499.4
799.4
1299.8
0
1900.2
901.7
4444.4
This is my program
import java.util.*;
import java.io.*;
public class QudratullahMommandi_3_07 {
Toolkit_General toolKit = new Toolkit_General();
public static void main (String[]args)throws IOException
[Code] .....
This is the error message
QudratullahMommandi_3_07.java:34: error: variable holder2 might not have been initialized
String holder2 = holder2.trim();
^
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
View Replies
ADVERTISEMENT
Oct 29, 2014
I have a small bug in my program. The user is asked what person(s) information they want to access but lets say they want captain they must enter "captain" twice. I think it will make more sense to you with the code. I have searched all over to see what is causing the bug but still have found no resolution. I even tried making two different scanners but that didn't work either.
I know the while loop (line 16) I am using is causing the bug because it works fine without that but then I cannot validate the input.
package myproject;
import java.util.Scanner;
public class Enterprise {
public static void main (String[]args){
String userInput;
[Code]......
View Replies
View Related
May 9, 2015
I am having a great deal of frustration trying to use the scanner to get user input from the eclipse console. Here is what I am trying to do.
Print a menu for the user with options, take that (1) char input and run it into a switch statement for processing. I have that done and it is working fine.
If the user chooses to enter strings for storage, They are instructed to enter their string and press enter to complete that string entry. Then enter the next string and press enter, etc.
So I have a While loop for this. Get the scanner input, store it in the LinkedList, get the next scanner input, etc. I get the scanner string and store it in a Linked list. That works fine. The user is instructed to simply enter a blank string to end the entry procedure, like just press Enter on the new line without typing a new string.
The problem is the scanner doesn't seem to return anything for me to Test to close this procedure. How do I TRAP the fact that the user just pressed enter so I can end my procedure? I have tried next() and nextLine() and reset(), etc. And I am getting knowhere.
View Replies
View Related
Sep 13, 2014
Is there any way to use scanner to get input and distinguish between int and text? I'm trying to make a scanner method I can call whenever I need user input no matter if I need int or text. Using two methods (one for int, other for text) at the moment.
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Person person1 = new Person();
System.out.println("What is your name?");
[Code] .....
View Replies
View Related
Jun 12, 2014
So I clipped this out of my Jeopardy game code and made it into its own project for testing. using this code I want to check to see if the input for wage is an integer?
import java.util.Scanner;
public class test {
/**
* @param args
*/
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
Obviously right now if you enter "aflwkj" or some such for the wager, the program terminates. How to make a loop that will keep asking the user for a value for wage until the input is an integer?
View Replies
View Related
Apr 15, 2014
I'm trying to read user input from the terminal and separate the input into separate arrays depending on if the user input is an integer, scanner, or a string. The terminal should keep asking the user for input until the user types "quit".
import java.util.*;
public class arrayScanner {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Double> doubleList = new ArrayList<Double>();
ArrayList<String> otherList = new ArrayList<String>();
[code].....
View Replies
View Related
Feb 19, 2014
Assignment: Your task is to implement a similar scheme to store polynomials of any number of terms, such that the number of terms and the components (coefficient, variable and exponent) of every term are entered from the keyboard. To implement the interactive input we will using the Java class Scanner, defined in the java.utils standard package. The Scanner class can be used in Java to read data types from a file.
Since the input console (keyboard) is treated as the file called System.in, we can create a Scanner for that input stream as new Scanner (System.in), as shown below. Once you define a Scanner object, using its method next() you can read Strings from the file/keyboard. The incomplete program below is your assignment. You are supposed to complete without changing the existing code. Your output should be the terms of the polynomial entered by the user, separated by + signs. Additional instructions in the code below, that you will change to achieve the requested functionality.
import java.util.Scanner;
public class Polynomials {
public static void storeTerm (int coeff, String var, int exp, String poly[][], int
where){
//ENTER THE COEFFICIENT, VARIABLE AND EXPONENT INTO THE
//ARRAY POLY THAT REPRESENTS THE POLYNOMIAL, AT POSITION "where"
//THAT RANGES BETWEEN INDEX 0 AND POLY.LENGTH-1
[Code] ....
Anyways, I'm not looking for straight answers. I just want to know which section I should start on first because I was advised to tackle programs one problem at a time.
View Replies
View Related
Jan 29, 2014
I Just made a simple code, but in any case it's not taking the input I'm trying to enter for the city.
View Replies
View Related
Jun 13, 2014
The method below is called within a large loop in another method. The code has been boiled down to easily show the problem I'm having. I found that the method worked fine (prompts user for input, gets input, returns input) until I added a statement at the end of the method that closes the Scanner object. When the kb.close() statement is present, the method works the first time through, but subsequent calls to this method only display the prompt and the Scanner object never waits for the user to input anything thereafter.
public int scanIt()
{
int i;
Scanner kb = new Scanner(System.out);
System.out.print("==> "); // prompt for input
if(kb.hasNextInt())
{
i = kb.nextInt();
System.out.println("User entered " + i);
}
kb.close();
}
View Replies
View Related
Jul 28, 2014
I'm having an issue, I have a scanner (Scan.nextLine();) that scans the console for input to fetch the string "word". Then I want to fetch a character using Scan.findInLine(word).charAt(number);. The problem is that the console requires me to write 2 lines in order for the program to move on. I only want the program to scan for a word, and then move on with what it has instead of requiring 2 inputs.
View Replies
View Related
Apr 7, 2015
The project is a program that allows the user to enter students and enter grades for each student. One of the requirements is that if there is already a grade stored for the student that it will display the previous grade. IF the user then enters a new grade the new grade will be stored. IF the user simply presses enter (enters an empty string) nothing is done. I have everything working except for the requirement of doing nothing if the user enters an empty string. If I just press enter at this point I get a NumberFormatException.
The below code is a method "setTestGrades" from the student class. This method iterates through each student object stored in an array list and then checks if there is a previous grade (Requirement# unset grades have to default to -1) before allowing the user to set a new grade.
public void setTestGrades(int testNumber) { //Sets the grade for the specified test number for each student in the arraylist.
testNumber -= 1;
Scanner input = new Scanner(System.in);
for (int i = 0; i < studentList.size(); i++) {
System.out.println("Please enter the grade for Test #" + (testNumber + 1) + " For Student " + studentList.get(i).getStudentName());
[code]....
View Replies
View Related
Oct 23, 2014
public class DemoBlock{
public static void main(String[] args){
System.out.println("Demonstrating block scope");
int x = 1111;
System.out.println("In first block x is " + x);
[code]....
this is the error I'm getting..reached end of file while parsing. I think this is referred to as either an extra curly brace or that I am missing a curly brace but I'm not 100% sure.
View Replies
View Related
Nov 18, 2014
I have to read this CSV file into an arraylist. Arraylist i call weight. The data in the csv file has 200 double values which is the weight of all the 200 bananas. Since the csv file is a string i guess I cannot create an double arraylist from scratch, but i have to parse the csv file afterwards, right? Well this does not work and ill show you the code in a bit, just need to state all my issues.
My second issue is how to be able to print out the the weight items (in this case bananas) next to the weight of the bananas taken from the array? This has to be done by constructor i assume? I just dont know how constructors work with arraylists.
Id like it to look something like this in the output window.
Banana 34,55
Banana 43,55
Banana 23,74
and so on (200 times)
so these are my issues, how to parse and how to print the arraylist weights and the item banana in the output window with constructor. Here is the code.
package Main;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.io.IOException;
import java.io.Serializable;
import javax.swing.JOptionPane;
[Code] ...
View Replies
View Related
Jun 10, 2015
I have the bellow file. What I should do the parsing sentences from the given file?
[ A/DT form/NN ]
of/IN
[ asbestos/NN ]
once/RB used/VBN to/TO make/VB Kent/NNP
[ cigarette/NN filters/NNS ]
has/VBZ caused/VBN
[ a/DT high/JJ percentage/NN ]
of/IN
[Code] ....
View Replies
View Related
Dec 19, 2014
I am trying to merge two files together using the Apache Commons CSV library. I figured it was easier to use this than to write my own implementation.
The first problem is that the output contains the underlying class logic and as a results my output contains a bunch of un-needed data. How do I get rid of this? I just want the column and row data, nothing else.
Example
I want this:
customerNo, firstName, lastName, birthDate, mailingAddress, married, numberOfKids, favouriteQuote, email, loyaltyPoints
1, John, Dunbar, 13/06/1945, 1600 Amphitheatre Parkway Mountain View, CA 94043
But I get this:
CSVRecord [comment=null, mapping=null, recordNumber=1, values=[customerNo, firstName, lastName, birthDate, mailingAddress, married, numberOfKids, favouriteQuote, email, loyaltyPoints]]
CSVRecord [comment=null, mapping=null, recordNumber=2, values=[1, John, Dunbar, 13/06/1945, 1600 Amphitheatre Parkway
Mountain View, CA 94043
The second problem is that I just want the row Headers from the first file, but the row headers from my second file are still being included in my output. I used withSkipHeaderRecord(true), but it didn't have the desired results.
Code is below.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.ArrayList;
[Code] .....
View Replies
View Related
Jan 16, 2014
class thread1 implements Runnable{
Thread t;
thread1() {
t=new Thread(this,"my thread");
System.out.println("current thread : "+t);
t.start();
[Code] ....
I m getting an error : threadmain.java:44: error: reached end of file while parsing ....
View Replies
View Related
May 9, 2015
I thought of processing line by line using scanner and then split each line in the ":" . But then i stuck, i only need the right part of the ":" so that i can save it to a class e.g. John Doe, 133456593, married etc.
Java Code: Name: John Doe
TRN: 133456593
Status: Married
Income: 22500
Receipts:
Receipt ID: 1
Date: 25/2/2014
Kind: Basic
Amount: 2000
Company: Hand Made Clothes
Country: UK
City:London
Street: Cambridge
Number: 10
Receipt ID: 2
Date: 28/2/2014
Kind: Entertainment
Amount: 500
Company: Floca Cafe
Country: Italy
City: Rome
Street: Piazza
Number: 4 mh_sh_highlight_all('java');
View Replies
View Related
Mar 1, 2015
Java Code:
package inventory;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileNotFoundException;
public class Inventory
{
private static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
ArrayList<Product> InventoryList = new ArrayList<>();
Inventory I = new Inventory();
int option = 0;
View Replies
View Related
Oct 12, 2014
Every time i compile my program, i get these errors..
java:102: error: reached end of file while parsing
}
^
java:116: error: reached end of file while parsing
2 errors
I'm almost done with everything before the arithmetic part of the program. here is my code..
[import java.util.Scanner;
public class pdonahue_Numerology
{
public static void main (String[] args)
{
int month = 0;
int day = 0;
[Code] .......
View Replies
View Related
Apr 1, 2014
How to maintain quotes in a text file? If no source files .
Новый точечный .jpg
Prompt in what direction to go .... maybe you can use other languages to do it?
Заранее СПасибо!!!
View Replies
View Related
Nov 29, 2014
I am new to java coding. so reading the log file using java code.
1) I wish to parse a file, and find a value for variable e.g. [StorageVersion].
2) After finding the value, it will be pushed to database.
Log File Path e.g. C:Temp est.txt"
Sample Logs :
{
//NAME
[1.2.2917] := s['3.4.454545'];
//Parameter
[1.2.2947] := s['1.2.56778'];
//Enabled
[1.2.3910] := B['22.43434.12232'];
//MAXCLOUMNNAME
[1.2.4217] := N['1.7.899982'];
//StorageVersion
[1.2.2917] := K['706146.0.22106932'];
}
From above log file, the Value i need is "706146.0.22106932"
View Replies
View Related
Feb 14, 2015
package jdbc;
import java.sql.*;
import javax.sql.*;
import java.util.*;
public class Jdbc {
public static void main(String[] args) {
[code]....
View Replies
View Related
Feb 25, 2014
I was given a text file that has list of names phone numbers, calls in and out etc... Like this
Adams#Marilyn#8233331109#0#0#01012014#C
Anderson#John#5025559980#20#15#12152013#M
Baker-Brown#Angelica#9021329944#0#3#02112014#C
The # are delimiters between data items and each line has the call status as the last item. I need to know how I can display each persons information on the screen in a format such as:
Name Phone Calls Out Calls In Last Call
Marilyn Adams (823) 333-1109 0 0 01-01-2104
John Anderson (502) 555-9980 20 15 12-15-2013
Angelica Baker-Brown (859) 254-1109 11 5 02-11-2014
I have to use substring method to extract the phone number and add parentheses/dashes ect I also must have a while statement and a delimiter...
So Far my code looks like this Also I am in a beginners Java coding class....
import java.util.Scanner;
import java.io.*;
public class phonedata2_1 {
public static void main (String[] args) throws IOException {
String Phonefile, FirstName, LastName;
Scanner PhoneScan, fileScan;
[Code] ....
View Replies
View Related
Jul 4, 2014
Java Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class PrintALongFile{
public static void main(String[] args) throws FileNotFoundException{
File inFile = new File("C:VeryLongFile.txt");
[Code] .....
When I try to read from a file that's 4,075,904 lines long and 41,646KB in size, it wont go past line 1,266,471. The above code is simplified from my actual program, which actually prints out the results to another file. However, the console (Eclipse IDE) and the output file, both show that it's stopping at the same line.
How can I read the to the very end of my file?
View Replies
View Related
May 26, 2014
I have a simple txt file, each line simply containing 1 word.I would like each work to represent an index of the array..im having some difficulty populating an array from either a txt file or a scanner.i seem to be able to fill the scanner so to speak with the contents of the text file but not the array. I don't know how to syntax it
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Arrays;
public class ReadFile
public static void main(String[] args) {
[code]....
View Replies
View Related
Jul 26, 2014
I get a null pointer error on line 17 of the following fragment. I suspect it has to do with the way I am reading the file, but I'm unsure. I can provide more information or code if necessary.
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode( JFileChooser.FILES_AND_DIRECTORIES );
int userInput = jfc.showOpenDialog( null );
if( userInput == JFileChooser.CANCEL_OPTION ){
[Code] .....
View Replies
View Related