Finding Space In String
Jan 19, 2015
I have an assessment for college, the part of my code that im struggling with is the part where the user enter their first name followed by a space and then their second name. I m using ---------------fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " ); ------------- to capture this. the issue is if the user only enter 1 name I need to output an error. the issue is I cant find a way to tell if the user entered a second name. This is what I have so far:
public void makename() {
// makes an inputbox to ask their name
fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " );
//separates the first and second name into 2 strings in order to make the username
[code]....
The problem is it accepts anything I type in without causing an error. What should I type in to stop this? ive tried anything but I cant find a way to tell if a surname has been entered or not.
View Replies
ADVERTISEMENT
Mar 30, 2014
I want to cut my string from space char but i am getting exception....
Java Code:
import java.util.Scanner;
import java.util.StringTokenizer;
public class NameSurname {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
String s0,s1=null,s2 = null,s3=null;
s0=sc.next();
[Code] ....
Console:
Lionel andres messi
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at java.util.StringTokenizer.nextToken(Unknown Source)
at com.parikshak.NameSurname.main(NameSurname.java:15) mh_sh_highlight_all('java');
I/p -O/p:
my s0=Lionel andres Messi
And I want to break it as soon as i find space and save it in s1,s2 and s3
s1=Lionel
s2=andres
s3=messi
View Replies
View Related
Jul 23, 2014
I started using Java a couple of days ago, If you haven't guessed I want to see if the user is typing a full name or not, but I'm actually not too concerned with any more complexity than I mentioned in the title. It's ok if an input like "GLba b" comes out positive.
View Replies
View Related
Oct 4, 2014
My current problem is when im trying to choose case 2 and call markItemOnLoan i cant seem to find the title i want after i enter a few. I will include all of my code but i have mentioned what I am currently stuck on :
import java.util.Scanner;
public class Library {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MediaItem mCall = new MediaItem();
Library call = new Library();// makes instance
[code]....
View Replies
View Related
Oct 14, 2014
The problem asks me to write an expression whose value is the number of characters in a specific string. Normally, it would be a simple task of using the String.length() method, but that is not allowed. In fact, no variable declaration is allowed (it's a MyProgrammingLab assignment, if any are familiar).
Simply put, if I have a String "This is a sample string." is it possible to find the length without assigning it to anything?
In other words, the code must be able to go into the parentheses of System.out.println( ); and correctly print the length of said string.
The answer, for those curious:
System.out.println("This is a sample string.".length());
I didn't know the .length() method could be called on a literal. Now if only I could find out how to close this thread...
View Replies
View Related
Feb 13, 2015
I'm trying to find a word in an array of char.....but I'm stuck. How to formulate the code to step through the array and pick out the word. This is what I have so far...
public static void searchAcross(String string, char[][] puzzle) {
// Gets the number of rows in the matrix
int rowLength = puzzle.length;
//Gets the number of columns in the matrix.
int colLength = puzzle[0].length;
[Code] ....
View Replies
View Related
Jul 13, 2014
My interest in Java leads me to try and print numbers from 1-100. The output should show all numbers from 1-100 and each number that is divisible by by 13 should be replaced with a string "Fuzzy".
public class Fuzzy {
public static void main (String[]args){
for(int i = 0; i < 100; i++)
if(i % 13==0)
System.out.print(i + "fuzzy");
}
}
View Replies
View Related
Feb 7, 2007
import javax.swing.*;
public class mostFrequent{
public static void main(String args[]){
char index;
String s;
s = JOptionPane.showInputDialog("Enter String here");
int currFrequency = 0;
for(index = 0; index<s.length(); index = index++){
int i = 0;
for(i = 'A'; i<='Z'; i++){
if(i==s.charAt(index)){
currFrequency = currFrequency + 1;
}
}
}
System.out.println("end");
}
}
//my code so far
View Replies
View Related
Feb 12, 2014
In c++, I'm aware that you can use the ignore function to ignore numbers after space, but in string how do I ignore a number after space is found for instance, "109 33" how would I ignore 33?
View Replies
View Related
Apr 11, 2014
The title says it all and i'm a fairly new java programmer but here is the code:
package TestVersion;
import java.awt.Color;
import java.awt.Graphics;
[Code]......
I think i need a variable or something oh and here is the 2 other classes, the keylistener one:
package TestVersion;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
[Code].....
View Replies
View Related
Apr 2, 2014
Here is the question I'm having trouble with:
Write a program that reads a list of characters:
eg.ubccdddwfreshawbgtiijhktrocbfgrtwghdddguppgrkitt. etc.
inputted from the keyboard one character at a time starting with a vowel (The vowels are a, e, i, o, and u) until a blank {spacebar} character is issued. You do not press enter after each character. Any other sequence should return an error message and a way of entering it correctly.
Having read in the list (or during the reading of the list) the program is to find the longest continuous occurrence of consonants, outputting the vowels between which this occurs and the length of this string of consonants.
{In the example above, the output would be 13ou}
Check for input errors and respond accordingly.
I have done most of it, the problem being that I don't know how to end the scan with a space so that I don't have to press enter. I have done the bit that finds the biggest amount of consonants in a row. (I'm writing in netbeans)
View Replies
View Related
Dec 8, 2014
I have been trying to space out output on a Java console window so that I have three columns with 6 rows of data from three different arrays. The code I have so far outputs the data with no problem however the spacing between the columns is uneven. My loop so far is made up as follows
for (int i = 0; i < printVotes.length; i++) {
System.out.println(printNames[i] + "
" + printVotes[i] + "
" + printPrecent[i] + "%");
}
As you can see I have been manually adding the space between each element of the array but this means that the space between each element is different because the size of each element is different if work out a loop that works out an even amount of space between the elements and then print this along with the elements ....
View Replies
View Related
Mar 27, 2014
I have eight panels that are one above the other and are the full width of the frame.
I would like to hide some of them some of the time and then "bring them back".
If I use panel1.setVisible(false) panel1 will disappear but then all the panels below it move up to take panel1's place.
I don't want that to happen. I want just a blank area where panel1 was.
Is there a way to do that?
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
Mar 7, 2015
Java Code:
public synchronized boolean isHit (FilledRect missile) {
for (int col = 0; col < aliens[0].length; col++) {
for (int row = 0; row < aliens.length; row++) {
if (aliens[row][col]!= null && aliens[row][col].overlaps (missile)) {
aliens[row][col] = null;
return true;
}
return false;
}
}
} mh_sh_highlight_all('java');
And I get an error saying that it cannot find symbol method overlaps. So is there something wrong that I am doing?
View Replies
View Related
Nov 2, 2014
I am writing a program where whatever number I input in my scanner, it will space them...
For example: If I input 456, the output should be 4 5 6... here is my code:
import java.util.Scanner;
public class midterm {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
String number = "";
[Code] ....
the current output for this program is:
456 456 456
View Replies
View Related
Nov 10, 2013
What is the ratio of Memory(RAM) to heap space ? ie: When JVM will throw OutOfMemoryError based on heap available to JVM ?
OutofMemoryError - Memory:RAM size
View Replies
View Related
Mar 17, 2014
I have the below code with spaces on uri and prefix (directive attribute).
<%@ taglib uri=" news.tld" prefix=" news" %>
how to fix without altering the JSP? Can i handle with web.xml or any other property available.
I have hundreds of jsp like this and wanted to handle without code fixes.
I tried JSP trimSpaces but it fixes the namespace but not the attributes.
View Replies
View Related
Feb 26, 2015
How do I insert space between my int and the statement?
My out put it is: A heptagon has7 sides .
what I want is: A heptagon has 7 sides .
public class Geomerty {
public static void main(String[] args) {
int sides = 7;
System.out.println( "A heptagon has" + sides + " sides ." );
}
}
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
Apr 13, 2014
I'm working on this space invaders game and I've been stuck trying to get the alien spaceships to move down in one group. The way we have to design the game is we have to implement at least these 3 classes. These classes are GameData,GamePanel, and Animator. GameData is all the action figures/models in the game. GamePanel draws all game action figures/ models on the screen. Animator updates periodicaly action figures positions,sizes, colors etc. I can get the aliens to move right but once they hit the end of the canvas I cannot figure out how to get them to move as a group down and then begin moving left. I tried to add alien objects to the GameFigures list and then have the update method in the Alien class be responsible for moving the alien ships, but I couldn't figure out how to make it work. So then I made an alienFigures list in the GameData class and I still cannot figure out what to do. Here is the GameData class
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
[code]....
View Replies
View Related
Jun 20, 2014
I am setting text to textarea from textfield. It works perfectly. My challenge, however, is setting texts with spaces in-Between them. For example, what I observe is:Amount23 instead of: Amount 23. Is it possible to automatically create word space while setting texts?
View Replies
View Related
Jul 1, 2014
I'm on windows 8 and I want to use notepad or notepad++ to simulate the press of the space bar every 15 minutes so my computer doesn't auto lock.
View Replies
View Related
Jan 4, 2014
1: 3D space. How do i define the space where i will make my world? Is there a certain point that i will define as 0,0,0 or is there some other way.
2: Camera positioning. When i have the 3D space, how to i make it so that the camera will show that what i want it to show. Lets say il define its lokatios 30,50,100 and direction is NW, then how to i make it so that is see the SW direction from the point 30,50,100?
View Replies
View Related
Jul 31, 2014
I am attempting to create a 2d game that has a realistic space simulation. The map is supposed to be set so that you are looking from the top down. The problem I am having exists in my interaction between the sun and a planet. Simply put, the planet will only move in a single diagonal direction without ever changing direction. The formulas I am using are F=(G*M1*M2)/D^2 and A=F/M. The code below is simplified.
(inside planet Class)
double x;
double y;
double mass;
double xForce;
double yForce;
double xAccel;
double yAccel;
[Code] ....
Things I have already tried:
1. Setting G to negative
2. changing the order of the subtraction in determining D
3. some weird pythagorean theorem thing someone suggested
View Replies
View Related
Sep 1, 2014
I'm new to programming and I would just like to know what happens inside the memory once you run a java application. How do the memory allocate space for your objects or variables? Where does it start?does it start in memory location 1 first when I create my first variable or object? What is the use of variables and how is it used by the memory?Also if I create a variable named String s1 then I created another variable again named s1= "myString" Did I create a new object or I just edited the value of the s1 variable??
And if I create a variable int num1 = 10 then I created another variable int num1 = 12 did I create another one or I just edited the num1 variable?? Also how much memory would an object have? Does it depend on how many primitives objects like arrays or strings it have?Also I want to develop 3D games also, in the future is Java going to be great as I know machines in the future will have massive amounts of RAM and the JVM will be better...
View Replies
View Related