String Characters Display First Three Multiple Times

Sep 26, 2014

I am trying below challenge to display first three characters three times if the size of the string is greater than 3.

Say if i send hello i should get helhelhel

if i send xyz i should get xyzxyzxyz

I wrote as below

public String front3(String str) {
if(str.length()==3){
return str+str+str;
}
if(str.length()>3){
String str2=new String(new char[]{str.charAt(0),str.charAt(1),str.charAt(2)});
return str2;
}
}

I am getting error as below

Error:public String front3(String str) {
^^^^^^^^^^^^^^^^^^
This method must return a result of type String

Possible problem: the if-statement structure may theoretically allow a run to reach the end of the method without calling return. Consider adding a last line in the method return some_value; so a value is always returned.

View Replies


ADVERTISEMENT

Count And Display Number Of Times Specified Character Appears In String

Mar 7, 2014

Write a program using a while-loop (and a for-loop) that asks the user to enter a string, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the string. (So, you will have two separate program codes, one using a while-loop and the other one using a for-loop.)

Example:
Enter a string: "Hello, JAVA is my favorite programming language."
Enter a character: e
The number of times the specified character appears in the string: 3

I don't even know where to begin I've only got this

import java.util.Scanner;
 public class letterCounter {
 public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string");
String myString = sc.nextLine();
System.out.println("Enter a letter");
String letter = sc.nextLine();
}
}

View Replies View Related

How To Make A Program Run Multiple Times

May 18, 2014

I have made a basic math game that asks you questions and tells you if you have answered them correctly or incorrectly. The game runs great! The thing is that it asks you a question one time and after you answer you have to run the program again. I want to get the program to ask ten questions. After that I want to figure out a scoring system for it but the first step is to get it to ask my ten questions. Here is my code.

