Ok so I'm trying to create a programmed where the user is asked to enter a word, this word is then stored as a variable, and then its displayed as an array.
This is what I have so far:
package coffeearray;
import java.util.Arrays;
import java.util.Scanner;
import java.lang.String;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
String drink;
String coffee;
int [] a =new int[6];
Scanner in = new Scanner(System.in);
System.out.println("Please input the word coffee");
drink = in.next();
So basically I need some code so that the word stored is then displayed as variable. For example "Coffee" would then be displayed but each letter on its own line.
I've searched all over and can't seem to find out how to do this. Thanks in advance.
Given string str :
String str = "someString"; //this is the input from user trough scanner
char[] charArray = str.toCharArray();
Just iterate trough character array. I'm not going to show you this as this can easily be googled. Again if you're really stuck let me know, but you should really know this.
Update
Since you're new to Java. Here is the code per Jeff Hawthorne comment :
(for int i=0, i < charArray.getlength(); i++){
System.out.println(charArray[i]);
}
You can use String.split("")
Try this:
String word = "coffee"; //you get this from your scanner
if(word.length()>0)
String [] characterArray = Arrays.copyOfRange(word.split(""), 1, word.length()+1);
You can convert the String to a CharSequence:
CharSequence chars = inputString.subSequence(0, inputString.length()-1);
you can then iterate over the sequence, or get individual characters using the charAt() method.
Related
I need to create an array of strings from user input and print the first letter of each element. I'm aware that I need to convert the array to a string somehow, but unsure how to accomplish this. I was unsuccessful with Arrays.toString
Following is my code:
import java.util.Scanner;
import java.util.Arrays;
class Main{
public static void main(String[] args){
Scanner inp = new Scanner(System.in);
System.out.println("How many names would you like to enter in this array?: ");
int numName = inp.nextInt();
String nameArray[] = new String[numName];
System.out.println("Enter the names: ");
for(int i = 0; i <= nameArray.length; i++){
nameArray[i] = inp.nextLine();
}
System.out.println(nameArray.charAt(0));
}
}
You need to iterate over every String in the Array and then print the first char. You can do this using charAt() and a loop.
for(String str : nameArray) {
System.out.println(str.charAt(0));
}
Or you can use Arrays.stream():
Arrays.stream(nameArray).forEach(e -> System.out.println(e.charAt(0)));
Also just a few problems with your code:
You are going to enter into this problem, because nextInt() does not consume the newline. Add a blank nextLine() call after nextInt()
You are looping until <= array.length which will cause an indexOutOfBounds error. You need to only loop until less than array.length
Just do another iteration over the "nameArray", and get the first character of each array element, and print it.
For example, you can use for-each:
for(String name : nameArray) {
System.out.println(name.charAt(0));
}
Arrays.stream(nameArray).map(s -> s.charAt(0)).forEach(System.out::println);
I'm trying to create a program in BlueJ that allows the reader to type any word, then print out: that word, the length of the word, and whether or not the word contains "ing". I've figured out how to print the word and its length, but I can't figure out how to include whether "ing" is in the word.
Here is my code:
import java.util.*;
public class One
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = "";
System.out.println("Type in a word");
str = sc.nextLine();
//System.out.println(str);
System.out.println(str.length());
}
}
How can I tell whether "ing" is included in the word?
You can do that using the contains() method on your resulting string:
if (str.contains("ing")) System.out.println("Contains ING");
If you need to match either lowercase or uppercase, you can just convert str to upper case and check that instead:
if (str.toUpperCase().contains("ING")
I have explained what I want it to do down below. I'm having a hard time trying to figure it out and I would really appreciate some help.
import java.util.*;
import java.lang.*;
class Main {
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
System.out.println("Input a palindrome");
String str = s.nextLine();
String strL = str.toLowerCase();
String[] strSplit = strL.split("");
// After this I need to alter the array and make all 4's become a's, all 3's become e's, and all 0's become o's. then I need to make that a string named "strSplitRev". I have no idea how to do that.
}
}
What you need to do is loop through your array strSplit using a for loop which would look something like this
for(int i = 0; i< strSpil.length; i++)
inside your for loop you would check if strSplit[i] equals 4,3 etc and replace it with a,e etc. For example
if (strSplit[i].equals("4"){
strSplit[i] = "a"
}
this or you could use switch/case statements. And in the same loop you can append all the individual characters to a StringBuilder object
Hope it'll be helpful. Feel free to ask if something is unclear.
String strSplitRev="";
for(int i=0;i<strSplit.length;i++){
System.out.println(strSplit[i]);
if(Integer.parseInt(strSplit[i])==0){
strSplitRev+="o";
}
else if(Integer.parseInt(strSplit[i])== 3){
strSplitRev+="e";
}
else if(Integer.parseInt(strSplit[i])==4){
strSplitRev+="a";
}
else strSplitRev+=strSplit[i];
}
I need help doing the following:
receiving input using Scanner class (I got this)
taking input from scanner and making it a String
use replaceAll to remove numbers 0-9 from user input.
The below code is what I have so far but it is only returning user input and not removing numbers:
public static void main(String[] args) {
Scanner firstname = new Scanner(System.in);
System.out.println("Please enter your first name:");
String firstname1 = firstname.next();
firstname1.replaceAll("[^0-9]","");
System.out.println(firstname1);
Updated Code. Thank you Hovercraft. I am now investigating how to retrieve all alpha characters as with the code below, I am only getting back the letters prior to the numeric values entered by the user:
import java.util.Scanner;
public class Assignment2_A {
public static void main(String[] args) {
Scanner firstname = new Scanner(System.in);
System.out.println("Please enter your first name:");
String firstname1 = firstname.next();
firstname1 = firstname1.replaceAll("[^A-Z]","");
System.out.println(firstname1);
String input = yourScannerObject.nextLine ();
where "yourScannerObject" is the name you give your scanner.
What method did you use to scan? is it {scanner object name}.next() ?
if so you have got a string and all that you have to do is create some string, and save the input to it, e.g.:
String str="";
str = {scanner object name}.next();
before using anything in java, I would advise you to read the API :
http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#next()
receiving input using Scanner class (I got this)
taking input from scanner and making it a String
use replaceAll to remove numbers 0-9 from user input.
Here's an example:
String in;
Scanner scan = new Scanner("4r1e235153a6d 6321414t435hi4s 4524str43i5n5g");
System.out.println(in = (scan.nextLine().replaceAll("[0-9]", ""))); // use .next() for space or tab
Output:
read this string
The problem in your code is the regex "[^A-Z]" is set to remove all non-alphabet capital characters. This means you remove all lower case as well. You could say "[^a-zA-Z]", but then you're also removing special characters.
import java.util.Scanner;
public class CourseSplitter {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
char[] course; //course code format: ABCDE##
String code;
//int num;
System.out.println("Input Course: ");
course = keyboard.next();
System.out.println(course);
code = String.copyValueOf(course, 0, 4);
System.out.println(code);
}
}
I don't know how I should let the user input the course when I'm using a character array instead of string. In short, how do I use the "scanner" on character arrays?
The instruction is the user will input a course code in the format: ABCDE##
Then, the program must split it into the course name and the course number. So, I had to use the copyValueOf method but it doesn't seem to work because from all the articles I read online, they used a char[] array but initialized the array with some value. So I was wondering how I could use the scanner on character arrays.
Why not just read a string from the scanner and then call String.toCharArray? It's not even clear why you need a char array here...
Why not just read a string directly with scanner.nextLine?
import java.util.Scanner;
public class CourseSplitter {
public static void main(String args[]){
Scanner keyboard = new Scanner(System.in);
System.out.println("Input Course: ");
String course = keyboard.nextLine();
System.out.println(course);
String code = course.substring(0, 5); //You put 4 but it left out the last letter in the course name. I changed it to 5 and it worked but I'm confused since the index always start with 0.
System.out.println(code);
String num = course.substring(5, 6);
System.out.println(num);
}
}