This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I am a new learner of Java language and for the past two years I have been working with C++. I am gonna need something equivalent to cin.ignore() in Java.
In java equivalent of cin.ignore() is InputStream.skip(). You can refer to Java Docs
Normally in Java you would read the text and remove the bits you don't want. e.g. instead of doing this.
first = std::cin.get(); // get one character
std::cin.ignore(256,' '); // ignore until space
last = std::cin.get(); // get one character
std::cout << "Your initials are " << first << last << '\n';
you would do
Scanner in = new Scanner(System.in);
String first = in.next(), last = in.next();
System.out.println("Your initials are " + first.charAt(0)+last.charAt(0)+"\n");
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I am trying to create the following program in which I need to turn the input String's characters into lowercase to ease my work.
I tried using toLowerCase(); first but it didn't work. Then I tried using toLowerCase(locale); and yet I have not succeeded.
public static void Mensuration() {
Locale locale = Locale.ENGLISH;
Scanner inputz = new Scanner(System.in);
System.out.println("Which kind of shape's formula would you like to find out.2D or 3D?");
char dimension = inputz.nextLine().charAt(0);
if(dimension == '2') {System.out.println("Okay so which shape?");
String dimensiond = inputz.nextLine().toLowerCase(locale);
if(dimensiond == "rectangle") {System.out.println("Area = length x breadth\nPerimeter = 2(length + breadth)");}
}
}
I expected the program to give the accurate output but the thing that happens is that there is no output actually!!
use equals to compare strings. I think this causing the error
"rectangle".equals(dimensiond)
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
i want to ask a question for a bedsize and while the answer is not what i choose it will be i want that it will ask the user to answer again
import java.util.Scanner;
public static void main(String[] args) {
String newBedType ;
Scanner sc1 = new Scanner(System.in) ;
System.out.println("you want a single bed or doublebed? ") ;
newBedType = sc1.next() ;
while (newBedType != "single" + "doublebed") {
System.out.println("please choose againe the bed size: ");
newBedType = sc1.next() ;
switch (newBedType) {
case "single" : System.out.println("i see you like sleeping alone");
break ;
case "doublebed" : System.out.println("got company ;) ");
break ;
}
}
}
}
the code kinda works it shows the cases if i write the correct string but it will continue to ask me forever.....
i just stared learning java so be easy on me i know its a stupid question but after hours of trying and searching here(though i did found in python but dont know how to "translate" it to java)
i cant figure it out... thanks to anyone willing to help :)
Your issue is with the line:
while (newBedType != "single" + "doublebed")
This doesn't do what you think it does. You are comparing the variable newBedType with the string "singledoublebed", the addition operator is concatenating those two strings. You want the line:
while (!newBedType.equals("single") && !newBedType.equals("doublebed"))
Note the use of the .equals() method, as string comparisons in Java do not act as expected with the == or != operators.
This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 6 years ago.
I am beginning to learn Java and I am stuck on how to receive the user's input as a String and converting it to an int afterwards so I can use If Statements. Thank you for reading guys, good programming for all.
Try this:
JOptionPane pane // your control
int result = Integer.parseInt(pane.getInputValue().toString());
System.out.println("result = " + result);
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
What is the correct way to put string in a condition statement?
I tried this, but it won't print x.
String c = "c" ,d = "d";
Scanner in = new Scanner(System.in);
String selection = in.next();
if ( c == selection){
System.out.println("x");
Assume I input c.
Strings in Java are compared using str.equals(<string to compare>);
So in your case it would be if(c.equals(selection)) not if(c==selection)
This question already has answers here:
Dynamic variable names Java
(3 answers)
Closed 9 years ago.
So say I have a string like "ABC", is there any possible way for me to pull out these characters individually and assign values to those as if they were a variable name?
For instances
String temp = "ABC"; //temp.charAt(1) being 'B' and assigning 5 to
int temp.charAt(1) = 5; //the variable name 'B'
Obviously that syntax is no where near correct, I'm just using it to explain what I'm trying to achieve.
Is this even a possible thought?
Sorry for the trivial question, I'm fairly new to java.
Use a data structure instead, like this:
Map<Character, Integer> vars = new HashMap<Character, Integer>();
String temp = "ABC";
vars.put(temp.charAt(1), 5); // B = 5
System.out.println(vars.get('B')); // 5
System.out.println(vars.get(temp.charAt(1))); // 5