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?
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
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); } }
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.
One of our assignments this week was to make a program that when given a number it will return the biggest prime factor of that number. So for example if the program is given the number 15, the output is 3.
144 gives you 3 also. 17 gives you 17 21 gives you 7.
Anyways, after some time trying out various things and combining stuff i googled with my textbook I somehow stumbled upon a code that works. But I cannot for the life of me understand how it works. I think it has something to do with the inner loop and the outer loop. But I feel like i cant go on without understanding how it works.
Here is the code:
public class BiggestFactor { public static void main( String[] args ) { int N; N = StdIn.readInt(); int n = N;
Write a Java code of the method startsWithCount that takes an array of strings words and a String S. The method should return an integer that is the number of strings that starts with S.
For example if:words = { "All", "Arab", "size", "Almond", "Allowed", "here"} and S= "All", then the method should return 2
PHP Code:
public class StringwithCount { public static void main (String[]args) { String strings[] = { "All", "Arab", "size", "Almond", "Allowed", "here"}; String output= ""; for ( int i = 0; i < words.length; i++) { if (words[i].startsWith("s")) c + +; }
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;} } }
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) { int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 }; int num = 3; int count = numbers(a, num); for (int i = 0; i < a.length; i++) {
Write a recursive Java method countDigitx that takes two integer numbers as input (a number n and digit x) and returns the number of times digits x occurs in number n:
For example if n=23242 and x=2 then the method should return 3 that is the number of times the number 2 found in n.
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;
//read the file //make the numbers 1 string line //count the number of repetitiveness in the string for the numbers //display four lowest ones
import java.io.*; import java.util.*; public class Lottery2
[Code] ....
when I run it the array gets sorted but how do i keep the data in other words
what it is supposed to do is grab numbers from a file, and give me the lowest 4 numbers back. I know the numbers using the array but how do i pull out the lowest 4 and keep the data true no matter what numbers or how many of them are in the file.
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.
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 ....
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) {
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.
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
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
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).
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?
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()
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:
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.
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.