Return A String In While Loop

Nov 18, 2014

Here, I want to avoid using the line **return"";** and instead return all of the values I output with **System.out.println();**

I have tried using a **StringBuilder** method, but this outputs nothing when I run my test program. I am guessing that StringBuilder must work differently in a while loop, but I can't find how!

[URL] ....

public String toString(){
int topLine = size;
int topCurrentLine = size;
int bottomLine = 1;
int bottomCurrentLine = size;

[Code] ....

View Replies


ADVERTISEMENT

Method Creation - Take A String With Duplicate Letters And Return Same String Without Duplicates

Nov 7, 2014

How can I write a method that takes a string with duplicates letters and returns the same string which does not contain duplicates. For example, if you pass it radar, it will return rad. Also i would like to know how can I Write a method that takes as parameters the secret word and the good guesses and returns a string that is the secretword but has dashes in the places where the player has not yet guessed that letter. For example, if the secret word is radar and the player has already guessed the good guesses letters r and d, the method will return r-d-r.

View Replies View Related

Method Must Return Int Type - If Given Integer Is Strong Return A / If Not Return B

Sep 7, 2014

I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?

I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.

mlong should return an int depending on the X.moth. at the moment my code looks like this:

// File1:
public class date {
public int day;
public int month;
public int year;
}

// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}

View Replies View Related

How To Return Value Certain As Number And Some As String

Nov 23, 2014

I've to return some value as a string and some as a int, how is this possible? Here's my code:

public class Card {
public void start(){
String [] suit = {"Spade","Club","Diamond","Heart"};
int [] number = {1,2,3,4,5,6,7,8,9,10,11,12,13};}
public String getColour(){
String [] suit = {"Spade","Club","Diamond","Heart"};

[Code] .....

So at the top, i've set it to return value as string because of the King Jack Queen and Ace, but i also have to return as numbers(int). And also i'm using a loop to read all the numbers, is there any other way?

View Replies View Related

String Method To Return A Double

Jul 11, 2014

I'm having an issue with figuring out how to structure and call a method using a string prompt. My prof has given us specific methods to create, and the one I'm having issues with is blic static double getOperand(String prompt). What I have currently is this:

public static double getOperand(String operand1, String operand2){
Scanner input = new Scanner(System.in);
System.out.println("What is the first operand?");
operand1 = input.toString();

System.out.println("What is the second operand?");
operand2 = input.toString();
return operand1, operand2;
}

which is obviously not working. When I asked my prof he sent me this, "The String paramater in the getOperand() method is intended to have the prompt value that is passed to it. Within the method, the prompt is then displayed, what the prompt is asking for is input, and then returned to the method call.For example, if the method is public double getOperand(String prompt) within the method you would display the prompt, as in System. out. println(prompt), use the Scanner class to create an object, perhaps called input, and then use the input object to get the value asked for by the prompt. Once you have that value, which in this example would be a double, then return the value to the method that called it.

The call could be something like this: double value = getOperand("What is the first operand?");". But that only works a little. Clearly I'm missing something and no matter what I google, or how many times I read the chapter, I am just not getting this. Everything else up to this point has been easy. and I can create regular methods just fine, the menu method works, but I can't figure out this string method and all the rest build off this method.

View Replies View Related

How To Return XML File / String From Web Service

Nov 10, 2014

I have a web service which is returning a string. I want to extend this web service so that it returns an xml based string. To begin with I am able to return xml based strings from a normal POJO class ....

View Replies View Related

Return A String That Holds User Inputs

Apr 18, 2015

I'm working on an assignment for my Java Programming class that requires me to work with Multiple Classes.

Overview of the assignment: Create a "Database" that allows a user to input information about a College Class.

My program has three Classes:

The information that must be tracked in the Course Class is:
- Section Number
- Instructor
- Classroom
- Students (an ArrayList of class Student)

The information that must be tracked in the Student Class is:
- Name
- Major
- GPA
- Hours

The Driver class will use the main method to print the user a prompt asking for inputs, then display all of the user's inputs after he/she wished to exit the program.

I'm having no trouble with asking the user for inputs and then checking the validations for the inputs, I'm currently struggling with return a String that houses all of the user's inputs for the variables above. The information must be returned in a method called toString(). Both the Student and Course Class must have a toString() method that returns the information in the class (value of the variables), I must then call upon these methods from the main method of the Driver Class to print them back to the user.

The toString methods within the Student & Course Classes are at the bottom of the code

To Summarize: What I need to do within the toString() methods to return the values the user has inputted for the various variables? Such as.. Should I pass something through the method? Do I need the this keyboard to pass through the variable again? I just fear that I'm currently misunderstanding a very critical part of programming.

My Driver Class

import java.util.Scanner;
public class Driver {
Course courseInputs = new Course();
Student studentInputs = new Student();
public static void main(String[] args) {

[Code] .....

View Replies View Related

How To Return A String From Java To Android Project

Jun 26, 2014

I tried many times to return a string from java project to an android project But it keeps sending incorrect values as in as it should be 1 here is an example.

Java Project:

boolean somethingboolean = false;
if(something == "1"){
somethingboolean = true;
}
public static String getString(){
if(somethingboolean == true){
return "TRUE";
}else{
return "FALSE";
}
}

Android project:

System.out.println(JavaProject.getString())

And in the android project it prints "FALSE"

So what should i do?

View Replies View Related

Return Given Char That Matches Input String

Apr 17, 2015

Processing a string. How would I only return a given char that matches the input string e.g. v and/or n and/or m.

Everything else that does not match will return a '*' - e.g. user input = t result = *

I assume I need to also iterate through this input string using charAt() ?

View Replies View Related

Students GPA Database - Return String With All Information

Dec 13, 2014

Here is the question with my Pseudocode:

Create an application that keeps track of students. The application should have 3 new classes.

1. Create a class which will represent an instance of 1 student:

I want to be able to display the user's name (Last, First), Age (in years as an integer) and GPA (to 2 decimal points). This class only needs a constructor and a toString method.

2. Create a Team class that will represent a group of students.

This class should allow the driver class to manage students for a given course. The class will keep track of the course name and section, the students in the class, and allow the driver to specify the max number of students to keep track of.

Methods:
1.Constructor
2.It should have a method to insert a single student into the array and return a Boolean indicating success or failure.
3.It should also have a method which returns a string with all of the course and student information formatted for display. You should make the name 12 characters long.

3. Create a driver class that will manage the whole process.

Pseudocode:

BEGIN Driver main method
Initialize student count to 0
Input size of the array
Instantiate a copy of the Team class
Input a student name (or quit)
WHILE (the student name is not quit)
Input student age
Input student GPA

[Code] ....

View Replies View Related

Assign A Value To Numeric Variable Then Manipulate It And Return A New String

Aug 18, 2014

At first I wanted to just use an array and set each day a value, however I was told that it has to be stored as a string.

Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type Day:

A. Set the day.
B. Print the day.
C. Return the day.
D. Return the next day.
E. Return the previous day.
F. Calculate and return the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.

import java.util.*;
public class Day {
static Scanner readinput = new Scanner(System.in);
String day;
public Day(String day) {

[Code] ....

So right now if I run my code it allows me to type in a day, then it gives me the next and previous day, the last part is to add X days to it. Ideally I would like to be able to take the day entered, depending on the day set a numeric value, then add prompt for number of days you want to add. The program should then use modal (%7 ) to add value to the value that was given based on the day, and then translate it back into a String value (the day).

View Replies View Related

Servlets :: Some Methods Such As ServletRequest GetParameterValues Return A String Array

Jan 5, 2015

Some methods such as ServletRequest's getParameterValues return a String array whereas others (e.g. HttpServletRequest's getHeaders) return an Enumeration. Do these return types need to be learned parrot-fashion, or is there some sort of logic to it?

View Replies View Related

ToString Method Return A String Rather Than Display A Message To Screen

Aug 11, 2014

I need making the toString() method return a String rather than display a message to the screen. Also, I'm not supposed to call the toString method in my demo class to test it, so what should I do instead?

public class cupDispenser {
String location;
int noOfCups;
cupDispenser(String location,int cups)
{
this.location=location;
this.noOfCups=cups;
}
public String getlocation()

[Code]...

View Replies View Related

Capitalize Method - Return String With First Letter In Uppercase And All Other In Lowercase

May 19, 2015

I'm trying to create a private method called capitalize() which takes String nameModel in any uppercase/lowercase combination and returns it with the first letter uppercased and all other lowercased. E.g. "stePHeN" returns "Stephen" . This is what I've written so far:

private String makePrettyString(String modelName) {
if (modelName ==null) {
return null;
}else if (modelName =="") {
return "";
} else{
return modelName.substring(0,1).toUpperCase() + modelName.substring(1).toLowerCase();
}
}

Unfortunately it doesn't work and it still returning me the String modelName in its original uppercase/lowercase combination.

View Replies View Related

Java Method - Return True If String Can Be Found As Element Of Array And False Otherwise

Mar 17, 2014

I am new to Java and would like to ask how to do this question specifically:

Code a Java method that accepts a String array and a String. The method should return true if the string can be found as an element of the array and false otherwise. Test your method by calling it from the main method which supplies its two parameters (no user input required). Use an array initialiser list to initialise the array you pass. Test thoroughly.

View Replies View Related

String Split Method - Return Array Of Strings Consisting Of Substrings Splitted By Delimiters

Sep 28, 2014

I am having a problem with the following code. It compiles and runs fine however my output is wrong.

public class SplitString {
public static void main(String[] args) {
String[] string1 = split("ab#12#453", "#");
String[] string2 = split("a?b?gf#e", "[?#]");
for (int i = 0; i < string1.length; i++) {
System.out.print(string1[i] + ",");

[code]....

The split method in the String class returns an array of strings consisting of the substrings split by the delimiters. However, the delimiters are not returned. Implement the following new method that returns an array of strings consisting of the substrings split by the matching delimiters, including the matching delimiters.public static String[] split(String s, String regex)For example, split("ab#12#453", "#") returns ab, #, 12, #, 453 in an array of String, and split("a?b?gf#e", "[?#]") returns a, b, ?, b, gf, #, and e in an array of String.

View Replies View Related

How To Put All Char In Loop To String

Mar 16, 2014

I can't figure out how to have all of the random characters generated to go into the String. Below I can only get the last character to covert over to a String.

System.out.println("Original random character string:");
String printingString = "a";
for (int i = 0; i < 50; i++)//loop to obtain 50 random characters
{
char randomChar = (char) ((Math.random() * 255) +32);
System.out.print(randomChar);
printingString = Character.toString(randomChar); }
return printingString; }

View Replies View Related

String Out Of Bounds Caused In While Loop

Jul 24, 2014

The code is supposed to insert HTML formatting on a specified string. (i.e o<b>the</b>r )Most of the code works correctly as I verified the outputs without a while loop. The loop is causing a string out of bounds error. I've tried adding the if statement and also formatting the (result += line) nothing is getting appropriate results...

public static String addFormat(String webpage, String toMatch, String format) {
String result = "";
String insert = "";
format = format.toLowerCase();
switch (format){
case "bold":
insert = "<b>" + toMatch + "</b>";

[code]....

View Replies View Related

Declare String Inside Of Loop - Use It Outside

Apr 26, 2015

I'm trying to code a little text RPG. I've made a little "personality test", with 4 questions (answers a b c), where every letter stands for a type of personality. The analysis of the result is simple counting of the answers, if you have 3 answers a (4 questions), then a has won. If 2 on a, and 2 on b, a simple Random method shows weather result a or b.

Now... the test should give you a "partner" (imagine the pokemon game with 3 different starter pokemon). I have now 3 string variables, like 3 different partners. They are all declared somewhere outside, but i only need one later.

Like...the test is completed, you have your partner and how can i make now that the program is just showing me my partner? Like...when i type " System.out.println(partner); " (outside of the test loop! ) that i only get the one i got through the test?

I was trying to declare in every loop the partner String with every result. But outside the loop java isn't recognising that String, bcs...ofc...it was declared for the loop. So i had in every if or else if clause a " String partner = anwsA;" and answB and answC, thats why i cant declare them outside.

Short: i need a partner String variable that could have 3 possible results... i jut need one

I'm using Eclipse Luna 4.4

View Replies View Related

How To Return Values In Object Return Type Function (method)

Apr 2, 2014

How do i take input values for TwoDPoint (which are objects) and return it back in numerical values also print them.

When i create an object in main method and pass values to my function of return type TwoDPoint,it gives error:- found int,int need TwoDPoiint,TwoDPoint.

// Here is what i tried to do:

Created class TwoDPoint that contains two fields x, y which are of type int. Defined another class TestTwoDPoint, where a main method is defined.In the main method created two TwoDPoint objects.

Then I modified the class TestTwoDPoint and add another function to it. This function takes two TwoDPoints as input and returns the TwoDPoint that is farthest from the point (0,0).

Then I added another function to TestTwoDPoint. This function takes two TwoDPoints as input and returns a new TwoDPoint whose x value is the sum of x values of the input TwoDPoint's and whose y value is the sum of the y values of the input TwoDPoint's.

class TwoDPoint {
int x = 2;
int y = 4;
}
class TestTwoDPoint {
public static void main(String args[]) {
TwoDPoint obj1 = new TwoDPoint();
System.out.println(obj1.x);
System.out.println(obj1.y);

[Code] ....

View Replies View Related

How To Pass A Loop In Method String ToString

Apr 7, 2014

I am looking to pass a long list of results in a String toString() method,this is my code

public void newlist(){
for(int i = 0 ; i <= nbComposant;i++){
System.out.print(ref+i+" (quantity "+quantity+i+")");
}
}
public String toString(){
return newlist();
}

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

Returns Area Of Triangle / Unless Triangle Not Value So Return String?

Dec 28, 2014

I'm doing a problem where the area of a triangle is returned (if valid). However, I want to return a message (i.e. 'triangle is not valid) if the triangle is invalid.

I'm not sure how to go about to doing this as my method (called area) will only let me return doubles. Possible to return a string in an else within my area method?

public class MyTriangle {
public static void main(String[] args) {
//triangle is valid if the sum of any two sides is bigger than the third
System.out.println(isValid(3, 4, 5));
System.out.println(area(543, 4, 5));

[Code] ...

View Replies View Related

How To Return A String From Project Into Android Project

Jun 26, 2014

I tried many times to return a string from java project to an android project But it keeps sending incorrect values as in 2 as it should be 1 here is an example.

Java Project:

boolean somethingboolean = false;
if(something.equals("1")){
somethingboolean = true;
}
public static String getString(){
if(somethingboolean == true){

[Code]...

Android project:

System.out.println(JavaProject.getString())

and in the android project it prints "FALSE"

So what should i do?

View Replies View Related

If Given Integer Is Even Return 1 / If Not Even Return 0

Sep 7, 2014

I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is even return 1, if it is not even return 0. How would you write that in a Code?

I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.

mlong should return an int depending on the X.moth.

At the moment my code looks like this:

// File1:

public class date {
public int day;
public int month;
public int year;
}

// File 2:

public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}

View Replies View Related

Unable To Print A Loop Inside Array Of Greater Size Than Loop Itself

Jan 27, 2015

I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.

int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;

now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.

View Replies View Related







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