package pkgnew;
import java.util.Scanner;
import java.util.Random;
public class New {
public static void main(String args[]) {

[Code] .....

View Replies View Related

If Condition Matches Multiple Times

Feb 16, 2015

I'm working on a problem that requires me to generator all possible word combinations based on a 7-digit number as input. Many of the generated "words" will be nonsense, but some with be "NEWCARS", "TAKEOUT", etc... This problem mimics the phone number a company would use to support clients remember that number.

I completed the exercise, but I would like to explore more elegant solutions. Specifically, I've used an IF-THEN-ELSE condition inside of a FOR loop. Here is my working code:

package com.johnny_v.exercises.telephone;
public class WordGenerator {
public static void main(String[] args) {
int numOfTimes = 2187;
String two = "ABC";
String three = "DEF";
String four = "GHI";

[code].....

I receive StringIndexOutOfBoundsException exceptions. I it's because multiple conditions are matched. For example, the indexSix is reset to 0 when row is a multiple of 9. Because row is also a multiple of 3, this condition also executes and then increments "indexSix".

View Replies View Related

Calling Single Class Multiple Times

Apr 14, 2014

how we can call one class in multiple programs to reduce code redundancy

View Replies View Related

Java Statement Getting Executed Multiple Times Outside For / While Loop

Aug 14, 2014

I am trying to execute the following method: This method takes a delivery date as one of the arguments and a list of calendar holidays as another argument. If the delivery date is present in the holiday list, I will add 1 day more to the delivery date and check again if the new delivery date is part of holiday list.

Issue is that the last 3 statements before 'return' are getting executed multiple times though there is no for or while loop. Why they are getting invoked multiple times.

@SuppressWarnings("rawtypes")
private String fetchNextWorkingDay(String sDeliveryDate, Element eleCalendarDayExceptions, SimpleDateFormat sdf, Format formatter) throws Exception {
System.out.println("");
System.out.println("Inside fetchNextWorkingDay method");
System.out.println("Del Date to compare is "+sDeliveryDate);
Boolean isDateSet = true;

[Code] .....

View Replies View Related

Unable To Set Text To A Label Multiple Times In Same Program

Aug 5, 2014

I am developing a java swing application. In which i want to set a different text to a label in same program ( in single JFrame). I dont want to use another JFrame instead i want to use setText method of java and set different text to a label at different intervals of time according to my need.

I am using java 1.7 sdk and pupy linux as OS. Below i am giving source code.

What i am doing is in constructor of class i am setting an image to JFrame and setting text "Welcom...". And when user clicks on this JFrame a method is called which will clear the text "Welcome.." and sets new text to another label "Enter...." and from there another method is called and it clears label "Enter..." and sets a text "Plzz wait..". and yet there are more methods, i havnt provided here.

But what is happening is it shows welcome text and then directly Plzz wait..

Its not showing text Enter... because control is finished after last method gets executed.

My concern is i want to show all the setText (my messages) in a sequence. Without using another JFrame. There is no any user input other than user will click on first welcome page.

public class BackgroundFrame extends javax.swing.JFrame {

JLabel setTitle1;
JLabel setTitle2;
JLabel setTitle3;
JLabel setTitle4;

[Code] ....

View Replies View Related

JSF :: Data Table Displaying Same Item Multiple Times

Nov 25, 2014

I currently have a datatable on the bookingList.xhtml which is supposed to list all bookings made, if first booking petName is spot it displays the booking details perfect but if another booking is made (for example one with petName fluffy) then it displays 2 lines with fluffys details and spots details are not displayed, it carries on that if a third booking is made then the 3rd bookings details are displayed 3 times and neither spot or fluffys booking details are displayed,

bookingList.xhtml code
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">

[code]....

View Replies View Related

Swing/AWT/SWT :: Listener Called Multiple Times On Single Click?

Jan 30, 2015

I have the following listener on a tableset of names:

ListSelectionListener singleListener=new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
int row=e.getFirstIndex();
if (row==previousRow) {
row=-1;

[Code] .....

The println shows that getValueIsAdjusting is called 4 times when I click a single selection from the list. Is this normal behavior? If so how do I determine which call to really use. If not, where can I look to see why it is being called 4 times on a single click?

View Replies View Related

Calling Method Several Times And Trying To Write Multiple Records To A File?

Oct 27, 2014

So I am calling this method several times and trying to write multiple records to a file. Problem is that every time I call the method it overwrites the file from before and doesn't add it.

public void fileWriterMethod() throws IOException{
RandomAccessFile raf = new RandomAccessFile(filename, "rw");
raf.writeInt(id);
raf.writeInt(existingMileage);
raf.writeInt(gasCost);
raf.writeInt(ndays);
raf.writeInt(rate);
raf.writeInt(totalCharge);
raf.writeInt(discount);
raf.writeInt(tax);
raf.writeInt(netCharge);
raf.writeInt(returnMileage);
raf.writeBytes(carName + "
");
//Closing the stream
raf.close();
}

View Replies View Related

String Split Method To Tokenize String Of Characters Inputted?

Sep 27, 2014

I am currently trying to make a calculator in Java. I want to use the String split method to tokenize the string of characters inputted. I thought I was using the String split method wrongly, because I had surrounded the characters I wanted to delimit with square brackets. However, when I removed the square brackets, the code threw an exception when I pressed the equal button. The exception was a PatternSyntaxException exception. Am I using the String split method wrongly? And why is the exception thrown? Here is my code:

import javax.swing.*;//import the packages needed for gui
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.List;
import static java.lang.Math.*;
public class CalculatorCopy {
public static void main(String[] args) {

[Code] .....

View Replies View Related

Display Result Of Two Dice Thrown Five Times And Total Of Those Results

Sep 12, 2014

I need to create a simply application that would display the results of two dice thrown five times and the total of those results. This is shown below in the attached file.

The problem is, I have a do-while loop that loops 6 times. Inside the loop, I have 2 random.nextInt(5) that generate random numbers. But how can I output the total? How can I make a variable equal to the sum of the two random numbers if the two random numbers are located inside a do-while loop?

Attached below is also the code I have thus far.

(Attached below is both files: what it needs to look like, and what it currently looks like)

View Replies View Related

Roll Four Sided Die Between 100 And 1000 Times Depending On Input And Display Answer

Oct 29, 2014

I'm trying to get the program to roll a four sided die between 100 and 1000 times, depending on your input, and then displaying the answer.

public class Lab08
{
public static void main(String[] args)
{
int v1 =0, v2 = 0, v3 = 0, v4 = 0;
int n;
char response;
System.out.print("Enter number of rolls (100-1000): ");
 
[Code] ....

View Replies View Related

Count Number Of Characters Contained In Both Strings And Display Results

May 2, 2014

I have been working on this for hours and cannot get it to work properly. The program is to count the number of characters contained in both strings and display the results. It is also to display the final statement, but only once. This version has a complete statement for each matching character.

import java.util.Scanner;
public class CountMatches {
public static void main(String[] args) {
String firstStr = ("");
String s1 = firstStr;

[Code] ....

View Replies View Related

Scheduling Software - Display Chart Showing Lesson Times For Particular Teacher And Students

Aug 27, 2014

I want to develop a dynamic scheduling application that I hope to use at work, but I have some questions about the design approach since this is my first undertaking of this kind of project.

The purpose of the application is to display a chart showing the lesson times for a particular teacher and her students.

Here's are more important details:

-The chart displays the current day's schedule.
-The top row displays the teacher's name. There are at least 4 teachers each day.
-The leftmost column shows the times (from 3:00 to 8:00).
-The table is filled with the names of the students.

My current problem is deciding on the best approach to storing this data containing the teacher and her students and times. I should also note that there are about 500 students and around 20 teachers (i.e. the dataset is small). Is this a problem solved using a database and JDBC or could I just write the data to file? Are there other approaches that would solve this problem?

View Replies View Related

How To Print String Only Five Times

Feb 11, 2015

I can't figure out how to print this string only five times. I tried to use the * operator, but that doesn't work with strings apparently, unles i'm not importing correctly.
 
import java.lang.String;
public class Looparray
{
public static void main(String args[] {
for (String myStr= "Hello there!";;) {
System.out.print (myStr);
System.out.print("
");
}
}
}

View Replies View Related

Accept String And Number Of Repetitions As Parameters And Print String Given Number Of Times

Oct 3, 2014

I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...

This is the question:

Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:

printStrings("abc", 5);

will print the following output: abcabcabcabcabc

This is what I attempted:

public class printStringsproject {
public static void printStrings(int abc, int number) {
for (int i = 1; i <= number; i++) {
System.out.print("abc");
}
}
public static void main(String[] args) {
printStrings(1, 5);
}
}

View Replies View Related

Check How Many Times Char Is Used In The String

Apr 1, 2014

I tried to make a program that takes a string str, and char a and checks how many times the char is used in the string.

Example: the string Welcome and the letter e, is 2 times. so the program should print 2.

It compiles but when I run it and enter the information, i cannot get the printing line out.

Heres my code:

import java.util.Scanner;
class program
{
public static void main(String[] args) {
Scanner user_input=new Scanner(System.in);
String str;
String b;
System.out.print("Please enter a word");

[Code] .....

View Replies View Related

Check How Many Times Char Is Used In String

Apr 1, 2014

I tried to make a program that takes a string str, and char a and checks how many times the char is used in the string. Example: the string Welcome and the letter e, is 2 times. so the program should print 2. It compiles but when I run it and enter the information, i cannot get the printing line out.

Heres my code:

import java.util.Scanner;
class program
{
public static void main(String[] args) {
Scanner user_input=new Scanner(System.in);
String str;
String b;
System.out.print("Please enter a word");
str=user_input.next();

[Code] ....

View Replies View Related

Ignore Certain Characters In A String

Apr 5, 2014

I'm trying to loop through a string and depending on the character, add a JLabel to a game. The problem is the character 'L' represents a lantern but is also used in the reply the game gives which is "LOOKREPLY". I've tried to use some code to ignore the LOOKREPLY bit but it's not working. Here's what I've tried.

else if(message.charAt(i) == 'L' && message.charAt(i+1) != 'O' | message.charAt(i+1) == 'Y'){
JLabel localLabel = new JLabel();
localLabel.setIcon(lantern);
panel.add(localLabel);
panel.revalidate();
panel.repaint();

But the first image on all of the JLabels is always a lantern, which is what L represents. As it's only 1 lantern this leads me to believe that it's ignoring the first 'L' but for some reason it's not ignoring the 'L' at the end of LOOKREPLY.

View Replies View Related

How To Handle String With Characters

Sep 16, 2014

how we can handle the string with special characters. For instance:

123456789BNJKNBJNBJKJKBNJKBJKNBJK"VJKNBJNHBJNJKBVJ KBJKNB"VHBVBBVNBVNBVBVBVBNBVNBNBJHFBVFJB FNVJBNVJNVJDFNVJKNVJKNVJKVNNVJ NN"

I get some user inputs with double quotes and i need to split to 80 chars line.

.length fails to get the length if it contains special characters like double quotes and ?

View Replies View Related

How To Find Different Characters In String

Apr 8, 2014

like i have String s="11222ddeefs"

so here i want program output like 1=2
2=3
d=2
e=2
f=1
and s=1

it has to show no of duplicates in each character in string

View Replies View Related

Getting Characters Into Array From A String

Oct 17, 2014

As of right now my code can take characters from a string to an array from a string like "ABCD" but the project says I have to take it from a string like "A B C D" how can I correct my code to grab the characters from a single spaced line?

Scanner sc = new Scanner(System.in);
System.out.println("Enter Order of Cars:");
String carsInput = sc.next();
int x = carsInput.length();
int[] cars = new int[x];
for (int i=0; i < cars.length; i++) {
cars[i] = carsInput.charAt(i)-64;
}

View Replies View Related

Combining Characters Into A String

Sep 10, 2014

So what my program is supposed to do is take a number inputted by the use and then take a phrase. It then changes that phrases letter by the number inputted prior for example if you type in 2 as your int and Hello as your phrase you should get JGNNQ, which i can do. but the problem is that when i run it, it outputs like this:

J
G
N
N
Q

As separate characters how can I combine those characters in 1 string so it looks like JGNNQ? this is my code

import java.util.Scanner;
public class Dcod_MAin {
private static final Object[] String = null;
public static void main(String[] args){
Scanner input = new Scanner (System.in);
System.out.println("What is the day of the month");
int shift;

[Code] ....

View Replies View Related

How To Fill A String With Characters Randomly

Jul 5, 2014

I need to make a string filled with naughts and crosses like this one : "xxx ooo xox". There are 3 groups separated with a space. how to fill the string randomly ?

View Replies View Related

Removing Specific Characters From A String

May 7, 2014

I just need to write a simple program/function that replaces certain letters from a string (i.e. censor( "college", "aeiou" ) returns "cllg"). I'm trying to get the code right first, and then write a function for it.I basically just thought that I would iterate over the first string, and once I had the first character, I would then iterate over the second string, to see if the character exists. I'm getting a "dead code" error on my second loop because I put the second "break."

public class ap {
public static void main(String [] args){
String s = "Hello";
String s2 = "aeiou";

[code]....

View Replies View Related







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