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 ..
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;
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());
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".
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".
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;
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?
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()); } }
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];
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:
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.
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
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);
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();
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????
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.
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
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[])
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());
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?
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.