String Formatting - Enter Three Positive Integers Separated By Spaces
Sep 18, 2014
double a = scan.nextInt();
double b = scan.nextInt();
double c = scan.nextInt();
//**********************************Equations**********************************
System.out.println ();
double sum = a + b + c;
System.out.printf("Sum = %d", sum);
Heres the error I'm getting
Enter three positive integers separated by spaces, then press enter:
15 20 9
Sum = Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.Double
at java.util.Formatter$FormatSpecifier.failConversion(Unknown Source)
at java.util.Formatter$FormatSpecifier.printInteger(Unknown Source)
at java.util.Formatter$FormatSpecifier.print(Unknown Source)
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at project2.main(project2.java:52)
View Replies
ADVERTISEMENT
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
Mar 11, 2014
Write an application that inputs one number consisting of five digits from the user, separates the number into in individual digits and prints the digits separated from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each.
For example, if the user types in the number 42339,
the program should input 4 2 3 3 9.
Here is my code so far but I am stuck.
mport java.util.Scanner;
public class Seperating {
public static void main ( String[] args) {
Scanner input = new Scanner(System.in);
int number = 0 ;
System.out.printf("%d" , number );
System.out.print("Enter integer");
number = input.nextInt();
}
}
but I am not getting the result I wanted what am I doing wrong
View Replies
View Related
Apr 15, 2014
Having the following fields for my money class.
import java.text.DecimalFormat;
public class Money {
//Fields for money will hold dollars and cents
private long dollars;
private long cents;
My task is to use those fields and make a toString method that returns them like a dollars sign. For instance, if there are 32 dollars and 40 cents, then in my String method I have to return something similar to this "$32.40."
I have already tried some of the methods, but they don't seem to work fine.
public String toString() {
DecimalFormat formatter=new DecimalFormat("$#0");
DecimalFormat formatCents=new DecimalFormat(".00");
return formatter.format(dollars)+ formatCents.format(cents);
}
Code:
import java.text.DecimalFormat;
public class Money
{
//Fields for money will hold dollars and cents
private long dollars;
private long cents;
[Code] ....
Actually changed a little on my code and I believe strongly this should work; however, doesn't seem to. In my demo,
public class Dem {
public static void main(String[] args) {
Money myMoney=new Money(7.10);
System.out.print(myMoney.toString());
}
}
I pass this, but I get "$700.00" as the answer... confused...
My calculation is wrong in the toString method, but still the cents do not appear to be showing.
View Replies
View Related
Jan 23, 2015
I am just learning arrays or rather teaching myself about them and I am not to sure how they work and why. I understand that in order to set one up you do
int[] example1;
example1 = new int[]{01, 02, 03, 04};
But I don't really understand is why if you only need 3 data integers, why put the 4th one in?
Also with outputting data, I understand .print() and .println() but I am not to sure what .printf() does and then the formatting after that.
System.out.printf("%3d%11") // and then what you want to print
View Replies
View Related
Jan 27, 2014
I have something like:
xxx.x.x.x.x.33423.AMDAC-4
The number of dot separated segment in front of the string may vary, but it doesn't matter since I only need to extract the last two segments from the string (int this case : 33423 and AMDAC-4). How do I do this efficiently? I need to process hundreds of thousands of these strings every day. it is guaranteed that the segments will always be separated by dots only (no whitespaces in the string)
View Replies
View Related
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
Jun 14, 2014
I was working on my personal project when I realized I needed to split a string and extract 2 elements of it. The way it works is the user enters 2 numbers separated by a comma like this: 4, 8. Then I want to put the first number into a veriable called x and the next number into a variable called y. How can I do this?
View Replies
View Related
Feb 5, 2014
public Date(String s) {
???????
}
such that
Date("1250/345/759")
it will return
(1250,345,759)
in short Date("a/b/c")
return (a,b,c)
View Replies
View Related
Dec 29, 2014
I am trying for a logic that i have some emp ids as a string seperated by commas and i need the substring of emp ids as below. splitting the string as below.
public static void main(String args[]) {
String empId = "1,2,3,4,5,6,7,8,9,10";
int i;
int count = 9;
for (i = 0; i <= count; i = i + 3) {
System.out.println("Emp IDs are : " +empId);
}}
Result is:
Emp IDs are : 1,2,3,4,5,6,7,8,9,10
Emp IDs are : 1,2,3,4,5,6,7,8,9,10
Emp IDs are : 1,2,3,4,5,6,7,8,9,10
Emp IDs are : 1,2,3,4,5,6,7,8,9,10
But I want the result like:
Emp IDs are : 1,2,3
Emp IDs are : 4,5,6
Emp IDs are : 7,8,9
Emp IDs are : 10
View Replies
View Related
Jan 21, 2014
I am trying to split a string into a String[] tokens array to declare variables for an object; however, I'm having an issue getting the string to tokenize correctly. Here's an example of the input:
a : 100 : John Smith : 20 Main St.
a : 101 : Mary Jones : 32 Brook Rd.
Here is the basic code I have now, to properly sort each line of text, etc. (without the split() method):
Java Code:
while (scanner.hasNextLine()) {
currentLine = scanner.nextLine();
lineScan = new Scanner(currentLine);
if (currentLine.startsWith("/") || currentLine.trim().isEmpty())
continue;
[Code] ....
I was able to eliminate the comments and identifiers from the text by trimming the first two characters of the string. For the split, I tried String[] tempArray = currentLine.split("s+"); however, that also took the spaces out of the addresses and names...so the results looked like this:
100
John
Smith
20
Main
St.
As you can see, it splits via space regardless, including where I replaced all the :'s with spaces. Is there any way to do this?
View Replies
View Related
Apr 6, 2014
I'm not sure why but my infix string isn't removing the spaces. Here is the part of the code:
Scanner s = new Scanner(System.in);
System.out.println("Enter Infix express: ");
String infix = s.nextLine();
infix.replaceAll("\s", "").trim();
System.out.println(infix);
InfixtoPostfix convert = new InfixtoPostfix(infix);
String postfix = convert.toPostFix();
System.out.println("Converted Express: " + postfix);
Is there something I'm doing wrong? Here is the output when I run it:
Enter Infix expression:
(7 + x) * (8 – 2) / 4 + (x + 2)
(7 + x) * (8 – 2) / 4 + (x + 2)
Converted Expression: 7 x+ 8 – 2* /4 x 2++
View Replies
View Related
Oct 5, 2014
I am working on a small brain teaser project where I am taking a string input from a Scanner, and turning into ascii. The problem comes into play when the string has a space in it, so if the question is what's your name? and you say Michael Jackson, Michael gets converted then Jackson becomes the answer to the next question, rather then the second portion of the current string.
This is an older version of what I'm doing currently, but it had the same basic problem with spaces.I will say I did my current version entirely different.
nner user_input = new Scanner (System.in);
//Creates a string
String favoriteFlick;
System.out.println("Enter the title of your favorite film?");
favoriteFlick = user_input.next();
[Code] .....
View Replies
View Related
Apr 5, 2014
I am trying to write a program and the name variable can only store letters,dotes and spaces. But whenever I enter a space, the program doesn't work. Following is my code.
import java.util.Scanner;
import java.util.*;
public class Space {
public static void main(String []args) {
Scanner reader = new Scanner(System.in);
[Code] ....
View Replies
View Related
Sep 28, 2014
I have this error that keeps coming up any time I select one of my buttons. It actually doesn't hinder the performance of the project, everything works. But I am concerned I missed something and errors are never a good sign.On a slightly different note, I would like to figure out what the best way to format my output would be? I would like it to display as "100.00 F". I have a couple ideas on how to get the F symbol (or other symbol) by inserting something like
String degreesymbol = "F" or whichever it is and then returning that in the output string later. I can't get the decimal formatted correctly and I don't know how to print the degree symbol.Here is the code
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
[code]....
View Replies
View Related
Dec 30, 2014
I am currently working on a project that can save text from a JTextArea and also open that same saved text. The problem I am having is that when I open the saved text file it is no longer formatted.
How the file looks before saving/closing:
public class HelloWorld{
System.out.println("Hello World!");
}
How the file looks after being saved/closed then opened:
public class HelloWorld{ System.out.println("Hello World!");}
When I save the file I'm actually saving the entire thing to a single string. When I do this the String eliminates all tabbed spacing and pushes all characters to one single line.
The only ideas I had for fixing this were to either somehow use Format in the String class or record every time user tabs and add to the String.
View Replies
View Related
Feb 15, 2015
I am suppose to design a problem the prompts the user to enter a String. Based on the input from the user, have the program prompt with a full description of the playing card entered. Below is a table of the notation and meaning:
Notation Meaning
A Ace
2...10 Card Values
J Jack
Q Queen
K King
D Diamonds
H Hearts
S Spades
C Clubs
Use the .charAt(...) to extract each character from the user's input and build a switch statement to determine the full description.
import java.util.Scanner;
public class PlayingCardIdentifier {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a card notation: ");
String s = input.nextLine();
[code]...
View Replies
View Related
Mar 23, 2015
I'm just trying to append new employee information to a previously created file. When I type yes to add new employee and enter a string, the data does not appear in the file.
Java Code:
public class Records {
public static void main(String [] args) throws IOException {
Scanner input = new Scanner(System.in);
FileWriter fw = new FileWriter("dbs3.java", true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
[code]....
View Replies
View Related
Feb 7, 2014
I need to allow users only enter integer into a String variable, "input" and I am not sure what statement to use.
import java.util.Scanner;
public class assq2b {
public static void main(String []args) {
Scanner reader = new Scanner(System.in);
String input,b;
[Code] ......
View Replies
View Related
Mar 3, 2015
So this I need to write a program which asks the user to enter any string, but the string has to have integers inside. I need to calculate the sum of those integers mentioned in the String. Also return 0 if there aren't any.
User input: I have a 91
Output: 10
I cant seem to find a way scan through the string to find the integers and add them.
String x1=input.next();
char digit= x1.charAt(3);}
public static int findDigitSum (String x)?
View Replies
View Related
Feb 28, 2015
I have to create a constructor with eight parameters containing both string and integers.the variables were supposed to be entered by user. but when I try to create an object of the class the IDE post error messages about the constructor.
public class hfiledriver {
//the class name is hfile
//after the main method I try creating an object of the class
//after prompting the user to enter the data
hfile hfile= new hfile(firstname, lastname, gender, age, weight, height);
}
View Replies
View Related
Apr 10, 2015
I am pretty new to Java and am just learning about two dimensional arrays. I think that I understand the concept, but I seem to be having trouble adding stuff to my array. I wanted to make an array to hold both strings and integers, but wasn't sure if I could put integers in a string array. So I thought that I would be able to convert my integers to string and then add them. This however causes an error. This is my code(yes its probably not the best):
static String [][] students = new String [14][4];
static int number = 0;
String fName, lName, fullName;
int test1, test2, test3, test4;
String a, b, c, d;
[Code] .....
View Replies
View Related
Nov 9, 2014
I don't want to use an array. I do not know of a function built into Java to allow me to do this.
View Replies
View Related
Sep 13, 2014
How to improve my code. I finally was able to create a program that gives you the corresponding letter grade, when you enter in a numeric grade without using an array. The only issue left is that I have to be able to enter 5 grades at a time, and it give me the letter grade for all 5. I have the programming working, but only am able to enter 1 at a time. I am not sure what kind of loop or if I am supposed to use a loop.
public static void main(String[] args){
{
int grade = 0;
Scanner input = new Scanner(System.in);
System.out.println("Enter : ");
grade = input.nextInt();
if (grade >= 90)
[Code] .....
View Replies
View Related
Feb 10, 2015
I am working on my homework, and everything is fine. But I was wondering if I can fix one thing. basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{
static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
System.out.print("Enter the first name of the file: ");
String input = kb.readLine();
int[] firstArray = readFile(input);
firstArray = bubbleSort(firstArray);
View Replies
View Related
Feb 10, 2015
Basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{
static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws Exception {
System.out.print("Enter the first name of the file: ");
String input = kb.readLine();
[Code] ....
As you you may figure, it works if I put numbers separated by each lines. I was wondering if I can change it so that it works when I put numbers separated by space rather then each lines.
View Replies
View Related