Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So I need to write a program that defines the String variable containing the name 'JOE BLOGGS'. I then need to printout in the console the 1st and 5th characters, How could I make a java program for this so that the name can also be changed and the program will still function correctly?
And then I have to extend the code to add (concatenate) the string ‘is an employee’ to JOE BLOGGS's name and convert the sentence to upper case, and print the resulting string to the screen, i.e.
JOE BLOGGS IS AN EMPLOYEE
Many thanks for your time:) I literally got no idea how to start as i'm quite new to Java.
Sometimes I feel like programming is just a bunch of Googling to answer my questions.
But never fear; here are a couple of hand-picked (although thoroughly unoriginal) links that I believe will answer all of your listed questions:
Oracle's Java Strings Tutorial
Oracle's java.lang.String
The Oracle tutorials I find surprisingly good. On the second link, just search for upper, or better yet, skim the methods section.
try this...
public class TestClass {
public static void main(String[] args) {
new TestClass().printChars("JOE BLOGGS", "is an employee");
}
public void printChars(String baseString, String additionalString) {
System.out.println(baseString.charAt(1));
System.out.println(baseString.charAt(5));
System.out.println(baseString + " " + additionalString.toUpperCase());
}}
People should really stop posting their homeworks, and start trying at least...
If you're taking a java class, i'd say the least you have learned for you to do this homework is already enough.
Anyways:
public class classname{
public static void main(String[] args){
String str = "Your String goes here"; //so usually you should put JOE BLOGGS between the quotes
System.out.println(str.charAt(1));
System.out.println(str.charAt(5));
str += " is an employee";
str = str.toUpperCase();
System.out.println(str);
}
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This is my code:
public class sample {
public static void main(String []args) throws Exception {
System.out.println("Enter from file:");
BufferedReader br=new BufferedReader(new FileReader("C:\\Users\\KK\\A Key.txt"));
String currentline;
while((currentline=br.readLine())!=null){
System.out.println(currentline);
}
BigInteger a = new BigInteger(currentline);
System.out.println(a);
}
I want to read from the text document , convert it into big integer from a string, I tried this but i get a run time error , How to convert String into corresponding Big integer Ascii value.
Your problem is very simple: you build a BigInteger from a null string!
You are looping until currentLine is null.
Afterwards you try to create a BigInteger from that!
Simply move things into your loop:
while((currentLine=br.readLine())!=null) {
System.out.println(currentLine);
BigInteger a = new BigInteger(currentline);
System.out.println(a);
}
Et voilà, things are working (assuming that you expect each line in your file to be a number). If the whole file represents one number, then you must do as zstring suggests in his comment: you would have to use a StringBuilder for example to "collect" all lines into a single string that you then use to create the BigInteger object.
And please note java coding styles: class names start Uppercase; and variable names use camelCase (thus I changed to currentLine).
But just for the record: you wrote a low quality question. You should always include compiler error messages or stack traces when asking "why is my code not working".
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I know this may be a weird question to be asking, but I came across this method and is not too sure of it's purpose.
I been reading online for it's explanation, but did not find one that is easily understood.
What I need is not a code, but rather why should it be implemented.
The snippet code are as follow:
private PrintWriter out;
private static final String end = "067e6162-3b6f-4ae2-a171-2470b63dff00 ";
//generated using the above method, UUID.randomUUID()
public static String getEnd() {
return end;
}
private void sendOutputToClientFile(String str) {
out.println(str);
out.println();
out.println(end);
}
The codes are found inside a server file, and is supposedly use to denote the end of message.
But what purpose does it serve here and why is it necessary?
P.S : I would prefer a layman's term of explanation...
The code writes the random string that is assumed (and with astronomic level of probability very likely to be true) unique, no other calls to UUID.randomUUID will probably ever produce it. Probably this string is checked at another end as the "end of stream" sequence.
Most likely the designer was not able to reserve any particular char sequence to mark the end of the data stream, and for some reason could not use the usual IO methods either. It is highly unlikely that such string will appear inside the stream as well for some reason just by chance.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm doing a small project in Java whilst I'm learning the language.
Basically what I'd like is for a user to input a string via JOptionPane.showInputDialog();, if that string is empty, I want it to make them re-enter a valid string and then the program will continue.
What I did consider doing is using goto but I read up on it and it said it's not good practice.
Any help would be greatly appreciated.
Thanks :)
I would do it in an infinite loop asking for input (using OptionPane.showInputDialog()).
Here you have some pseudo code:
message = ""
while message.equals(""):
message = ask_for_input() // (.trim() if needed)
end
You could do it in a loop. So you take a while-loop where the condition is that the input is empty. Then the message will pop up until the user entered something.
Despite goto is a reserved keyword in Java, it's actually (implicitly) meant for not being used.
JOptionPane.showInputDialog(...) returns a String.
I suggest using an infinite while loop and comparing it to empty, through String.isEmpty().
For instance:
String input = null;
while (true) {
input = JOptionPane.showInputDialog("Your input (not empty):");
if (!input.trim().isEmpty()) {
// TODO something with the input variable
break;
}
}
This is what do ... while loops are for. You could also use the Apache commons StringUtils class.
String input;
do {
input = JOptionPane.showInputDialog("Please enter your string");
} while (StringUtils.isBlank(input));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i am working n this lab for school and have am doing something wrong with my toString
this is what i have
public String getRelatives(String person)
{
String s = "";
s+=(person);
s+=(" is related to ");
for(String relative : map.get(person))
{
s+=(relative);
s+=(' ');
}
return s;
}
/**
* returns the String version of the entire map listing each key person and all of
* their relatives
*/
public String toString()
{
String output="";
return getRelatives();
}
i want it too look like this
Bob is related to John Tom
Dot is related to Chuck Fred Jason Tom
Elton is related to Linh
i know i am doing something wrong in my toString but to me at least it makes sence and am not sure how i should do it
The declared method getRelatives() takes a String as an argument. When you are calling it in the toString() method, you are not passing a String argument to it. Therefore, it gives you an error.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I am doing an assignment and I have to do several simple string manipulations. I think I have the last one figured out, and it works by itself, but it does not work when I put it together with the other string manipulations, giving me an arrayoutofbounds exception error. Any advice?
Here is the small code I made
public static void main (Strings[] args) {
Scanner sc = new Scanner(System.in);
String theSentence = sc.nextLine();
String [] theWords = theSentence.split(" ");
Arrays.sort(theWords);
System.out.println(theWords[1]);
System.out.println(Arrays.toString(theWords));
}
This does not work when it is put together with the rest of the code even though it works by itself. For reference, this code is supposed to take in a small sentence and give me the smallest word lexicographically. Ex: input: "4 WHAT WAIT IS THIS" output would be "IS"
The code is assuming that theWords will always have at least two elements in it. If the sentence provided by the user does not have any spaces, theWords will never get an element in position 1 and so the program will crash on System.out.println(theWords[1]);
In Java arrays are indexed from zero, where the 0th element is the first.
As Mark said you are assuming that there are always two words entered in this case. You should check the length of theWords before trying to access an element.