Storing Input From User As String Variable And Then Converting It To Double?

Jul 8, 2014

I am trying to make a program that calculates the change due in dollars and cents. The user inputs both the amount due and the amount tendered. My program only works with whole numbers?

View Replies


ADVERTISEMENT

Converting String Input To Double

May 2, 2014

I am supposed to write a program on PuTTY using UNIX (I have Windows 8), but I am not comfortable with it yet, so I am using Java through NetBeans (IDE 7.4).

The program has to follow these instructions (ignore the Linux part of the instructions, the rest is in bold):

Write a program on the Linux system using the putty utility. The program should get strings of data from the command line (that is, look for the data in the "args" array of strings). Use a loop to convert each of the strings in the array into a double and add the number to a total. Print the total after all of the strings have been processed.

The program will use try-catch to catch any error that occurs. If an error occurs, the program will print a message saying that the error occurred. The program can end at that point.

You should create the Java program using the nano editor. The input data should be a list of numbers on the line that runs the program.

The problem so far is that I keep getting an error when converting a String value into a new double value. I have yet to code the try-catch method in my program

Java Code:

import java.util.*;
public class Program13Strings {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
System.out.println("How many lines of data do you wish to enter?");
int size = scan.nextInt();

[Code] ....

*NOTE: right where my code says double newDouble = Double.valueOf(newResponse); is where the error is occurring.

*ERROR: Exception in thread "main" java.lang.NumberFormatException: For input string: "abc"

at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1241)
at java.lang.Double.valueOf(Double.java:504)
at program13strings.Program13Strings.main(Program13St rings.java:21)

Java Result: 1

How can this be fixed so I don't get this error?

View Replies View Related

Getting User Input From TextArea And Storing It As String Value

Jan 20, 2015

I am having trouble with getting a user input once they enter it in to a JTextArea and then storing it in to a string value. See, I wanted the user to enter an answer in to the JTextArea, and then once I store it in to a string value, I would be able to use it later. I wanted to after storing the variable to print it on to a JLabel, but nothing is showing up... Heres my code.

