Finding String Length Without Variables

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


ADVERTISEMENT

Split String Based On String Length?

Jan 23, 2010

I am trying to split a string based on length(example length 5) of the string. But I am having a issues with this substring(start, end) method. I get all substring which are of length 5. But if the last substring is less than 5 then I am not getting that last substring. But I need the last substring even if it is less than 5.

String s = "fjdjfdfjgffgjhfjghfjkhjhjh";
String spLine;
for(int i=0; i<s.length(); i=i+5){
spLine = s.substring(i, (5+i));
}
>

View Replies View Related

Count Length Of Each Word In A String

Aug 5, 2014

I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:

hello world how are you
There are 0 words of length 0
There are 0 words of length 1
There are 0 words of length 2
There are 3 words of length 3
There are 0 words of length 4
There are 2 words of length 5

This is my code so far it sort of does the job but not the way i want it too

import java.util.Scanner;
import java.util.StringTokenizer;
public class Brown_Matthew_13117002{
public static int count(String s, int len){
int result=0;
StringTokenizer st=new StringTokenizer(s,"[ ,;]");

[Code] ....

The output would end up being :

hello
There are 0 words of length 0
world
There are 0 words of length 1
how
There are 0 words of length 2
are
There are 3 words of length 3
you
There are 0 words of length 4

View Replies View Related

Count The Length Of Each Word In A String

Jul 31, 2014

I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:

hello world how are you
There are 0 words of length 0
There are 0 words of length 1
There are 0 words of length 2
There are 3 words of length 3
There are 0 words of length 4
There are 2 words of length 5

ithis is my code so far it sort of does the job but not the way i want it too

import java.util.Scanner;
import java.util.StringTokenizer;
public class Brown_Matthew_13117002{

[Code].....

View Replies View Related

Counting Length Of Each Word In String

Aug 5, 2014

I am looking for a way to create a method with the initial state in while loop, which will count the length of each word in a string using I want the output to be something along the lines of:

hello world how are you
There are 0 words of length 0
There are 0 words of length 1
There are 0 words of length 2
There are 3 words of length 3
There are 0 words of length 4
There are 2 words of length 5

This is my code so far it sort of does the job but not the way i want it too

import java.util.Scanner;
import java.util.StringTokenizer;
public class Brown_Matthew_13117002{
public static int count(String s, int len){
int result=0;
StringTokenizer st=new StringTokenizer(s,"[ ,;]");

[Code] ....

The output would end up being

hello
There are 0 words of lenghth 0
world
There are 0 words of lenghth 1
how
There are 0 words of lenghth 2
are
There are 3 words of lenghth 3
you
There are 0 words of lenghth 4

I think I need to use string.split instead of stringtokenizer and the while loop is incorrect it needs to loop equal to the number of letters in the longest word. But how to do either of these ?

View Replies View Related

Java Class - Limiting String Length

Jul 16, 2014

I defined a java class thus:

class Info{
public String name;
public String version;
public String arch;
double CPUSpeed;
double ranUtil, CPUUtil;
};

But each object of such a class takes many bytes. How can I limit it to one fourth of a Kilo-Byte?

View Replies View Related

Find Any String Length Without Using Library Method?

Apr 10, 2014

How can find any string length without using any library method??

View Replies View Related

String Class Private Final Character Length

Feb 27, 2015

The String class stores the characters of the string internally as a private char[] and calling someString.length() results in getting the length field from the character array. I am looking to get the details on how the length is implemented. I understand it is a field, but in the original question I provide sample code and really want to know if/how the resulting byte code may differ when compiled, perhaps I am just not seeing the simple answer through my confusion.

Link ....

View Replies View Related

Finding A String In Array

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

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

Finding A String Word In Char Array

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

Finding Divisible Numbers And Replace With String

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

Finding Most Common Character In A String Using For Loop

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

String Value To Be Name Of Variables Created

Mar 11, 2015

Is it possible to have the value of an entered String to be set as the name of the String to be created whilst running.For example: String username= Keyboard.readString() & the user entered for example the word: "Hello".Is there a way how I can make the Java Program create another String named Hello (Inputted value of String username).

If this is allowed, what do I have to use and if possible show me exactly what I have to do with the example mentioned above?

View Replies View Related

Using If Else Statement For String Variables?

Oct 17, 2014

Is it possible to use a if-else statement for string variables?

I am trying to make a program that execute different depending on the user typing in "male" or not when he/she gets asked about gender. The program execute the else part no matter what the user types in.

Here is my code :

import java.util.Scanner;
public class c4Testname {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
System.out.println("Type in your gender");

[Code] .....

View Replies View Related

Randomize Placement Of Variables In Outputted String

Jul 12, 2014

public class PasswordGen {
 public static void main (String[] args) {
 Generator();
 }
public static void Generator() {
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
 
[Code] ....

I'm trying to make a random password generator, but it isn't as random as I'd like it to be. I have set places where either a number, letter or symbol can go. I'd like to randomise the placement of these set types in the output from "symbol + letter + number + letter etc" to "? + ? + ?". Sample output from this code:

@W1L5m8%j4
^Y2L8z4@d1
£D6X5i3^i8

View Replies View Related

Two Dimensional String Array - Accessing Variables

Feb 5, 2015

Suppose I have private static void name() { ... } that has a two dimensional string array named array[. Now suppose I have private static void different() {...} and I want to write a condition where if (item == array) { ... }, how can I access my array from name() when I am in different()? I get a compile error saying cannot find symbol. My code is similar to:

Java Code:

public static void main(String[] args) {
...
String item = keyboard.nextLine();
... }
private static void name() {
...
String[][] array = new String[1][5];

[Code] .....

View Replies View Related

Sorting Array Of Objects Based On One Of Class String Variables

Apr 8, 2014

I have a school assignment that involves me sorting an array of objects based on one of the class String variables. I am using as the title says a simple selection sort method. The problem I'm having is that when I run the program in debug mode, it never seems to enter the if statement in the inner loop. I would like to say I've tried a number of things to figure it out, but honestly I'm just stumped as to why it's not working.

Here is the code:

public static void sortTransactions(Transaction[] oTransaction){// This is the sorting method, obviously it's not done so it currently just prints to screen.
System.out.println("Successful call to sortTransaction()");
String min = "";
int curInd = 0;
Transaction[] temp = new Transaction[1];

[Code] ....

The output when I check to see if the array is sorted verifies that the array never does get sorted.

View Replies View Related

Why Does String Split Method Producing Arrays With Whitespace Variables

Jan 17, 2015

I used the string split method in the following lines of code like this:

String[] numbers = out.split("[^d.]", 0);
String[] operations = out.split("[.d()]", 0);

When out is equal to the String "2x2.5", the array operations ends up looking like this when it is printed using the toString method:

[, , , x]

As you can see, before the array element x, there are three String variables which only contain whitespace. Why does this occur, and how can I prevent this from happening?

View Replies View Related

Splitting Date String By Date And Time And Assigning It To 2 Variables?

Jul 17, 2014

I have an requirement of splitting a Date-Time String i.e. 2013/07/26 07:05:36 As you observe the above string has Date and Time with space in between them.

Now I want just want split the string not by delimiter but by length i.e. after 10th place and then assign it to 2 variable i.e. Date <----2013/07/26 and Time <---07:05:36 separately.

View Replies View Related

EJB / EE :: Is There A Limit For Length Of Get URL Request?

Oct 17, 2014

I want to know is there a limit for length of get url request? if if what is the max length.

View Replies View Related

Generate Random List Length?

Apr 16, 2015

I have my code which will generate a random string using an ArrayList. However, I aso want to have it generate them in random lengths, not just using all the chars and numbers.

public static void main(String[] args) {
String[] CHARS = {"A","B","C","D","E","F","G",
"H","I","J","K","L","M","N",
"O","P","Q","R","S","T","U",
"V","W","X","Y","Z","1","2",
"3","4","5","6","7","8","9","0"};
List<String> pass = Arrays.asList(CHARS);
Collections.shuffle(pass);
for (String chars : pass) {
System.out.print(chars);
}
}

View Replies View Related

What Is The Length Of A Null Array Object

Feb 21, 2014

I tried the following and it gives me null pointer exception. I just want to verify if what is the actual value of a length of an array?

Object [] obj = null;
System.out.println("length:" + obj.length);

View Replies View Related

Int Declarations - Assigning Length And Width

Mar 19, 2015

What is happening in below mentioned java code. Why in method insert, int l, int w is declaring & it is assigning to length & width.

class Rectangle{
int length;
int width;
void insert(int l,int w){
length=l;
width=w;

[Code] ....

View Replies View Related

How To Test For Array Length Equal To Zero

Mar 15, 2015

I am working on an assignment covering exception handling and am stuck on part of the assignment. How can you test for array length = 0?

When I try something like: if (array.length == 0) on a null array, naturally I get a NullPointerException. I would try something like if (array != null) but both array length of 0 and null array are supposed to throw different expressions.

View Replies View Related

Stuck On Average Word Length

Aug 1, 2014

The goal of this code was to create a program using main method java to analysis a piece text which has been entered from a user.

They do this by entering the text into a scanner which is then analysed by the program. The analysis is to produce word frequency, for example " This is a test" produces this results:

This is a test

1 letter words: 1
2 letter words: 1
3 letter words: 0
4 letter words: 2
5 letter words: 0

The bit that I'm stuck on is producing a mean/average, My mind telling to divide

counts.length
by
str.length

But I'm not the Best at java and I've tried to implement this but all I get are errors.

Code:

import java.util.Scanner;
public class While_Loop {
public static void main (String[] args) {
while(true){

[Code] .....

View Replies View Related







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