How can i make the value the the user inputs appear next to thew print statement? - java.util.scanner

import java.util.Scanner;
public class nameP{
public static void main(String[] args){
Scanner user = new Scanner(System.in);
int value;
System.out.println("Type a number: ");
value = user.nextInt();
if (value % 2 == 0)
System.out.println("even");
else
System.out.println("odd");
}
}
Need help making the value given by the user appear right next to the print statement....and not in the bottom as it commonly happens. Help would be greatly appreciated.

Use System.out.print
instead of System.out.println
println prints a newline after the String you send it.

System.out.println("The number " + value + " is even");

Related

Can anybod tell my why java.util.Scanner is throwing this exeption?

import java.util.Scanner;
public class FractionTester
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int num1 = scan.nextInt();
int num2 = scan.nextInt();
int num3 = scan.nextInt();
int num4 = scan.nextInt();
}
}
This code keeps throwing the error
FractionTester.java: Line 10: java.util.NoSuchElementException
can anybody explain why or how to fix it, please? Thank you all in advance.
are you sure you are passing 4 input values to the prompt because as its name says NoSuchElementException means scan.nextInt() is not getting any element.
I had tried you code in local and its working for me while passing 4 input values.
see the below code I am giving 4 values to it
now in here see I am giving only 3 values so it throw the exception

groovy.lang.MissingMethodException String vs java.lang.String

I was doing a coding challenge that prints a given text in a zig-zag:
thisisazigzag:
t a g
h s z a
i i i z
s g
So I have my code (not sure if it's right or not yet, but that's not part of the question)
class Main {
public void zigzag(String text, int lines) {
String zigLines = [];
while(lines > 0){
String line = "";
increment = lines+(lines-2);
lines = lines + (" " * (lines-1));
for(int i=(lines-1); i<text.length(); i+=increment) {
line = line + text[i] + (" " * increment);
}
zigLines.add(0, line);
lines--;
}
for(line in zigLines){
println(line);
}
}
static void main(String[] args) {
zigzag("thisisazigzag", 4);
}
}
But when I run the script, I keep getting this error:
groovy.lang.MissingMethodException: No signature of method: static Main.zigzag()
is applicable for argument types: (String, Integer) values: [thisisazigzag, 4]
Possible solutions: zigzag(java.lang.String, int)
And I'm very confused as to the difference between java.lang.String and String and then Integer and int?
Any help or explanation of this would be great!
Thanks.
You should make your zigzag method static.
Your code wasn't working because without the static modifier zigzag was an instance method, meaning you would need an instance of your class Main to be able to call it. Here's an introductory tutorial explaining some of these concepts: docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

store string from 3 jcombobox's in one jtextfield

Currently when clicking the jButton, jComboBox1 Text will appear in jTextArea1 but text from jComboBox2 does not appear. Any pointers on how to have the text from both comboboxes appear in 1 textfield?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jTextArea1.setText(jComboBox1.getSelectedItem().toString());
jTextArea1.setText(jComboBox2.getSelectedItem().toString());
I figured it out. Not sure if this is the most efficient considering there are a total of about 15 comboboxes but here it is.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String severity1;
String s1 = s1ComboBox.getSelectedItem() + " ";
String timing1;
String t1 = t1ComboBox.getSelectedItem() + " ";
jTextField2.setText(s1 + t1);
If anyone knows a more efficient way please let me know.

Taking a string from a user and checking it against a "Password" variable

I am trying to create a program that takes a string from a user and checks it against a "password" variable. If the password is equal it prints "valid" if not then "invalid"
Here is what I have so far, it looks correct to me, but it apparently isn't.
import java.util.*;
class WS6Q5 {
public static void main (String[] args){
Scanner in=new Scanner(System.in);
String s =" ";
int x = 123;
System.out.println("Please type in the Password");
s=in.nextLine();
if (s.length()==x){
System.out.println("Access Granted");
}
else if (s.length()!=x){
System.out.println("Invalid");
}
}
As one might expect, s.length() gives you the length, in characters, of string s. Your variable x is set to the integer (not string!) value 123. So your if-statement is seeing whether x is 123 characters long. Probably not what you want!
You probably want x to be a string. String x = "123".
You want to compare the value of x to the value of s. Look at String.equals() (http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#equals(java.lang.Object))

Finding sub-strings in Java 6

I looked through the String API in Java 6 and I did not find any method for computing how many times a specific sub-string appears within a given String.
For example, I would like to know how many times "is" or "not" appears in the string "noisxxnotyynotxisi".
I can do the long way with a loop, but I would like to know whether there is a simpler way.
Thanks.
Edit: I'm using Java 6.
org.apache.commons.lang.StringUtils.countMatches method could be preferred.
Without using an external library, you can use String.indexOf(String str, int fromIndex); in a loop.
Update This example fully works.
/**
* #author The Elite Gentleman
* #since 31 March 2011
*
*/
public class Test {
private static final String STR = "noisxxnotyynotxisi";
public static int count(String str) {
int count = 0;
int index = -1;
//if (STR.lastIndexOf(str) == -1) {
// return count;
//}
while ((index = STR.indexOf(str, index + 1)) != -1) {
count++;
}
return count;
}
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Test.count("is"));
System.out.println(Test.count("no"));
}
}
You can do this, but a loop would be faster.
String text = "noisxxnotyynotxisinono";
String search = "no";
int count = text.split(search,-1).length-1;
System.out.println(Arrays.toString(text.split(search,-1)));
System.out.println("count= " + count);
prints
[, isxx, tyy, txisi, , ]
count= 5
As you can see this is correct if the text starts or ends with the search value. The -1 argument stops it removing trailing seperators.
You can use a loop with indexOf() which is more efficient, but not as simple.
BTW: Java 5.0 has been EOL since Aug 2007. Perhaps its is time to look at Java 6. (though the docs are very similar)

Resources