Dealing With Spaces During Input String Conversion?

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


ADVERTISEMENT

How To Delimit Spaces In User Input Separating Two Data Types

Jan 17, 2015

I am creating a calculator in which a user will directly input the numbers and the operator...

Here is my code:

int answer = 0;
int num1, num2;
char operator;
System.out.print("Enter calculation: ");
num1 = kb.nextInt();
operator = kb.next().charAt(0);
num2 = kb.nextInt();

The code above will accept the user input when there are spaces like this: 1 + 1

So the program will give an answer which is 2...BUT

if i input 1+1 it will give an error...Exception in thread "main" java.util.InputMismatchException

How can i do that it will separate integer to character? because i set the operator as character....

So that it will accept one digit to several digit numbers... like 500+84 or 1520+872??

View Replies View Related

Splitting A String While Keeping Some Spaces

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

String From User Not Removing Spaces

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

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 View Related

Write A String Name Variable To Store Letters / Dotes And Spaces Only In Java?

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

Swing/AWT/SWT :: Movement And Conversion Of GetText Input In OrderCalc Method

Mar 7, 2014

My OrderCalculator class is throwing an exception. The input from my HourPanel that is supposed to add to my OrderCalc is cooperating.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
public class OrderCalculator extends JFrame
{
private JobPanel jobs;
private HourPanel rate;

[Code] .....

View Replies View Related

Double To Hex String Conversion

May 8, 2014

I am trying to convert the double 4270571936.0000000000d to a hex string using Double.toHexString() and the answer I'm getting is 0x1.fd17834p31, what does p stands for?

The answer I'm expecting to get is 0x41efd17834000000 so not sure why it won't give me the correct answer?

The following floating point Double to hex calculator shows the write answer right Floating Point to Hex Converter

View Replies View Related

Conversion From String To Integer

Mar 4, 2014

I want to conver String value into Integer, and I have this :

String timeInterval = tfInputTinter.getText();

Integer tint=(Integer.parseInt(timeInterval))*1000;

but when I put this second line, the conversion, the program stops to work. I tried also with Integer.valueOf(timeInterval) but again I had the same problem.

View Replies View Related

Byte Stream To String Conversion?

Apr 1, 2014

I am getting byte stream as below. These looks like UTF 8 bytes

3C524F4F543E3C535452494E473E54455354204F4E4C5920535452494E473C2F535452494E473E3C2F524F4F543E

I want java code which will convert above bytes to string as shown below

<ROOT><STRING>TEST ONLY STRING</STRING></ROOT>

View Replies View Related

String To Character Array Conversion

Jul 17, 2014

How I can convert a string into a character array without using toCharArray???

View Replies View Related

String To Numerical Data Conversion

Feb 3, 2015

A group of my classmates and I were discussing strings. We were asked, "What circumstances would you want to convert a text string to numerical data?" but we couldn't think of any answers outside of counting characters within the string for various applications.

View Replies View Related

String To Blob Or Clob Conversion?

May 11, 2015

how to convert string to blob or clob data type in java coding?

View Replies View Related

Temperature Conversion Project - Send Input Value To Calculation Area And Get Value To Display In JLabel

Sep 30, 2014

The problem I'm trying to resolve now is getting my ActionEvent to send the value input in the JTextArea out to be calculated, then to display the result in my result JLabel.

Here is an image of the GUI :

The basic procedure the teacher is asking for is the user is supposed to enter a temperature in the input. The user then clicks the input scale, and then on the output scale selection the converted value is to be displayed in the output area.

As I mentioned my problem is in the sending of the input value to my calculation area, and getting the value to display in the JLabel.

How should I approach this solution? Do I need to have a listener after the JTextArea of the input box? Will that allow me to limit the input values to numbers only?

final JTextArea inputText = new JTextArea("" + (char)176,1,4);
//ReadConsole equivalent to specific input was a number?

Here is the section of code my ActionListener is:

//celOut represents the Celcius output scale JRadioButton.
celOut.addActionListener (new ActionListener () {

@Override
public void actionPerformed(ActionEvent e) {
//if the Celcius Input Scale is selected.
if(cel.isSelected())

[Code] ....

All the calculations are required to occur in a separate java file, which what I think is tripping me up. How do I send a value from the JTextArea into the Calc.celToFahr method?

//contents of my Calc.java calculation class.
public class Calc {
public static double celToFahr(double cel){
return cel * (9./5.) + 32.;
}
public static double fahrToCel(double fahr){
return (fahr - 32.)*(5./9.);
}
}

View Replies View Related

PopupMenu And JEditorPane String To Image Conversion

Jan 15, 2015

I am working on a simple online chat program. I have build the server services and generally everything works smoothly.Now for the input area i use a JEditorPane as like the output message area. The reason of choosing JEditorPane is because i want to apply some CSS on future...

Now under the input message area there are two JButtons. One for attachments and other one for emoticons. Now i am trying to make a Popup Menu with all my Emoticons Images when someone click the JButton(emoticons). I was thinking about a JList inside a popupMenu but that kind of thing is mostly impossible i guess. Any way to automaticaly convert String to images for example when someone type in inputJEditor something like this :) or this :( to convert it to emoticon image ...

View Replies View Related

String Format - Conversion To Date Object

Mar 21, 2014

I have a date in the following String format "2013-03-28,19:37:52.00+00:00"  and post processing I am converting this to following String as per prevailing logic "2013-03-28,19:37:52.00+0000"  (This is existing code and no changes have been Made here for last few years) And the using this SDFormat i.e  new SimpleDateFormat("yyyy-MM-dd,HH:mm:ss.Sz")   for conversion to Date Object
 
We are suddenly getting this exception now can't figured out what has changed ?
 
java.text.ParseException: Unparseable date: "2013-03-28,19:37:52.00+0000"
at java.text.DateFormat.parse(DateFormat.java:357)

View Replies View Related

Jsoup And Dealing With File Extensions

Oct 4, 2014

I am working with jsoup right now and I am trying to get a range of file extensions such as doc, docx, txt, pdf, and so on. Anyways i have looked through the jsoup api and cant seem to find what I am looking for. The closest thing I have found is

Elements files = doc.select("a[href$=.doc]");

This should work but only grabs one extension. How to grab multiple at one time? My guess would be:

Elements files = doc.select("a[href$=.doc]").select("a[href$=.docx]").select("a[href$=.txt]");

However I'm not sure for certain. Anyways I will provide a link for the api .....

View Replies View Related

Dealing With Presorted Object Array

Apr 11, 2014

I am trying to solve involves an object array each object has two instance variables, a string(origin) and a double(price), that was originally unsorted I have already sorted the array. There are a total of 11 possible origins and 6 prices, so obviously some origins share prices with others. The array is sorted by origin alphabetically ignoring case. I now have to write to a file how many instances of each origin there is, tally up all the prices for each origin and a total for all prices. I get bonus points for utilizing the presorted array to my advantage for the final output and calculations.

It would be easy to write a branching statement and tally up all the origins and prices etc... However this is a linear operation and doesn't seem to benefit from the presorted array at all so I came up with what I think is a descent alternate solution.

I was thinking that I could go through the array one object at a time and stop when I came to an origin of the next type. For example: there are 12 names called "Adam" and then a name called "Bob". So if I stop at bob, check the index and add 1 then I know how many Adams there were. I can then continue this process with all the other names, all the while calculating the combined prices and all.

View Replies View Related

Spanning Tree - Dealing With Static Variables

Feb 20, 2014

I'm new to Java and I'm trying to create a spanning tree in the desired order with 4 nodes (0,1,2,3). I've got the following code:

import java.util.ArrayList;
public class A {
public static boolean any(ArrayList<Integer> input) //To check for if any element in the ArrayList == 1
{
boolean answer = false;
for(int i=0;i<input.size();i++) {
if(input.get(i)==1)

[Code] ....

What happens is that the input parameter adj and hence the original adjmat inside main gets changed everytime I enter the method "connected", and I don't want this to happen. I understand that this is due to the main method being static and hence adjmat becomes static as well, but how do I change the code such that adjmat only gets modified after coming out of the connected function, and not while inside it?

View Replies View Related

Java Dealing 5 Random Cards From A Deck

Aug 3, 2014

I redid my entire code to use array-lists instead of just arrays my professor finally got back to me and said he doesn't want us to use lists. The assignment is to create a CardGame driver class, then create a "card" in a card class, then a "deck of cards" in a DeckOfCards class, shuffle, and deal 5 random cards and "deal a card and report the number of cards left in the deck". That last line in quotes is what I do not know how to do.

Also, I renamed a lot of variables via some suggestions and the assignments states "Make sure to write the appropriate constructors, getters, setters, toString and other methods as required for both classes." I think they're appropriate. Should I change some of he methods to better getters and setters identifiers?

Here is my code.

import java.util.Random;
public class CardsGameTest {
//execute application.
public static void main(String[] args) {
DeckOfCards myDeck = new DeckOfCards();
myDeck.shuffle(); //shuffle cards.

[code].....

The instructions also say to "print each card as it is dealt" does that mean 5 cards one at a time? Anyways, I was thinking that in the driver class I could add a for loop to the for loop and as it deals a card it could run through the second for loop and print how many cards are left in the deck.

View Replies View Related

Setters And Getters In Java Program - Dealing With Circles

Oct 29, 2014

You should write a class that represents a circle object and includes the following:

1. Private class variables that store the radius and centre coordinates of the object.
2. Constructors to create circle objects with nothing supplied, with just a radius value supplied and with a radius and centre coordinates supplied.
3. Public instance methods that allow the radius and centre coordinates to be set and retrieved (often known as set/get methods).
4. Public instance methods that return the circumference and area of the circle.
5. A public class method that tests if two circle objects overlap or not

Here is my code:

import java.lang.Math;
public class Circle {
private double xCentre, yCentre, Radius;
// constructors
public Circle() {
xCentre = 0.0;
yCentre = 0.0;
Radius = 1.0;

[Code] ....

The second program just has to demonstrate each aspect addressed in the brief I pasted at the top.s of what else I can do.

This is as far as I got during the 3 hour lab session I had and both compiled fine but when running just displayed all the text eg. "Circumference of first circle is ", but didn't display any numeric values. I don't have the facilities to actually run the program unless I'm in the computer lab, I have a short opportunity to go in tomorrow but that will be the last so I'm doubtful that I'll get it fully working in time.

The problem is that when this code runs it doesn't display any numerical values, ie nothing is being passed between the two programs.

View Replies View Related

How To Add Spaces To A Pattern

Apr 21, 2015

I have created this program that outputs

1
12
123
1234
12345
123456

I tried looking up some ways to create spaces in between each number, but I failed miserably. Here is what I have right now.

public class Iterations {
public static void main(String[] args){
for(int i=1;i<=6;i++)
{
for(int j=1;j<=i;j++)
System.out.print(j);

[Code] ....

View Replies View Related

How To Read A Name Which Has Spaces In Java

May 2, 2014

I do not know how to read a name which has spaces in Java and I wish to learn. The following is my code

import java.util.*;
public class Asking {
public static void main(String []args) {
Scanner reader = new Scanner(System.in);
String [] names = new String[20];
int [] marks = new int[20];

[code]....

View Replies View Related

Digit Separation By Three Spaces Each

Oct 14, 2014

I am having a problem of how to write a program dat will input one number consisting of five digit,separate the number and print the digits separated from one another by three spaces each.. I tried to use divisional and remainder operation but it didn't work.

View Replies View Related

Trim Spaces Between Words

Feb 4, 2014

How can i make sure that when user enter value there is no space between words example :

NEW  YORK MUST BE NEW YORK

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







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