Identifying Difference Between Integers And Words From User Input

Feb 24, 2015

I am currently taking a computer science class in high school, and its my first time programming (2 weeks since i started). I have currently finished my program however there were a few details that bothered me. I was supposed to create a program which can calculate average of individual marks and then calculate all of the average marks previously entered by user. However I noticed something that bothered me, which was the fact that if i were to enter a number when asked to enter a name, it would assume that number is a name. Also when I asked for the 5 marks the program would run fine until an error occurs if i accidentally type in a letter or word. I have done some research and found system scanners however i still don't understand the concept in a looping situation. Here is what I have so far.

import java.io.*;
public class loopingEx6Final {
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
double average, totalaverage = 0;
int Mark, Marktotal=0, marktotalsum=0, count=0;

[Code] .....

View Replies


ADVERTISEMENT

Counting Number Of Words From User Input

Jan 2, 2015

I tried the following code, OK:

Java Code:

String temp = "hi this sf hello is new what is this";
String[] cmd = temp.split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

However, when i get the input from user , i didnt get the expected result:

Java Code:

System.out.println("Enter the input string to count the words: ");
String[] cmd = new Scanner(System.in).next().trim().split("s");
int num = cmd.length;
System.out.println("number of words are: "+num); mh_sh_highlight_all('java');

Now Result for above code:

Enter the string to count the words:

hi this is new

words are: 1

View Replies View Related

How To Take User Input As Integers

Jun 2, 2009

Suppose I write a program to multiply two numbers.

The user enters the numbers as follows:

2
3

How do I extract these integers inside main? [Explain the case where the integers are on two separate lines].

What to do for the newline inbetween?

View Replies View Related

Sort User Input 3 Words Alphabetically And Output Middle Word

Feb 8, 2014

I have a project requiring me to build a program having a user input 3 words, sort them alphabetically and output the middle word. I have done some searching and seem to only come back with results for sorting 2 words. I so far have code to get the user input but I am completely lost as to how to sort them alphabetically.

import java.util.Scanner; //The Scanner is in the java.util package.
public class MiddleString {
public static void main(String [] args){
Scanner input = new Scanner(System.in); //Create a Scanner object.
String str1, str2, str3;
System.out.println("Please enter three word words : "); //Prompt user to enter the three words

[Code]...

we havnt done arrays yet and I THINK i have to do compareTo.....how to use it?

View Replies View Related

Print Maximum And Minimum Values Of Integers From User Input

Apr 27, 2015

So I'm learning java from a website and I was tasked with creating a simple program which allows the user to enter a series of integers, then finally when they decide to input a non-integer the program will print the maximum and minimum values of the integers they entered. So for example if they entered 5, 4, 3 and 2 then enter a non-integer the program would output 5 (maximum value), then 2 on a new line (minimum value).

Here is my code:

import java.util.Scanner;
public class MaxMinPrinter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

[Code] ....

And this is what the output looks like:

Actual output
-------------------------------------------
Enter an integer: 5
- 10
-
- Enter an integer: -4
- 8
- -6
-
- Enter an integer: 11
- -1
-
- Enter an integer: q
- -1
- -6

When it's supposed to look like this:

Expected output
-------------------------------------------
Enter an integer: 5
Enter an integer: 10
Enter an integer: -4
Enter an integer: 8
Enter an integer: -6
Enter an integer: 11
Enter an integer: -1
Enter an integer: q
11
-6

View Replies View Related

Allow User To Input One Or More Integers And Store Them In Vector For Manipulation Later On In Program

Oct 1, 2014

I'm playing with vectors for the first time... What I'm trying to do is to allow a user to input one or more integers and store them in a vector for manipulation later on in the program... Here's the portion of the program I'm working with:

Java Code:

package com.itse2317;
import java.util.*;
public class VectorTest
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

[code]...

My question is this: Is there any way to move from inputting integers to printing them, without entering a non-integer (for example, hitting enter)? I looked at the API for the Vector class, and either I'm not thinking about the problem the right way to be able to find an answer, or it's just not there.

View Replies View Related

Identifying Hashes That User Has Entered

Jun 18, 2014

Before continuing a task, any more accurate way to identify hashes? Currently, I am using regex + length of hash to give the user possible hashes.

Identifier class:

package votek;
* Purpose: Class is used to run checks on possible hashes that the user has entered.
*/
public class Identifier {

//Hash entered by user is stored here
private String hash;
//String that will contain all possible hashes to share with user
private String results = "Possible Hashes: ";
//Method that runs the other hash methods checks and prints out the results.

[Code] ...

View Replies View Related

Writing A While Loop To Read Positive Integers From User Until User Enters EOF Character

Feb 24, 2014

import java.util.Scanner;
public class Project_5
{
public static void main (String[] args)
{
Scanner input= new Scanner (System.in);

[code]....

So I'm attempting to have this program take the users input of integers, pressing enter in between each one. If they enter a negative number it lets them know that its invalid. At the end of the program it takes all of the valid integers entered and must add them and average them. I've put the final println in there as a placeholder. If the user types in "6" then presses enter and types in "3" , the output is:

There were 3 valid numbers entered.
The sum of the valid numbers was --- and the average was ---.
There were 0 invalid numbers entered.

It then continues on allowing the user to enter more values. Here is the example output for correct code"

Enter a positive value (EOF to quit): 4
Enter a positive value (EOF to quit): 7
Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 2
Enter a positive value (EOF to quit): -1
The number "-1" is invalid.

Enter a positive value (EOF to quit): 8
Enter a positive value (EOF to quit): 0
Enter a positive value (EOF to quit): -4
The number "-4" is invalid.

Enter a positive value (EOF to quit): CTRL-D

There were 6 valid numbers entered.
The sum of the valid numbers was 29 and the average was 4.83.
There were 2 invalid numbers.

View Replies View Related

Applet Java GUI Building To Display Words To User

Nov 17, 2014

Implement an applet that display historical facts. The applet will include facts from october 28 to december 4th to test the applet.

What is the best thing to add text in my GUI - JLabel, JTextArea? I am trying to display different words on my csFacts. Once a user click button yesterday and button tomorrow?

public class Facts extends JFrame implements ActionListener{
private JButton button1,button2;
private JPanel panel;
private JLabel label;
TextField ri = new TextField(50);
private void createGUI(){
setDefaultCloseOperation(EXIT_ON_CLOSE);

[Code] .....

View Replies View Related

Input Ten Words And Output Them Backwards In MSDOS Environment

Sep 4, 2014

how would a program code look like, input ten words and output them backwards in a msdos environment. My code this far is:

package baktext;
import java.util.*;
import java.io.*;
import java.awt.*;
//public static String baklänges(String s) {
//}
public class Baktext {

[Code] ....

View Replies View Related

I/O / Streams :: Program Is To Find Most Frequently Used Words Across All Input Files

Jul 30, 2014

Program is to find the most frequently used words across all the input files, where each word must appear at least once in each file.

View Replies View Related

Input A Sentence And Print Words That Start With Vowel - Java Error On Execution

Jul 23, 2014

This is a program to input a sentence and print the words that start with a vowel.......There is no syntax error but on executing and typing the sentence and pressing enter,I get a error saying :

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(String.java:658)
at sentence.main(sentence.java:22)

[Code] ....

View Replies View Related

Write A Java Program To Input 7 Integers?

Feb 21, 2015

Design, write a java program to input 7 integers and, for each integer, calculate and display its square and cube.

View Replies View Related

Program That Accept Words And Their Meanings Or Display List Of Words In Lexicographical Order

Apr 15, 2015

Write a menu driven program that either accepts words and their meanings, or displays the list of words in lexicographical order (i.e. as in a dictionary). When an entry is to be added to the dictionary you must first enter the word as one string, and then enter the meaning as separate string. Another requirement - from time to time words become obsolete. When this happens, such word must be removed from the dictionary.

Use the JOptionPane class to enter the information.

Use the concept of linked list to carryout this exercise. You will need at minimum the following classes:

- A WordMeaning class that hold the name of a word and its meaning.
- A WordMeaningNode class that creates the node of information and its link field.
- A WordList class that creates and maintain a linked list of words and their meanings.
- A Dictionary class that test your classes.

For the output, the program should produce two scrollable lists:

- The current list of words and their meanings.
- The list of the deleted words. You need not list the meanings, just the words.

So far, I have everything coded except for the remove method, and I am not sure how to code that. I coded the add method already, but now I don't know where to begin with the remove method in my WordList class. My classes are below.

WordMeaning Class:

public class WordMeaning {
String name;
String definition;
WordMeaning(String t, String d) {
name = t;
definition = d;

[Code] .....

View Replies View Related

CountDigitx - Take Two Integers As Input And Return How Many Time X Occurs In Number N

Apr 29, 2014

Write a recursive Java method countDigitx that takes two integer numbers as input (a number n and digit x) and returns the number of times digits x occurs in number n:

For example if n=23242 and x=2 then the method should return 3 that is the number of times the number 2 found in n.

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

Write A Program That Prompts User To Enter Two Positive Integers And Prints Their Sum

Jan 9, 2015

Write a program (TwoIntegers.java) that prompts the user to enter two positive integers and prints their sum (by addition), product (by multiplication), difference (by subtraction), quotient (by division), and remainder (by modulation). When the user enters 5 and 3, the output from your program should look exactly like the following:

Enter two positive integers: 5 3
The sum is 8.
The product is 15.
The difference is 2.
The quotient is 1.
The remainder is 2.

import java.util.Scanner;
public class TwoIntegers {
public static void main(String[] args) {
System.out.println("Enter two positive integers: ");
Scanner userInput = new Scanner("System.in");
int integer1 = userInput.nextInt();
int integer2 = userInput.nextInt();

[code]....

View Replies View Related

Add Integers To ArrayList And Prompt User To Delete Specific Numbers Not Required

Jan 21, 2015

This program simply adds integers to an ArrayList and then prompt the user if they would like to delete specific numbers that they don't want.

The output that I get when I want to delete numbers like 2 and 4 from 1 2 3 4 5 is 1 2 3 4 5 instead of 1 3 5.

Java Code:

import java.util.ArrayList;
import java.util.Scanner;
public class AL
{
// A regular array like int arr[] has to have its size declare before run-time and it's not dynamic which mean it can not grow or expand on its own.
static Scanner input = new Scanner(System.in);
// You have to use reference types(Integer, Double, Float); not primitive types(int).

[Code] .....

View Replies View Related

Identifying Classes And Attributes

Mar 1, 2014

I am new to Java and I am doing an assignment to identify Class and Attributes from below example. How to identify 7 classes and its attributes from this scenario:

ABC Maps Maker produces electronic maps for global positioning systems. Every map needs to define the latitude and longitude of the centre of the map, together with the length and breadth of the map. A map also has a name, and a set of geographical features.

A geographical feature is something noticeable in a a map; e.g., a hill, or valley. Among the types of features are the following: trace features, track features and tract features.

All features have a name that is displayed on the map next to the feature. A trace feature has a coordinate point to indicate its location relative to the centre of the map. Broadcasting stations, mountain peaks, and transmission towers, are examples of trace features. Every trace feature has a description associated with it.

Examples of track features include roads, railways and rivers. Each track feature has a list of points that define its course, and a line pattern. The line pattern specifies the colour, and the thickness.

Like a track feature, a tract feature also has set of points, except that when drawn on the map, the last point is linked to the first point to enclose a complete region. Additionally, it has a fill pattern which incorporates essentially a colour.

Recall that there is a class, Point, in the java.awt package – this can be used to hold the co-ordinate of a point

Class:
Attributes:

View Replies View Related

Read Input File And Create Output With Anagram For Words In File

Sep 24, 2014

Well my code is supposed to ask for an input file and then (ex: input.txt), read the input file and create an output.txt file with the anagram for the words in the file. Also it should be displayed on the screen. However my code doesn't display the anagram on screen or the output file!

Heres is the code:

import java.io.*;
import java.lang.*;
import java.util.*;

/* This program will read a file given by the user, read the words within the file and determine anagrams of the given words. If the file that the user inputs is empty, then the program will output "The input file is empty."
* The program will read the file line by line, counting the total number of words read. If there are more than 50 words, "There are more than 50 words."
* will be printed, and the program will terminate. After each line is read, the words in the line will be separated,punctuation characters will be removed, and upper case characters will be switched to lower case.
* If any word is larger than 12 characters, that word will not be considered in the total amount of words in the file and it will not be sorted.
* After each word is read, the letters will be sorted and stored into an array containing each
* word's 'signature'. After all the words have been read, words will be printed to the output file on the same line based upon their signature.
*/

public class Anagram {
//Creating constants for maximum words in file and maximum chars in word
public static final int MAX_CHARS = 12;
public static final int MAX_WORDS = 50;

[Code] ....

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

Identifying Right Key For Objects / Classes In Application

Jun 12, 2014

I recently had to work on a very simple banking application. Since customers provide their account numbers when they go to the teller to perform a transaction, I chose account number as the key to uniquely identify a customer. To address the fact that a customer can have multiple accounts, I decided to have an entry for every account the customer had, as part of my Teller class, so that he can identify himself with any of his active account numbers. When I was discussing this design with my friend, he felt it was bad and that I had to have a CustomerID field to uniquely identify a customer. I can't seem to understand why we can't do that with account numbers belonging to the customer?

View Replies View Related

Identifying File Type By Reading Its Content

Oct 27, 2014

Is there a way of detecting the file's type (whether it is a pdf, jpg, png,or anything else) by reading the content of the file. We could read the extension of file to determine it's type, but then extensions can be forged too. So I would like to know if the content inside a file is of a particular type or not. I'm not sure about this, but I have heard somewhere about each having having a specific kind of header which determines its type. So is it possible to read that header and determine it using a program?

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







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