Console Input Of Strings Using Scanner

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


ADVERTISEMENT

Scanner That Scans Console For Input To Fetch String Word

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

Cannot Find Symbol - Static Scanner Console

Jan 11, 2015

I am attempting to write a application that asks user input of a decimal, and outputs a value rounded to the nearest integer of the input.

Here is what I have so far, and I am seeing an error at Static Scanner Console that says Cannot Find Symbol for the class Scanner. and I am not sure how to resolve it.

Note the program so far is only asking for input - I have not gotten to the output part yet.

public class IT145Exercise13 {
static Scanner console = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) { 
double decimal;
int rounded;
 
[Code] ......

This is my second Java program (First was HelloWorld, which went really well )

Seems I need to include
import java.util;

when I do, the error at the console line goes away.

I am not clear what this import statement is doing so will check that out.

View Replies View Related

Manipulating Strings In A File Scanner

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

Getting Scanner Class To Accept Strings

Feb 2, 2014

Basically, I'm just trying to let the user enter the name. It crashes whenever the input isn't an int. Also, here's the bit of code I'm using that has to do with that. I don't feel like putting the entire class, so here's that bit of code:

PHP Code: String yourName = user.nextLine(); mh_sh_highlight_all('php');

Anyway, I just haven't figured out how. Also, the scanner is called user in this class.

View Replies View Related

Console Calculator Program - How To Split Array Of Strings

Apr 22, 2015

I'm writing a command line application of a calculator in Java. I need to be able to (as per program requirements) split the array of strings (6+3) into an array of strings? How would that be done? I know you can use the split method, but I am not really sure how that wold work to perform math with them?

On a side note: for this application, how could you make the program accept a variety of different lengths of strings to perform math with them?

View Replies View Related

Delimiter - Type In 5 Strings From Java Console And Print Them All Out

Feb 13, 2015

I am testing some stuff here is my code:

public static void main(String[] args) {
String[] a = new String[5];
//String input = JOptionPane.showInputDialog(null, "enter input");
Scanner scanner = new Scanner(System.in);

[Code] ....

I am trying to type in 5 strings from java console and print them all out. I am using a "/" as a delimiter. My problem is that it does not print any output after I type strings separated by "/" like this: hello/gea/cae/eaf/aer and press enter. It works if I use JOptionPane but from the console its not working. I want to use the console instead of JOptionPane for this one.

View Replies View Related

Scanner Taking Input Twice

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

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 View Related

Use Scanner To Get Input And Distinguish Between Int And Text?

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

Checking To See If Input From Scanner Is Integer

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

Creating A Scanner And Putting Input Into Arrays

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

Polynomials Into Array Using User Input With Scanner

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

Failing To Read Input With Scanner NextLine

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

Scanner Object Never Waits For User To Input Anything

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

How To Input Data From Console

Nov 7, 2014

I want to input character data from the console without using the BufferedReader class. I tried using the Scanner class but the compiler shows an error. This is what I tried:

sc.nextCharacter();
sc.nextcharacter();
sc.nextChar();
Sc.nextchar();

Is there any way I can input character data without using the BufferedReader class?

View Replies View Related

Input From Console To Array List?

Dec 10, 2014

I have this very simple application just to test console input:

import java.util.ArrayList;
import java.util.Scanner;
public class WriteTester {

[Code]....

When I let it run, only every third entry is put into the array list and I have to hit "enter" three times for the "break" in line 21 to trigger. I cannot find out why.

View Replies View Related

Limit On String Input From Console

Mar 2, 2015

I am trying to make a program in which first I am entering number of charachters and then in nextline their is exactly that number of characters should be enter after than program should stop taking input from console..this is I have try so far

private static ArrayList<String> chars;
static String inputdata;
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
chars=new ArrayList<String>();

[code]....

View Replies View Related

Can't Give Input A File By Console

Sep 28, 2014

this is my code...in my code i cant give input form my console...it takes input but it doesn't save in a target file..My code is :-

package filetester;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
 
[code]....

View Replies View Related

Console Input Menu Unresponsive On Recall

Nov 28, 2014

I am currently tackling homework for a small I/O-program with console input.Right now, only the part when you enter "E" or "e" on the console is implemented and the issue I am trying to tackle is described in the comment starting at line 38:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;

[code]....

View Replies View Related

Reading Input With Scanner And Checking If User Entered Empty String?

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

Code Not Running Scanner NextLine After Using Scanner NextInt?

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

Sorting Array Of Strings From User Input

Apr 3, 2015

I am working on an assignment where I initially have to take as input a number. That number will be the number of strings the user is going to type. As they are typed, they are to be sorted alphabetically, then printed. It took me a while to get this far, and I am stuck on how to properly invoke the other methods in main. There has to be at least three methods: main, one to do the sorting, and one to print the new array. I'm quite sure there are mistakes in my code, but this is definitely a challenge for me. Here is the code. Keep in mind that the methods being invoked are blank on purpose.

I am not allowed to use the Arrays, Arraylist or Collection class. This is basically to test my ingenuity and to see how arrays actually work.

import java.util.*;
public class Sort_as_Inserted {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of elements to be sorted: ");
String element_number = input.nextLine();

[Code] ....

View Replies View Related

Empty Strings Not Accepted On User Input

Apr 7, 2014

I've been trying to get my program to not accept empty strings when the user inputs, but I'm having some difficulties. I've tried for loops, as well as while loops.

import java.util.Scanner;
public class GasMileage
{
/**
* displayIntro
* This method displays the introduction to the user
*/
public static void displayIntro()

[Code] .....

View Replies View Related

How To Get Input Strings To Print Out After Turn Them Into Tokens

Jun 23, 2014

how do i get the inputed strings to print out after i turn them into tokens ? I need it to ask for a number and then multiple lines of strings, turn them into tokens, then print them out.
 
import java.util.*;
public class TokenDriver{
public static void main(String [] args){
// TokenStore words = new TokenStore();
String[] tokens = new String[300];
 
[code].....

also as a follow up how would use an array in an other class (TokenStore) to save the typed line information

View Replies View Related

If Statement - Checking / Comparing Two Strings From User Input

Dec 7, 2014

I am trying to do is get a user to input their name into the system. Once the user has inputted the name into the system, the system should check to see if the name entered matches "alice". If this is true then the system should print "welcome to the system alice". If it is not alice it should not do anything.

Here is the code I have so far... The fault/ error seems to be with the sInput string in the IF statement.

import java.io.*;
public class Hellohuman
{
public static void main (String[] args) {
try {
InputStreamReader isr = new InputStreamReader(System.in);

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved