Reading Characters From User Input

Jan 11, 2012

I am using jdk 6 ... I came to know that how we can read strings from user input ... and how we can convert a string to int .. but i dont fount how to read a character by character from user ..

View Replies


ADVERTISEMENT

Reading User Input From JTextField As Int To Create Matrix

Sep 16, 2014

I am required to read user input to create two matrices which will eventually be added together. I am unsure as to how I can read the input from the user as an int and not a String. The input is from size 0-10 for both column and row size. Also, can you have a new button created in the actionPerformed method of a previous button?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Matrices implements ActionListener {
private JFrame win1, win2, win3, win4;

[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

Change Input String To Array Of Characters

Dec 11, 2014

I am trying to change an input String to an array of characters, but it only stores the word before the space into the array. Here is the code:

Scanner scanner = new Scanner(System.in);
System.out.println(" Enter text: " );
String text = scanner.next();
char[] characterArray = text.toCharArray(); // convert string to array of characters
String char = "";
for( i = 0; i < characterArray.length; i++) {
char = char + characterArray[i]
} System.out.println(char);

Just typing hello gives me hello, but when I type hello world it does not type in the word "world".I am trying to change an input String to an array of characters, but it only stores the word before the space into the array.Here is the code:

Scanner scanner = new Scanner(System.in);
System.out.println(" Enter text: " );
String text = scanner.next();
char[] characterArray = text.toCharArray(); // convert string to array of characters
String char = "";
for( i = 0; i < characterArray.length; i++) {
char = char + characterArray[i]
} System.out.println(char);

Just typing hello gives me hello, but when I type hello world it does not type in the word "world".

View Replies View Related

Java Program That Will Ask A User To Input Grades Until User Inputs Sentinel Value

Apr 9, 2014

Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".

View Replies View Related

Program That Takes User Odd Number And Print Prime Numbers Lower Than User Input

Nov 4, 2014

I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.

public class PrimeNumber
{
Scanner scan = new Scanner(System.in);
int userNum;
String neg;

public void getUserNum()

[code]...

View Replies View Related

Reading From Standard Input?

Jun 20, 2014

I have to write sorting algorithm for numbers given on standard input(console) with following requirements:

The application must remember the distinction between double and integer numbers. This must be evident in the sorted output. Double type numbers are written using a period (.) to separate the fraction from the whole part.

The problem is I don't know how to efficiently distinct whether something is an int or double on standard input, to save them into 2 different arrays.

Only idea I've come to so far is to read each number as String and check if it has '.' character, to figure out if it's double and than convert it into right type and add it to correct array.

Also thought that maybe this would be achieveable using Pattern but how to do that?

View Replies View Related

Where To Input Segment To Get User Input Dialog

Apr 9, 2015

Where do I input this segment?

Scanner user_input = new Scanner( System.in );// This line goes at the top with the other global variables

public void getUserInput(){
System.out.print("Enter litres: ");
litres = user_input.nextDouble( );
System.out.print("Enter mileage: ");
mileage = user_input.nextDouble( );
System.out.print("Enter speed: ");
speed = user_input.nextDouble( );

[Code] ....

This is my client file.

class Client{
public static void main(String []args){
Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params
Bike R2=new Bike();//without params
System.out.println(R1.increaseSpeed());//calling methods
System.out.println(R1.maxDistance());
System.out.println(R2.increaseSpeed());
System.out.println(R2.maxDistance());
}
}

View Replies View Related

Histogram Not Reading Input File

May 1, 2015

The following program should read in a file on my desktop (I have the path set in Netbeans to that location) and produce a Histogram. Instead I am receiving this error.

Exception in thread "main" javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at Histogram.main(Histogram.java:9)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)

Here is the code for the Histogram:

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class Histogram {
public static void main(String[] args) throws Exception {
int[][][] ch = new int[4][4][4];

[code]...

Why it's not reading the file?

View Replies View Related

Reading Input From JTextField And Passing It To Another Class

Sep 7, 2014

I am having trouble taking user input and passing it to another class as a variable. If I assign the value explicitly (see line 59), it works just fine. What I want to do though, is assign the input from inputField to the variable inputVariable. I tried using:

inputVariable = inputField.getText();

This does not work. Regardless of what is typed into inputField, the output I get is just:

"The user typed "

package example;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

[Code] .....

View Replies View Related

Reading Input From Text File Not Working?

Apr 7, 2014

public void savePlants(ArrayList flowerPack) throws IOException
{
Scanner input = new Scanner(System.in);
String name;

[Code].....

When I open the saved file the information I need seems to be saved, but when I try to load and search it the data is not there. This is homework that was due about 2 days ago. I just want to get it right in my head for next time.

This is my text file: ¬í sr java.util.ArrayListxÒ™Ça I sizexp w sr FlowerNÏŠ¨r;¾  Z hasScentZ hasThornsL flowerColort Ljava/lang/String;xr Plant"ô²Ò0¢ I IDL Nameq ~ xp t Roset redsr Fungus“ +) Z isPoisonousL fungusColorq ~ xq ~  t toadstool t brownsr Weed #©éÇÙN Z isEdibleZ isMedicinalZ isPoisonousL weedColorq ~ xq ~  t dandylion t yellowsq ~  t tulip t pinkxq ~ q ~ q ~ 

View Replies View Related

First Character Is Missing While Reading Input Stream

Jul 10, 2014

This is my code

// TODO Auto-generated method stub
File file=new File("D:/input.txt");
java.io.FileInputStream is=new java.io.FileInputStream(file);
int a=is.read();
try {
while((a=is.read())!=-1) {
System.out.print((char)a);

[Code] .....

My text in file is :my birthday Hello World

But in out i see only:y birthday Hello World

Why is first character is missing here?

View Replies View Related

Reading File Input - Display All Values

Apr 8, 2014

I've been trying to learn how to read in file input and have a question about this piece of code:

Java Code:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class readFile {

public static void main(String args[]) throws FileNotFoundException{
File file = new File("file.txt");
int sum[]=new int[5];
int i=0;

[Code] .....

"file.txt" holds the following information:

1
2
88
42
56
89

But my output looks like:

2
42
89

if I take out sum[i]=input.nextInt(); it will display all values in the file.

View Replies View Related

Not Reading All Lines While Comparing User Entered Name To The One That Exists In File?

Jan 21, 2014

Logic her eis when user selects option1, it asks for user to enter name and as soon as user enters name, it should compar name to existing names in txt file. I have user while loop and for loop but for some reason, it doesnt compare properly as there seems to be some mistake in looping and it just read first line or you can say single line rather than comparing it with all lines in txt file. i have attached votes.txt file with this. Also, if user doesnt exists infile, it will ask user to enter vote as yes or no and add it to file and then count total number of Yes and No votes from file and compare them.

my votes.txt file looks as below with two columns namely (name and vote).

Hiren No
samir yes
bob no
rikul no
master yes
patrick no
summer yes
bhanja no

package samples;

import homework.EmployeeA;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

[code]....

View Replies View Related

Reading User-specified Text File Into ArrayList Of Strings Using Java?

Apr 23, 2015

I am advised to use a while loop the Scanner method hasNextLine() and in the while loop body, call the Scanner method nextLine(), and add the returned String to the ArrayList of Strings. what I have gotten so far:

Scanner input = new Scanner(new File(""));
while(input.hasNextLine()) {
String line = input.nextLine();
System.out.println(line);

View Replies View Related

Reading Text File With Specific Input Format - Output Formatted Report

Apr 10, 2014

Here's a link to it : [URL] ....

The basic gist is it's "A program that reads in a text file that uses a specific input format and uses it to produce a formatted report for output."

Specifically :"For this lab you will write a Java program that produces a simple formatted report. The program will prompt the user to enter a file name. This file must contain information in a specific format (detailed below). Each "block" of the file contains information for one player in a competition -- the name of the player followed by a number of different scores that that player achieved. The program should find each player's average score, median score and best and worst scores and display them in a line on the final summary report. The program should also determine which player has the highest average score and which player has the lowest average score."

I get the following errors when I try and compile it:

Enter an input file name: Project11.txt
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException... -1
at java.util.ArrayList.elementData(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at Project11.getMedian(Project11.java:68)
at Project11.main(Project11.java:27)

I get that the error(s) reside in lines 68 and 27, among problem other areas, but I'm not exactly sure how I can fix them.

Here's my code:

import java.io.*;
import java.util.*;
public class Project11 {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
System.out.print("Enter an input file name: ");
String input = in.nextLine();

[Code] ....

View Replies View Related

How To Get User Input

Nov 13, 2014

I have been coding in my class at school (Grade 11 Computer science) and i just downloaded the program on my computer at home, unfortunately i cannot access my computer notes at home and i also dont remember certian specifics of coding, so my question is how would i get user input to create a program. The comments are the parts i dont remember. (I am trying to slowly build my memory with this stuff)

Here is my code so far:

import java.util;
[highlight=java]
public class hello_world {
public static void main(String[] args) {
string name;
//WHAT DO I PUT HERE????

[code]....

View Replies View Related

Asking User For Input Twice?

Apr 19, 2014

I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.

//Create Scanner object
Scanner keys = new Scanner(System.in);
//Get chips

[Code]....

*****This is what I get when I run it

run:

How much money would you like to change? 50

You now have $50 in chips.

What game do you want to play? You did not pick dice.

View Replies View Related

Can Control User Input

Oct 11, 2014

Is there a way you can control user input?

For example, is it possible to only allow the user to enter digits?

View Replies View Related

Formatting User Input

Feb 4, 2014

I have been given a piece of work to do for College that requires me to format user input. For example: This is an example of text that will have straight left and right margins after formatting

will transform into:

This..is..an.example
of..text..that..will
have..straight..left
and...right..margins
after.....formatting

The user inputs the width, in the case above being 20. Each line has to therefore be 20 characters long. All spaces are to be replaced with full stops.

This.is.an.example.o
f.text.that.will.hav
e.straight.left.and.
right.margins.after.
formatting..........
public class Main {
public static void main ( String args[])

[code]....

View Replies View Related

ArrayList - Get User Input And Sum Together

Jul 2, 2014

Goal is to get user input and sum together. I've tried it different ways. This is the one with the least amount of problems.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: java.lang.Integer.parseInt

ArrayList<Integer> leftCU = new ArrayList<>(); {
System.out.println
("Please enter one at a time the number of CU's for each class that is left to complete. Enter Q when done.");
leftCU.add(in.nextInt());

[Code] .....

View Replies View Related

Get User Input For A Char Value?

Jan 9, 2015

I am trying to get user input for a char value and am having some difficulty getting input for a char value.

Java Code:

//imports packages
import java.io.*;
import java.text.DecimalFormat;
class ModuleCulminatingTask {
public static void main (String args []) {
//declares variables
float var3, var4;
long var5 = (int)(Math.random()*10);

[code]....

View Replies View Related

Simple Input Value From User

Apr 10, 2014

I'm not sure what is wrong with my code:

import java.util.Scanner;
public class Main {
int age = 0;
System.out.println("Enter your age: ");
// Read in values
age = in.nextInt();
}

The in is underlined, telling me "cannot find symbol"

View Replies View Related

Getting User Input Second Time

Apr 19, 2014

I'm working on creating a dice game and getting the users input is giving me a really hard time. I have the main game working but this part I'm stuck on. The problem is that it's not giving me the chance to enter the second input when I ask for the game.

//Create Scanner object
Scanner keys = new Scanner(System.in);
//Get chips
System.out.print("How much money would you like to change? ");
int chips = keys.nextInt();
System.out.println("You now have $" + chips + " in chips.");

[code]...

This is what I get when I run it run/How much money would you like to change?

View Replies View Related

2D Array Input User For GUI

Mar 25, 2014

Java Code:

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
public class SortTable {
public static void main(String args[]) {
Runnable runner = new Runnable() {
public void run() {

[Code]...

View Replies View Related

How To Get User Keyboard Input

Sep 4, 2014

I created a scanner object but it's not asking for any input. when I create a new scanner and then tell it to print it it just prints a bunch of weirdness when AFIK it should be asking me to type something and then it should repeat what i entered.

View Replies View Related







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