static String usera[]= new String [10];
for (int i=0; i< usera.length; i++) {
//These are the JTextArea names
usera[0]= Question1.getText();
usera[1]= Question2.getText();

[Code].....

I already initialized the JLabels earlier in my code, and I did not feel the need to post all my code here. This is where I am trying to get the string variable and then printing it out.

View Replies View Related

User Input Data As String And If Verified As Valid Number Then Convert Into Double

Nov 11, 2014

I am writing a code in which a user inputs data as a string and that data must be verified as a valid number. A valid number is anything from 0-100. Then all valid numbers are converted into double numbers.

I am having trouble in how to write the validation part of the code.

Is it suppose to be an if, else statement? And if so how is it suppose to be validated?

View Replies View Related

How To Get Double Variable From Keyboard Input

Apr 8, 2014

I am testing to get a double value from keyboard input as below?

import java.util.Scanner;
public class EchoLine{
public static void main(String[] args){
double amount;
Scanner myScanner=new Scanner(System.in);
amount=myScanner.nextDouble();
System.out.println(amount);
}
}

I type 5.95 in Console View of Eclipse but as a result I get an error on line 9.

View Replies View Related

Attempted User Input Using A Double

Jan 13, 2015

For some reason, the below code is not working out for me. I thought I had the scanner right and Eclipse is only throwing an error on the last line that states "else(ernie == 0)". What am I missing here?

Basically just trying to take user input, incorporate some if and else if statements to return a line based on if the condition is met.

Java Code:

import java.util.Scanner;
//Importing the scanner now for user input
public class Ernie {
public static void main(String[] args) {
double ernie;

[Code] .....

View Replies View Related

Converting User Input Binary To Integer

Sep 7, 2014

I am new to java and programming in general. I figured out how to convert an integer input to binary however I am having issues doing the opposite of converting a user input binary number to a decimal.

I need to do this with basic math (or string depending on how I represent the binary) and no functions.

I know how to convert binary to integer on paper but I am having a hard time working it out in java.

View Replies View Related

How To Get User Input And Create Variable

Sep 16, 2014

Okay, so I know how to get user input and create a variable from the input, create a basic addition sum etc...

So I have this code:

package calc;
import java.util.Scanner;
class calc{
private static final double add = 0;
private static final double subtract = 0;
public static void main(String args[]){

[Code] .....

I'm basically trying to make it so that when the user enters tnum, tnum2, tnum3 and tnum4, their answer turns to the variable which has to be either true or false to make the boolean work. I don't know really how to do this. I want to make it so that if they enter yes, then that makes the boolean true and the numbers will multiply and create the answer variable. If they enter no then the boolean is false and it moves onto the next if statement. How can I do this?

View Replies View Related

Allowing User To Input A Number And Assign This To Int Variable

Jul 4, 2014

I need the user to be able to input a number, and for the program to assign this value to an 'int' variable. I know how to do this with a 'string' variable:

Java Code:

String options = JOptionPane.showInputDialog(null, "In your decision, how many options do you have?
" +" (NOTE: The maximum number of options = 5, and you must enter your answer as a numeral.)"); mh_sh_highlight_all('java');

But I need to know how to do this with an 'int' variable.

View Replies View Related

Variable Fields Are Not Holding Information Through Constructor From User Input?

Oct 19, 2014

Variable Fields are not holding information through constructor from user input so here's what I did instead.

View Replies View Related

Write A Statement That Reads User Input Integer Into Defined Variable

Jan 20, 2015

so i'm following a java tutorial from the book and it has a few challenge questions. and i'm stucked on one. i think i just don't understand what is it that its asking me. heres the question, Write a statement that reads a user's input integer into the defined variable, and a second statement that prints the integer. assuming scanner is given, and i checked my heading code is ok.
 
Scanner scnr = new Scanner(System.in);
int userNum = 0;
System.out.println("What is the product of 8 time 2");
userNum = scnr.nextInt();

[code]....

View Replies View Related

Get A String From User Input?

Apr 13, 2014

I trying to get this code to get user input instead of reading from a hardcoded array. I'm getting compile errors while trying to get user input. Here's some of the code:

Java Code: // BubbleSort Java
// compile with: javac BubbleSort.java
// run with: java BubbleSort

[Code]....

View Replies View Related

Converting Double Value To A 2 Decimal Place Value

Jan 28, 2015

I am trying to convert the double grossPay to 2 decimal places but cannot get it to work ,I am unsure of the correct way of doing this but this is how far I can get

public static double grossPay(double Orate, double overtime, double salary) {
double grosspay = 0;
grosspay = (Orate * overtime) + (salary / 26);
DecimalFormat df = new DecimalFormat("#.##");
return(df.format(grosspay));
}//closes grossPay method

View Replies View Related

Evaluating User Input For String Or Int

Jan 14, 2015

I have to take a users input. The general gist of the problem is I want to convert a decimal number entered by the user inputs into it's binary equivalent....the conversion part I know how to do.

I'm stuck at the user input phase. If the user inputs a decimal number within the correct range (lets say between 0-5000 for this example) the conversion goes ahead as planned and the program outputs on the screen the binary equivalent.

If on the other hand the user inputs a number outside of this range ... OR a String OR and empty space, an error message is given and the user is asked to try again. I don't know how to handle it if the user enters the different types...int or String.

View Replies View Related

Converting Double Values (0.0-110.0) To A 4.0 Scale (as Used In GPA Calculation)

Sep 3, 2014

I am new to programming, but am working on a program that takes 6 inputted grade averages on a scale of 0.0 - 100.0 (exceptions in the case someone has exceeded the traditional 100.0 limit) and then divides it by 6 and returns the value in a dialog box. However, I want to take the values inputted and convert them to a 4.0 scale (90.0-100.0 = 4.0, 80.0-89.99 = 3.0, etc.) and then get the average of that so that I can return an average on a 4.0 scale. I have been trying to accomplish. Also, as of right now my program only prompts the user for 6 inputs, but I'd like the user to be able to input as many as he/she would like to, I am pasting the code I currently have below.

import javax.swing.JOptionPane;
public class GPA_Calculation1_0 {
public static void main(String[] args) {
String courseOne = JOptionPane.showInputDialog("Enter numeric GPA of Course One: ");//Course one
double numOne = Double.parseDouble(courseOne);

[Code] ....

View Replies View Related

Calculating Average Of Two Ints And Converting To A Double

Oct 31, 2014

I have been asked to construct an application that will read in the exam mark and coursework mark and then print out to 1 decimal place the average of the coursework and exam mark. When I submit it it says it's incorrect.

class Main
{
public static void main( String args[] )
{
int ExamMark=71;
int CourseworkMark=40;
double mark = (double)(ExamMark + CourseworkMark) / 2;

[Code] ....

View Replies View Related

Scanning Known String And Not Input Typed By User?

Jul 29, 2014

I'm having an issue with my program that needs 2 lines of input when I only want one. First I call Scan.nextLine(); to fetch a word. Then I want Scan.findInLine(word).charAt(number); to scan the string word for a letter instead of having to put another line in the console. How do I make Scan.findInLine(word).charAt(number); scan a single String instead of scanning input typed by the user?

View Replies View Related

Storing Value In Long Type Variable

Mar 31, 2014

How can I store a value in long type variable whose range is greater than int type variable ....

View Replies View Related

Calculating Average Of Two Ints And Converting To Double In Java?

Oct 31, 2014

how to use Java and for my coursework I have been asked to construct an application that will read in the exam mark and coursework mark and then print out to 1 decimal place the average of the coursework and exam mark. When I submit it to the coursework submission system it says it's incorrect.

The answer that it's asking for is the answer that my code produces (ex = 71 cw = 40 mark = 55.5) But for some reason it says this when I submit it:

-#### << Differences between expected (<) your answer (>) >> ------------
1c1
< ex= 91 cw = 80 mark = 85.5
---
> ex = 71 cw = 40 mark = 55.5
-------------------------------------------------------------------------

Check the above output for why this attempt failed And when I change the ex to 91 and cw to 80 it asks for > ex = 71 cw = 40 mark = 55.5 again.

class Main
{
public static void main( String args[] ) {
int ExamMark=71;
int CourseworkMark=40; 
double cost = (double)(ExamMark + CourseworkMark) / 2;
  System.out.printf("ex = " + ExamMark + " cw = " + CourseworkMark + " mark = " + cost);
System.out.println();
  }
 }

View Replies View Related

Continuously Get User Input Until Empty String Entered

Jul 26, 2014

I have the code below that just keeps getting the user's name and displaying it until the user enter's an empty string. Well, to simulate that, I just hit the keyboard instead of entering any name but for some reasons I am not seeing in my code, the programme just keeps looping.
 
System.out.println("Enter your name :
");
Scanner st = new Scanner(System.in);
while(st.hasNext()){
System.out.println("Enter your name :
");
String name = st.nextLine();
System.out.println(name);
if(name==" ") break;
}
System.out.println("you are out of the while loop now!!");

View Replies View Related

Business Application - Get Input From User And Converts It Into A String

Apr 24, 2014

So this application gets input from the user and converts it into a string. It then uses various methods to set instance variables of this class and ends up printing those classes to display information to the user about that products.

package javaapplication5;
import java.text.NumberFormat;
import java.util.Scanner;
 public class Product
{
private String product;
private String description;
private double price;
 
[Code] ....

I am getting a logical error. My output is this:

Enter a product for information. Choose either Java or C++
java
Prduct: null Description: null Price 0.0

View Replies View Related

How To Compare Int To A Empty String To Input Error Message To User

Apr 16, 2015

I made a guess a number program but I am having issue figuring out a way that when a user enter's in nothing for the program to spit out a message saying "hey entering nothing doesn't work try again" then ask for input. I have done some research and from what I have found is to read the input in as a String rather than int, and use something like Integer.valueOf() to get the integer value but I am completely lost on how to apply that to my program here is my code

//import statements
import java.util.*; //for scanner class
// class beginning
public class Guess {
public static void main(String[] args ) {
//Declare variables area
int guess, secretNumber = (int) (Math.random() * 10 + 1), lowGuess,highGuess;

[Code] ....

View Replies View Related

Hangman Game - How To Scan The String For Input Supplied By User

Dec 9, 2014

My assignment is to create a hangman game. One of my first difficulties with it is how to scan the string for an input supplied by the user.

Here is my method:

//Guesses that character c is in the secret word.
//Updates all instance variables accordingly,
//and returns true if c is present in the secret word.
public boolean guessCharacter(char c){
}

View Replies View Related

Storing Multiple Rows Of Table In Java Variable

Feb 3, 2014

Can I store multiple rows of a table in a two dimensional string array in java.. The table has 3 columns and approximately 10,000 rows.. Some pointers for coding to achieve this...

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

Retrieving Result By User Input Different Combination Using String Tokenizer (MYSQL)

Mar 30, 2014

I am creating a simple Symptom Checker application. The problem I have is that I'm trying to retrieve user input (JTextField) by comma's using StringTokenizer which contacts the database for a result which matches the user's input (SELECT * FROM DIAGNOSIS WHERE ?, ?, ?) . It successfully finds the correct result however only in a particular format. Not different combinations....

for example, if I enter say within the JTextField: "tearful, nausea, lack of motivation" it will find the result successfully (as that is how it is formatted within the particular column (in the database table) i wish to display a result from) however, if i enter a different combination of these symptoms: "nausea, lack of motivation, tearful" - it will not find any result. I'm very unsure how to make it work regardless of what is inputted first, second or last.

Here is the code:

public void actionPerformed(ActionEvent e) {
try {
String abc = fieldsymp1.getText();
StringTokenizer str = new StringTokenizer(abc);
while (str.hasMoreTokens()) {
str.nextToken((", ")).trim();
 
[Code] ....

View Replies View Related







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